伟哥~ YZ

大学剩下的日子 要在公司度过了,马上要步入大四啦!!!写写博客和大家分享编程经验。QQ 812200157。...

Android 响应键盘移动图标

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();
	}
   
}


 

阅读更多
个人分类: Android 小知识
想对作者说点什么? 我来说一句

键盘控制图标移动

2014年07月30日 47KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭