0

全部- 我有一个简单的应用程序,我希望一个人在屏幕上走过。现在,动画就像一本便签翻书一样发生在一个地方。换句话说,框架在一个地方发生变化,就像纺车一样。我的问题是如何让动画前进(以我想要的速度)以及改变帧?这是我关于这个问题的代码:

public void start(View v) {  
    ImageView img = (ImageView)findViewById(R.id.imageView); 
    img.setBackgroundResource(R.drawable.animation); 
    AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();                
    frameAnimation.start();
}

感谢您的时间和精力!

4

1 回答 1

1

你可以做这样的事情,使用你自己的形象(男人的形象):

主类:

package com.android.animation;

import android.app.Activity;
import android.os.Bundle;

public class Main extends Activity 
{

    Animation myView;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        myView = new Animation(this);
        setContentView(myView);
    }
}

动画类:

package com.android.animation;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.View;

public class Animation extends View
{
Bitmap gBall;
float changingY;

public Animation(Context content)
{
    super(content);

    gBall = BitmapFactory.decodeResource(getResources(), R.drawable.ball);
    changingY = 0;
}

@Override
protected void onDraw(Canvas canvas)
{
    super.onDraw(canvas);
    canvas.drawColor(Color.BLACK);
    canvas.drawBitmap(gBall, (canvas.getWidth()/2), changingY, null);
    if(changingY < canvas.getHeight())
        changingY += 10;
    else
        changingY = 0;

    invalidate();
}
}

XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />

</LinearLayout>

事实上,如果你愿意,你可以继续复制粘贴我的代码,看看它是如何工作的(确保将图像放在 drawable-hdpi 文件夹中)......你应该能够将它用作项目的模板. 希望能帮助到你!

P.S You could of course, change the ChangingY variable to ChangingX (for example; of course you would have to change a couple other things like the drawBitmap() method.. not hard though) to make the ball move in a horizontal line... see how it works out for you.

于 2012-08-09T19:56:18.150 回答