开发者

Drawing onClick in Android

开发者 https://www.devze.com 2023-02-06 17:09 出处:网络
I am trying to make a program the makes a graph using user input values from 2 EditText Fields. The program should use this input to draw a line from the center of an axis to a point specified by the

I am trying to make a program the makes a graph using user input values from 2 EditText Fields. The program should use this input to draw a line from the center of an axis to a point specified by the information. I have a button that each time it is clicked should make a new line to the point specified (so there can be more than one line) I have created a custom view to hold the axis, but that utilizes its onDraw method, obviously, so I cant use it also to draw the new line.

Here is the code for my custom view:

开发者_开发技巧
package android.physicsengine;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class AxisDrawing extends View{

public AxisDrawing(Context context){

    super(context);
}
public AxisDrawing(Context context, AttributeSet attrs){

    super(context, attrs);

}
public AxisDrawing(Context context, AttributeSet attrs, int defStyle){

    super(context, attrs, defStyle);
}

@Override
protected void onDraw(Canvas canvas){
    canvas.drawColor(Color.BLACK);
    Paint linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    linePaint.setColor(Color.RED);
    canvas.drawLine(canvas.getWidth()/2, canvas.getHeight()/2-200,        canvas.getWidth()/2 ,canvas.getHeight()/2+100, linePaint);
    canvas.drawLine(canvas.getWidth()/2-150, canvas.getHeight()/2-75, canvas.getWidth()/2+150 ,canvas.getHeight()/2-75, linePaint);
}
}


If your custom view class is defined in the activity, then it is an inner class of that activity and has access to variables and arrays which are defined on the activity level.

Every time the user clicks the button, you should process and store the information into these common variables or arrays which onDraw can access and from them calculate the next line or the entire graph. If your custom view is a separate class, then you need to pass the data, one way to do it is to use static varibales..

to make onDraw() method draw again your graph you need to state:

myCustomView.invalidate();

in the button click event, just after you set the new data for the graph.


You simply need to set the data in the custum view (Globally ) and call invalidate which will redraw the view.

importandroid.content.Context;
importandroid.graphics.Canvas;
importandroid.graphics.Color;
importandroid.graphics.Paint;
importandroid.util.AttributeSet;
importandroid.view.View;


@Override
protectedvoidonDraw(Canvascanvas
{

    canvas.drawColor(Color.BLACK);
        PaintlinePaint=newPaint(Paint.ANTI_ALIAS_FLAG);
    linePaint.setColor(Color.RED);
    canvas.drawLine(data,data,getWidtt()-data,getHeight()-data,linePaint);

    //you can also pplaceinvalidate() here which will recursively redraw the canvas in aloop

}

publicvoidsetData(intdata)
{
    this.data=data;
    invalidate();
}
}
0

精彩评论

暂无评论...
验证码 换一张
取 消