|
[li]package rw.SurFace;[/li][li][/li][li][/li][li]import android.app.Activity;[/li][li]import android.content.Context;[/li][li]import android.graphics.Bitmap;[/li][li]import android.graphics.BitmapFactory;[/li][li]import android.graphics.Canvas;[/li][li]import android.graphics.Color;[/li][li]import android.graphics.Paint;[/li][li]import android.os.Bundle;[/li][li]import android.util.DisplayMetrics;[/li][li]import android.util.Log;[/li][li]import android.view.KeyEvent;[/li][li]import android.view.SurfaceView;[/li][li][/li][li]public class Surfaecview01Activity extends Activity {[/li][li] /** Called when the activity is first created. */[/li][li] DisplayMetrics sMetrics;[/li][li] Thread maintThread;[/li][li] MysurfaceView view;[/li][li] boolean up,down,right,left;[/li][li] class MysurfaceView extends SurfaceView implements Runnable{[/li][li] Bitmap bitmap;[/li][li][/li][li] Paint paint=new Paint();[/li][li] long time;[/li][li] int Coord_x,Coord_y;[/li][li] int x,y;[/li][li] public MysurfaceView(Context context) {[/li][li] super(context);[/li][li] // TODO Auto-generated constructor stub[/li][li][/li][li] setFocusable(true);//可以聚焦[/li][li] bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.a);[/li][li] paint.setColor(Color.BLUE);[/li][li] paint.setTextSize(20);[/li][li] x=bitmap.getWidth();[/li][li] y=bitmap.getHeight()/2;[/li][li] maintThread=new Thread(this);[/li][li] maintThread.start();[/li][li] }[/li][li][/li][li] @Override[/li][li] public void run() {[/li][li] // TODO Auto-generated method stub[/li][li] while (true) {[/li][li] DoDraw();[/li][li] }[/li][li] }[/li][li] void DoDraw()[/li][li] {[/li][li] Canvas canvas=getHolder().lockCanvas();[/li][li] if (canvas!=null) {[/li][li] canvas.drawColor(Color.WHITE);[/li][li] if (up) Coord_y--;[/li][li] if(down) Coord_y++;[/li][li] if(left) Coord_x--;[/li][li] if (right) Coord_x++;[/li][li] canvas.drawBitmap(bitmap, Coord_x*10, Coord_y*10, null);[/li][li][/li][li] long now=System.currentTimeMillis();[/li][li] canvas.drawText(""+1000/(now-time)+"fps", x, y, paint);[/li][li] time=now;[/li][li] getHolder().unlockCanvasAndPost(canvas);[/li][li] }[/li][li] }[/li][li] }[/li][li] @Override[/li][li] public void onCreate(Bundle savedInstanceState) {[/li][li] super.onCreate(savedInstanceState);[/li][li] sMetrics=new DisplayMetrics();[/li][li] getWindowManager().getDefaultDisplay().getMetrics(sMetrics);[/li][li] /*[/li][li] * 获取屏幕大小[/li][li] */[/li][li] int Met_X=sMetrics.widthPixels;[/li][li] int Met_Y=sMetrics.heightPixels;[/li][li] Log.i("X--------->", Met_X+"");[/li][li] Log.i("Y--------->", Met_Y+"");[/li][li] setContentView(new MysurfaceView(this));[/li][li] }[/li][li][/li][li] @Override[/li][li] public boolean onKeyDown(int keyCode, KeyEvent event) {[/li][li] // TODO Auto-generated method stub[/li][li] switch (keyCode) {[/li][li] case KeyEvent.KEYCODE_DPAD_UP:[/li][li] up=true;[/li][li] break;[/li][li] case KeyEvent.KEYCODE_DPAD_DOWN:[/li][li] down=true;[/li][li] break;[/li][li] case KeyEvent.KEYCODE_DPAD_LEFT:[/li][li] left=true;[/li][li] break;[/li][li] case KeyEvent.KEYCODE_DPAD_RIGHT:[/li][li] right=true;[/li][li] break;[/li][li] default:[/li][li] break;[/li][li] }[/li][li] return super.onKeyDown(keyCode, event);[/li][li] }[/li][li][/li][li] @Override[/li][li] public boolean onKeyUp(int keyCode, KeyEvent event) {[/li][li] // TODO Auto-generated method stub[/li][li] switch (keyCode) {[/li][li] case KeyEvent.KEYCODE_DPAD_UP:[/li][li] up=false;[/li][li] break;[/li][li] case KeyEvent.KEYCODE_DPAD_DOWN:[/li][li] down=false;[/li][li] break;[/li][li] case KeyEvent.KEYCODE_DPAD_LEFT:[/li][li] left=false;[/li][li] break;[/li][li] case KeyEvent.KEYCODE_DPAD_RIGHT:[/li][li] right=false;[/li][li] break;[/li][li] default:[/li][li] break;[/li][li] }[/li][li] return super.onKeyUp(keyCode, event);[/li][li] }[/li][li][/li][li] /* (non-javadoc)[/li][li] * @see android.app.Activity#onDestroy()[/li][li] */[/li][li] @Override[/li][li] protected void onDestroy() {[/li][li] // TODO Auto-generated method stub[/li][li] Thread.interrupted();[/li][li] super.onDestroy();[/li][li] }[/li][li][/li][li]}
[/li]
复制代码
|
|
|
|