你可以做这样的事情,使用你自己的形象(男人的形象):
主类:
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.