如何做android图像动画

时间:2010-04-23 04:53:19

标签: android android-animation

我想尝试一个简单的图像动画。我想让它看起来好像直升机的螺旋桨叶片正在转动。我有三张直升机的图像,我根据动画进度显示其中一个图像。问题是所有三个图像最终彼此重叠,而不是一次只显示一个图像,从而创建动画。这就是我到目前为止所做的,我甚至尝试通过执行canvas.drawColor(Color.BLACK)来清除画布,但这将清除整个画布,这不是我想要的。

这就是我所拥有的:

1)在View类中:

静态类直升机{         private long mLastUpdate;         private long mProgress = 0;         私人最终浮动mX;         私人最终浮动mY;

    private final Bitmap mHelicopter1;
    private final Bitmap mHelicopter2;
    private final Bitmap mHelicopter3;
    private final float mRadius;

    Helicopter(long mLastUpdate, float mX, float mY,
              Bitmap helicopter1, Bitmap helicopter2, Bitmap helicopter3) {
        this.mLastUpdate = mLastUpdate;
        this.mX = mX;
        this.mY = mY;
        this.mHelicopter1 = helicopter1;
        this.mHelicopter2 = helicopter2;
        this.mHelicopter3 = helicopter3;
        mRadius = ((float) mHelicopter1.getWidth()) / 2f;

    }

    public void update(long now) {
        mProgress += (now - mLastUpdate);

        if(mProgress >= 400L)
        {
         mProgress = 0;
        }
        mLastUpdate = now;
    }

    public void setNow(long now) {
        mLastUpdate = now;
    }

    public void draw(Canvas canvas, Paint paint) 
    {       

        if (mProgress < 150L) 
        {
            canvas.drawBitmap(mHelicopter1, mX - mRadius, mY - mRadius, paint);
        } 
        else if (mProgress < 300L) 
        {
            canvas.drawBitmap(mHelicopter2, mX - mRadius, mY - mRadius, paint);

        } 
        else if(mProgress < 400L)
        {
         canvas.drawBitmap(mHelicopter3, mX - mRadius, mY - mRadius, paint);
        }

    }

    public boolean done() {
        return mProgress > 700L;
    }
}

private ArrayList<Helicopter> mHelicopters = new ArrayList<Helicopter>();

2)这是在线程的run()中调用的:

private void doDraw(Canvas canvas) 
    {
     final long now = SystemClock.elapsedRealtime();

        canvas.save();

        for (int i = 0; i < mHelicopters.size(); i++) {
            final Helicopter explosion = mHelicopters.get(i);
            explosion.update(now);
        }

        for (int i = 0; i < mHelicopters.size(); i++) {
            final Helicopter explosion = mHelicopters.get(i);
            explosion.draw(canvas, mPaint);
        }

        canvas.restore();
    }
有人能帮帮我吗?我在网上看了很多关于动画的例子,它们似乎总是涉及文本,而不是图像。感谢。

3 个答案:

答案 0 :(得分:1)

通常绘制画布的线程会重绘整个画布和所有道具/演员。这样,如果您增加直升机刀片图像索引,它将在重绘其余画布后绘制到位。类似于:Background-&gt; Helicopter-&gt; Blade1 next iteration Background-&gt; Helicopter-&gt; Blade2。

答案 1 :(得分:1)

答案 2 :(得分:1)