Mehmed
Mehmed

Reputation: 3040

How to make an Android app that prints touched points and draw them?

I want to make a small app. You will touch the screen and draw something and it will list points you pass and draw small green 3x3 rectangles for each fifth point. I use onTouchEvent for listing points using TextView and send it to setContentView. However, I have problem in drawing. I checked examples for drawing (onDraw) but I am not able to get it working for both printing point plus drawing green dots. Any help would be great, thanks.

Upvotes: 0

Views: 2673

Answers (2)

Pete Houston
Pete Houston

Reputation: 15079

Here you are, a quick sample of drawing on SurfaceView.

public class FunPanel extends SurfaceView {

    class Point {
        int X;
        int Y;

        public Point() {
            X = Y = -1;
        }

    }

    private ArrayList<Point> mPoints = new ArrayList<Point>();
    private Point mCurPoint = new Point();
    private Bitmap mBitmap = ....// your desired image


    @Override
    public void doDraw(Canvas canvas) {
        if( !(mPoints.size() % 5) ) {
            canvas.drawBitmap(mBitmap, mCurPoint.X, mCurPoint.Y, null);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mCurPoint.X = (int) event.getX() - mBitmap.getWidth() / 2;
        mCurPoint.Y = (int) event.getY() - mBitmap.getHeight() / 2;

        mPoints.add(mCurPoint);
        return super.onTouchEvent(event);
    }

}

Upvotes: 1

dbryson
dbryson

Reputation: 6127

It's not entirely clear what you're trying to do, but have a look at this It should get you started in the right direction. Basically extend a View and override the onDraw(Canvas) to draw the Rectangles and override the onTouchEvent(MotionEvent) to grab the touch points from the screen.

Upvotes: 0

Related Questions