如何在android中的动态壁纸中添加动画?

时间:2014-12-31 09:31:16

标签: android

我想在动态壁纸中添加带动画的触摸效果:

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);`

1 个答案:

答案 0 :(得分:1)

您可以按时间转换画布,即:

 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);
    }

}