1

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;
    }

}
5
  • how could android tell the difference between a touch on the top layer and "oh, this person must've really wanted to touch the 3rd from bottom layer"?
    – Marc B
    Commented Nov 20, 2012 at 20:21
  • Hmm, thks for the irony. With flash it's very easy, I juste have to add a "clip" per shape and attach a listener on it.
    – fego
    Commented Nov 20, 2012 at 20:36
  • 1
    Why does the view fill the screen? If you set the dimensions correctly, it won't.
    – Herr K
    Commented Nov 20, 2012 at 20:45
  • Thk you for your answer. I fixed the dimensions of the view with "onMeasure" which call "setMeasuredDimension", and it's better, the click not detected on the entire screen, only in the first view. But the onTouch method of my second view is still not call
    – fego
    Commented Nov 20, 2012 at 21:34
  • I've added the code, maybe can you see the mistake ?
    – fego
    Commented Nov 21, 2012 at 8:14

1 Answer 1

1

You want to create your shapes in the same view. Use the ShapeDrawable wrapper and put each one into the same arraylist when it is created. Then check the position in the arraylist to figure out which shape they want to move when overlapping.

3
  • Thks for your answer. How can I check on which shape the user clicked ? There's no way to attach a listener on a shape. Shall I deduce it from the coordinates of the event ?
    – fego
    Commented Nov 20, 2012 at 21:09
  • 1
    I don't have the API in front of me at the moment, but based but i think drawable should have methods for get coordinates and or bounds. So you will want your ontouch to run through array from the end and check coordinates.
    – anthropomo
    Commented Nov 20, 2012 at 21:55
  • ok, thk you for your proposition, if there is no other solution, I will do this way.
    – fego
    Commented Nov 20, 2012 at 22:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.