首页 > 程序开发 > 移动开发 > Android >

Android 响应键盘移动图标

2011-10-15

package rw.SurFace;import android.app.Activity;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import andr...

\\

package rw.SurFace;


import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.SurfaceView;

public class Surfaecview01Activity extends Activity {
/** Called when the activity is first created. */
DisplayMetrics sMetrics;
Thread maintThread;
MysurfaceView view;
boolean up,down,right,left;
class MysurfaceView extends SurfaceView implements Runnable{
Bitmap bitmap;

Paint paint=new Paint();
long time;
int Coord_x,Coord_y;
int x,y;
public MysurfaceView(Context context) {
super(context);
// TODO Auto-generated constructor stub

setFocusable(true);//可以聚焦
bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.a);
paint.setColor(Color.BLUE);
paint.setTextSize(20);
x=bitmap.getWidth();
y=bitmap.getHeight()/2;
maintThread=new Thread(this);
maintThread.start();
}

@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
DoDraw();
}
}
void DoDraw()
{
Canvas canvas=getHolder().lockCanvas();
if (canvas!=null) {
canvas.drawColor(Color.WHITE);
if (up) Coord_y--;
if(down) Coord_y++;
if(left) Coord_x--;
if (right) Coord_x++;
canvas.drawBitmap(bitmap, Coord_x*10, Coord_y*10, null);

long now=System.currentTimeMillis();
canvas.drawText(""+1000/(now-time)+"fps", x, y, paint);
time=now;
getHolder().unlockCanvasAndPost(canvas);
}
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sMetrics=new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(sMetrics);
/*
* 获取屏幕大小
*/
int Met_X=sMetrics.widthPixels;
int Met_Y=sMetrics.heightPixels;
Log.i("X--------->", Met_X+"");
Log.i("Y--------->", Met_Y+"");
setContentView(new MysurfaceView(this));
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_UP:
up=true;
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
down=true;
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
left=true;
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
right=true;
break;
default:
break;
}
return super.onKeyDown(keyCode, event);
}

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_UP:
up=false;
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
down=false;
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
left=false;
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
right=false;
break;
default:
break;
}
return super.onKeyUp(keyCode, event);
}

/* (non-Javadoc)
* @see android.app.Activity#onDestroy()
*/
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
Thread.interrupted();
super.onDestroy();
}

}

摘自:伟哥~ YZ

相关文章
最新文章
热点推荐