nirisha chilukuri
nirisha chilukuri

Reputation: 21

Select and place different shapes in canvas using OnTouch event

I am new to android programming In my project layout, I need created

  1. Color palette which is in grid layout(I put some buttons and set color background).
  2. Shapes(Triangle,square and circle which are buttons as well) is in Linear layout
  3. Next to these two is Relative Layout where users can draw the shapes

when user touch on one of the shape and then touch on the relative layout (which is next to shapes), that particular shape should be drawn and so as colors. For example if user touch on circle shape, then touch on the screen, circle should be drawn at the point where user touched.

I managed to create two touch events in two different classes, i.e one for selecting the shapes and other for placing shapes in layout.

I have no idea how to combine those two classes together.

Can anyone please give me an idea how should I approach this project. Where should I create the shapes (should I create a separate classes for each shape/in onDraw())? If I create shapes in onDraw() how can I call in onTouch()?

Any help would be great. Thanks in advance.

I hope I explained properly, sorry I am not good in English and this is the first time I am posting in this forum.

Upvotes: 2

Views: 938

Answers (1)

Ganesh AB
Ganesh AB

Reputation: 4698

Generally to draw shapes on Canvas with touch event, we used code something like below, may be this will help you.

  @Override
        protected void onDraw (Canvas canvas) {
            super.onDraw(canvas);

            canvas.save();
            canvas.drawBitmap(mBitmap, 0, 0, null);
            canvas.translate(xPos, yPos);
            editIcon.draw(canvas);
            canvas.restore();

       //     invalidate();
        }
        @Override
        public boolean onTouchEvent (MotionEvent event) {

            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN : 
                    xPos = event.getX();
                    yPos = event.getY();
                    invalidate(); // add it here
                    break;
            }

            return true;

        }

Check this example also,

http://android-er.blogspot.in/2010/05/android-surfaceview.html

Upvotes: 1

Related Questions