I want to create a simple drawing application. I want to be able to add somme lines, circles and rectangles. And I want to select any shape and move it.
I've tried this way :
- add a view per shape, and draw the shape in the onDraw method of the view
- add each views to a relative layout
I can see all the shapes, but I can only touch the first view, because it fills all the screen. I searched in the samples an example of a "layered" app, but without success
Thks :)
Edit : Code added.
If I click on the rectangle, I can see the log, but if I click on the circle, there's no log.
Edit 2 : Oups, I forgot to attach the listener in the Circle... :( sorry
The layout :
public class DrawingView extends RelativeLayout {
public DrawingView(Context context) {
super(context);
this.setBackgroundColor(0xFFFFFFFF);
this.addView(new Circle(context));
this.addView(new Rectangle(context));
}
}
The Circle :
public class Circle extends View implements View.OnTouchListener {
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
public Circle(Context context) {
super(context);
this.paint.setStyle(Paint.Style.STROKE);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
this.setMeasuredDimension(100, 100);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(50, 50, 50, this.paint);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d("myApp", "cercle");
return false;
}
}
And the rectangle :
public class Rectangle extends View implements View.OnTouchListener {
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
public Rectangle(Context context) {
super(context);
this.paint.setStyle(Paint.Style.STROKE);
this.setOnTouchListener(this);
this.setX(50);
this.setY(50);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
this.setMeasuredDimension(100, 100);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawRect(0, 0, 100, 100, this.paint);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d("myApp", "Rectangle");
return false;
}
}