当前位置:操作系统 > 安卓/Android >>

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.Su易做图ceView; 
 
public class Su易做图ecview01Activity extends Activity { 
    /** Called when the activity is first created. */ 
    DisplayMetrics sMetrics; 
     Thread maintThread; 
     Mysu易做图ceView view; 
     boolean up,down,right,left; 
    class Mysu易做图ceView extends Su易做图ceView implements Runnable{ 
        Bitmap bitmap; 
        
        Paint paint=new Paint(); 
        long time; 
        int Coord_x,Coord_y; 
        int x,y; 
        public Mysu易做图ceView(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 Mysu易做图ceView(this)); 
    } 
     
    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
        // TODO Auto-generated method stub 
        switch (keyCode) { 
        case KeyEvent.KEYCODE_DPAD_UP: 
           

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,