0

I want to add a touch effect with animation in live wallpaper :

Bitmap img = BitmapFactory.decodeResource(getResources(),R.drawable.heart);
float x = event.getX();
float y = event.getY();
Canvas mCanvas = null;
mCanvas.drawBitmap(img, x, y, null);`
gsanskar
  • 665
  • 5
  • 10
Avinash
  • 70
  • 6
  • 1
    try this link it work fine to me.. http://stackoverflow.com/questions/5743328/image-in-canvas-with-touch-events –  Dec 31 '14 at 09:38

1 Answers1

1

You can transform your canvas by time, i.e:

 class MyView extends View {

    int framesPerSecond = 60;
    long animationDuration = 10000; // 10 seconds

    Matrix matrix = new Matrix(); // transformation matrix

    Path path = new Path();       // your path
    Paint paint = new Paint();    // your paint

    long startTime;

    public MyView(Context context) {
        super(context);

        // start the animation:
        this.startTime = System.currentTimeMillis();
        this.postInvalidate(); 
    }

    @Override
    protected void onDraw(Canvas canvas) {

        long elapsedTime = System.currentTimeMillis() - startTime;

        matrix.postRotate(30 * elapsedTime/1000);        // rotate 30° every second
        matrix.postTranslate(100 * elapsedTime/1000, 0); // move 100 pixels to the right
        // other transformations...

        canvas.concat(matrix);        // call this before drawing on the canvas!!

        canvas.drawPath(path, paint); // draw on canvas

        if(elapsedTime < animationDuration)
            this.postInvalidateDelayed( 1000 / framesPerSecond);
    }

}
Ahmad Arslan
  • 4,998
  • 8
  • 40
  • 60