直接代码了
--------------------------------------MainActivity-------------------------------------------------
package com.example.animination;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.view.ViewPager.LayoutParams;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
import com.synjones.demo.GameView;
public class MainActivity extends Activity {
public GameView gameview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gameview = new GameView(this,null);
RelativeLayout.LayoutParams ll2 = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
addContentView(gameview, ll2);
View view1 = findViewById(R.id.view01);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
--------------------------------------MainActivity end-------------------------------------------------
--------------------------------------GameView-------------------------------------------------
package com.synjones.demo;
import java.io.IOException;
import java.io.InputStream;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
public class GameView extends View implements Runnable{
public Thread thread;
private Bitmap bitMap;
public GameView(Context context,AttributeSet attrs){
super(context,attrs);
try {
InputStream is = context.getAssets().open("bird03_yingwu.png");
bitMap = BitmapFactory.decodeStream(is);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
startThread();
}
//图片帧数,共五帧
public int index = 0;
private int x = 500;
private int y = 300;
//显示对象
private Rect src = new Rect();
//装载对象
private Rect next = new Rect();
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
Paint paint = new Paint();
//显示的内容
src.left = index*60;
src.right = src.left+60;
src.top = 0;
src.bottom = 57;
//内容显示的区域
next.left = x;
next.right = next.left+60;
next.top = y;
next.bottom = next.top+57;
//画图
canvas.drawBitmap(bitMap, src, next, paint);
//index的变化导致内容显示的变化
index++;
//本图共五帧
index %= 5;
//x的变化导致图片向左移动
x -= 9;
}
public void startThread(){
thread = new Thread(this);
thread.start();
}
public void stopThread(){
thread = null;
thread.suspend();
}
int i = 0;
@Override
public void run() {
// TODO Auto-generated method stub
//停止条件
while (x>-60&&thread!=null){
//界面刷新必须的
this.postInvalidate();
try{
Thread.sleep(200);
System.out.println(i++);
}catch(Exception e){
e.printStackTrace();
}
}
}
}
--------------------------------------GameView end-------------------------------------------------
bird03_yingwu.png
--------------------------------------activity_main.xml-------------------------------------------------
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/view01"
></RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"/>
</LinearLayout>
</RelativeLayout>
--------------------------------------activity_main.xml end-------------------------------------------------