原创作品,允许转载,转载时请务必以超链接形式标明文章
原始出处 、作者信息和本声明。否则将追究法律责任。
http://mahaile.blog.51cto.com/2891586/822737
package yxqz.com;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
/**
* android view简单的图片绘制 该类需要继承view 基类
* @author mahaile
*
*/
public class GameView extends View {
Bitmap bitmap_role;
float width,height;
float x,y; //图片渲染所在的屏幕的位置
int direction=0; //图片运行方向 控制图片向上 或向下运动
/**
* 构造方法
* @param context
* @param width 屏幕宽度
* @param height 屏幕高度
*/
public GameView(Context context,float width,float height) {
super(context);
this.width=width;
this.height=height;
//通过BitmapFactory.decodeResource 方法可以自动加载 res/drawable目录下的图片 R.drawable.role参数为android自动生成的图片id
bitmap_role=BitmapFactory.decodeResource(context.getResources(), R.drawable.role);
x=width/2-bitmap_role.getWidth()/2;
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
Log.d("GameView","GameView onDraw x is:"+x +"y is:"+y); //Log类方法用于日志输出
canvas.drawBitmap(bitmap_role, x, y, null);
super.onDraw(canvas);
}
}
package yxqz.com;
import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
GameView gameView;
GameSurfaceView gameSurfaceView;
boolean flag; //线程标示位 当为false时停止刷新界面
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//通过displayMetrics获取屏幕的宽高
DisplayMetrics dm = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
gameView=new GameView(this,width,height);
setContentView(gameView);
flag=true;
new Thread(new GameThread()).start();
}
class GameThread implements Runnable{
public void run() {
while(flag){
if(gameView.direction==0){
gameView.y+=0.2;
if(gameView.y>=gameView.height){
gameView.direction=1;
}
}else{
gameView.y-=0.2;
if(gameView.y<=0){
gameView.direction=0;
}
}
//通知UI线程重绘, gameView的主线程会自动调用onDraw的方法 这点要注意了 喔
gameView.postInvalidate();
//如果使用 gameView.invalidate(); 方法的话 需要需要把gameView.invalidate()写到 handler 里面 因为它只支持ui主线程中使用 }
}
}
}
}
package yxqz.com;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.SurfaceHolder.Callback;
/**
* android surfaceview简单的图片绘制
* @author mahaile
*
*/
public class GameSurfaceView extends SurfaceView implements Callback {
boolean flag; //线程标示位 当为false时停止刷新界面
SurfaceHolder surfaceHolder;
GameViewThread gameViewThread;
int x,y;
int direction=0; //图片运行方向 控制图片向上 或向下运动
int width,height;
Bitmap bitmap_role;
public GameSurfaceView(Context context) {
super(context);
surfaceHolder=this.getHolder();
surfaceHolder.addCallback(this); //添加回调
bitmap_role=BitmapFactory.decodeResource(getResources(), R.drawable.role);
}
public void onDraw(Canvas canvas){
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(bitmap_role, width/2-bitmap_role.getWidth()/2, y, null);
}
public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int width, int height) {
}
public void surfaceCreated(SurfaceHolder surfaceHolder) {
//获取屏幕的 宽高 只有在 surface创建的时候 才有效 ,才构造方法中获取 宽高是获取不到的
width=this.getWidth();
height=this.getHeight();
//初始化绘图线程
gameViewThread=new GameViewThread();
gameViewThread.flag=true;
gameViewThread.start();
}
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
gameViewThread.flag=false; //销毁线程
}
class GameViewThread extends Thread{
public boolean flag;
public void run(){
while(flag){
Canvas canvas=null;
try{
if(direction==0){
if(y>=height){
direction=1;
}
y+=1;
}else{
if(y<=0){
direction=0;
}
y-=1;
}
canvas=surfaceHolder.lockCanvas(); //锁定画布 并获取canvas
onDraw(canvas);//调用onDraw 渲染到屏幕
surfaceHolder.unlockCanvasAndPost(canvas); //此步不要忘记了喔 否则界面上显示不出来的
}catch(Exception e){
e.printStackTrace();
}
try {
Thread.sleep(10);//线程休眠时间 单位是毫秒 控制帧数
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //每一秒刷新一次
}
}
}
}
下面看看 mainActicity代码 程序的入口 这个就 相当简单了
package yxqz.com;
import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
GameSurfaceView gameSurfaceView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gameSurfaceView=new GameSurfaceView(this);
setContentView(gameSurfaceView);
}
}