Ad

Draw Bitmap Multipletime

I'm trying to draw bitmap multiple time something similar to this :

enter image description here

the code below makes bitmap just moving :

public class TheChainView extends View {
    Bitmap bitmap;
    float x = 200;
    float y = 200;

    public TheChainView(Context context, AttributeSet attrs) {
        super(context, attrs);
        bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.heart);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                x = event.getX();
                y = event.getY();
                invalidate();
                break;
            case MotionEvent.ACTION_MOVE:
                x = event.getX();
                y = event.getY();
                invalidate();
                break;
        }
        return true;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(bitmap, x, y, null);
    }
}

how I can make a bitmap draw a multiple time whenever I touch the screen

Ad

Answer

This may not be a good solution but you can hold the co-coordinates in some data structure like below:

   public class TheChainView extends View {
        Bitmap bitmap;
        List<Point> points = new ArrayList<>();

        public TheChainView(Context context, AttributeSet attrs) {
            super(context, attrs);
            bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.heart);
            points.add(new Point(200, 200));
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                case MotionEvent.ACTION_MOVE:
                    points.add(new Point(event.getX(), event.getY()));
                    invalidate();
                    break;
            }
            return true;
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            for (Point p : points) {
                canvas.drawBitmap(bitmap, p.x, p.y, null);
            }
        }

        static class Point {
            float x, y;
            Point(float x, float y) {
                this.x = x;
                this.y = y;
            }
        }
    }
Ad
source: stackoverflow.com
Ad