如何在画布上添加计时器,表面视图?

时间:2015-03-14 23:12:55

标签: android canvas timer android-studio

我使用画布画一个在屏幕上移动的球,当我在朋友的手机上模拟应用程序时,球的移动速度比在我自己的手机上慢。我想知道如何使用延迟或其他东西来控制我的应用在屏幕上绘制的次数/秒,这是我使用的代码:



public class rodando extends ActionBarActivity implements View.OnTouchListener{

    OurView v;
    int x = 0,y = 0;
    Bitmap Ball;
    
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rodando);
        getSupportActionBar().hide();
        v = new OurView(this);
        v.setOnTouchListener(this);
        ball = BitmapFactory.decodeResource(getResources(),R.drawable.ball);
        setContentView(v);

    }

    protected void onResume()
    {
        super.onResume();
        v.resume();
    }

    protected void onPause()
    {
        super.onPause();
        v.pause();
    }


    public class OurView extends SurfaceView implements Runnable
    {


        Thread t = null;
        SurfaceHolder holder;
        boolean isItOK = false;



        public OurView(Context context) {
            super(context);
            holder = getHolder();
        }

        @Override
        public void run() {


            while(isItOK == true)
            {
                Canvas c = holder.lockCanvas();

                if(!holder.getSurface().isValid())
                {
                    continue;
                }

                c.drawBitmap(ball,x,y,null);
                x++;
                y++;

                holder.unlockCanvasAndPost(c);
	        

            }
        }

        public void pause()
        {
            isItOK = false;
            while(true)
            {
                try{
                    t.join();
                }catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
                break;
            }
            t = null;
        }

        public void resume()
        {
            isItOK = true;
            t = new Thread(this);

            t.start();
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_rodando, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
         int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    @Override
    public boolean onTouch(View v, MotionEvent event) {

        
return true;
        }
        
}




0 个答案:

没有答案