I have a custom view called DrawView created in the main activity. I have implemented the onDraw() method in the DrawView class and it initially draws a circle. I have then added a touch listener, so that when a user clicks, it then draws a square. I am up to the part where the user clicks and a square is drawn. I’m not to sure how to go about this.
public class TestActivity extends Activity {
DrawView drawing;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ViewGroup myLayout = (ViewGroup) findViewById(R.id.mainLayout);
drawing = new DrawView(this);
myLayout.addView(drawing);
drawing.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// draw a square
}
return true;
}
});
}
private class DrawView extends View {
public DrawView(Context context) {
super(context);
}
protected void onDraw(Canvas canvas) {
Paint myPaint = new Paint();
myPaint.setColor(Color.BLACK);
// draw a circle
}
}
}
Help would be much appreciated.
here is the simple snippet for drawing the rectangle when user down,move and up of touch listener fire, just override in DrawView class not by setOnTouchListener()
define the Rect r = new Rect() in DrawView class then after implement this code in DrawView class
}
here is the onDraw()