2

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.

Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168

1 Answers1

1

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

Ganesh AB
  • 4,652
  • 2
  • 18
  • 29
  • Hello Thanks for your reply. We are supposed to implement the onDraw() in View class. How can use onDraw() in onTouch()? – nirisha chilukuri Apr 05 '15 at 01:10
  • @nirishachilukuri as you see in code there is a method invalidate() inside onTouch block which will call your onDraw() method. In short invalidate means redraw. – Ganesh AB Apr 06 '15 at 05:34