Surface View画布。按下@Override键

时间:2012-05-01 16:26:44

标签: android android-layout canvas surfaceview onkeydown

onKeyDown方法不起作用。当我按下按钮时,位图s不会移动。 当通过放j = j + 5直接完成;在画布里面它起作用。 什么时候直接在onkeydown j + 5上,然后它也没动。

public class SmileyView extends SurfaceView implements SurfaceHolder.Callback {
        public SmileyView(Context context, AttributeSet attrs) {
            super(context, attrs);
            // TODO Auto-generated constructor stub
            holder = getHolder();
             s= BitmapFactory.decodeResource(context.getResources(),R.drawable.smiley);
             holder.addCallback(this);
             sd=context.getResources().getDrawable(R.drawable.lander_plain);
        }

        @Override
    public boolean onKeyDown(int keyCode, KeyEvent msg) {

        return rv.dokey();
        }
    private int i=100;private int j=0;
        private Bitmap s;private SurfaceHolder holder;private RenderView rv ;
         boolean grun=true;long t=0;private boolean key=false;private Drawable sd;volatile private int f=0;
        public void resume()
        {
            rv=new RenderView();
        }


        @Override
        public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void surfaceCreated(SurfaceHolder arg0) {
            // TODO Auto-generated method stub
            grun=true;
            if(rv!=null)
            rv.start();
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder arg0) {
            // TODO Auto-generated method stub
        try {
            grun =false;
            rv.join();
            rv=null;
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }


    class RenderView extends Thread  {

        RenderView(){
        super();

        }
        @Override
        public void run()
        {
            SurfaceHolder h=holder;
            while(grun)
            {
                Canvas ca=null;
                try{ca = h.lockCanvas(null);
                   synchronized (h) {

                dodraw(ca);
                   }
                }
                finally
                {
                    if(ca!=null)
                    h.unlockCanvasAndPost(ca);
                }
            }
        }
        public void dodraw(Canvas canvas)

        {   canvas.drawColor(Color.BLACK);
            canvas.drawBitmap(s, j, j, null);

            sd.setBounds(10, 10, 230, 180);
            sd.draw(canvas);
            key =false;

            canvas.restore();
        }

         boolean dokey()
        {
            j=j+5;
            return true;
        }
    }

    }

2 个答案:

答案 0 :(得分:1)

视图必须具有焦点才能接受关键事件,除了强制关注元素外,还没有好办法让你的SmileyView成为焦点。相反,覆盖活动中的onKeyDown以传递键事件,如果此surfaceview是屏幕上的唯一内容:

public class MyActivity extends Activity {
  private SmileyView mSmileyView

  @Override
  protected boolean onKeyDown(int keyCode, KeyEvent event) {
    return mSmileyView.onKeyDown(keyCode, event);
  }
}

答案 1 :(得分:0)

你试过调试吗? j应该是同步的。您可以从一个线程编辑它并在渲染线程中使用