1

我正在开发我的第一个 Android 项目 - 动态壁纸。我需要我的项目中的一些图像一直在屏幕上飞行。图像应该从下到上飞,但它们需要一直飞,所以背景永远不会是空的。这是我的代码的一部分,但我知道我需要的不止这些。

void draw(Canvas c) {
    c.save();
    c.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.wallpaper1), 0, 0, null);
    double tmp = Math.sin(fiX * Math.PI / 180F) * 20;
    Bitmap bm = BitmapFactory.decodeResource(getBaseContext().getResources(), R.drawable.heart_s);
    c.drawBitmap(bm, 100 + (int) tmp, posY, paint);

    double tmp1 = Math.sin(fiX * Math.PI / 180F) * 20;
    Bitmap bm1 = BitmapFactory.decodeResource(getBaseContext().getResources(), R.drawable.heart1);
    c.drawBitmap(bm1, 300 + (int) tmp1, posY, paint);

    double tmp2 = Math.sin(fiX * Math.PI / 180F) * 20;
    Bitmap bm2 = BitmapFactory.decodeResource(getBaseContext().getResources(), R.drawable.heart3);
    c.drawBitmap(bm2, 200 + (int) tmp2, posY, paint)

    posY = posY -direction;
    fiX = fiX + 10;

    if(posY < 0) {
        //posY = getResources().getDisplayMetrics().heightPixels;
        direction = 5;                  
    }

    if(posY > getResources().getDisplayMetrics().heightPixels) {
        direction = 5;                                      
    }
    if(fiX > 180) {
            fiX = 0;
    }
    c.restore();
}
4

1 回答 1

2

在您的 draw 方法调用结束时使视图无效。这将在你的平局上开始一个循环。

在绘图开始时添加动画逻辑,创建一些 drawModel 并使用此逻辑对其进行更新。然后根据您的模型更改图像的位置。

void draw(Canvas canvas) {
    handleUpdateModelEvent();
    onDraw();
    invalidate();
}

private void handleUpdateModelEvent(){

}

private void onDraw(){
   // in MVC this method should be called from model change event
}
于 2012-10-22T08:29:37.523 回答