全部 - 我有一个简单的应用程序,我希望一个人走过屏幕。现在动画正在一个地方发生,就像粘滞便笺翻书一样。换句话说,框架在一个地方就像旋转轮一样在变化。我的问题是如何让动画向前推进(以我想要的速度)以及更改帧?以下是关于此问题的代码:
public void start(View v) {
ImageView img = (ImageView)findViewById(R.id.imageView);
img.setBackgroundResource(R.drawable.animation);
AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();
frameAnimation.start();
}
感谢您的时间和精力!
答案 0 :(得分: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文件夹中)...你应该能够将它用作模板为您的项目。希望它有所帮助!
PS当然,您可以将ChangingY
变量更改为ChangingX
(例如;当然您必须更改其他一些内容,例如drawBitmap()
方法..不难虽然)让球在水平线上移动......看看它是如何为你工作的。