2

我有一个活动,它使用surfaceview作为其内容视图来绘制一些位图并执行动画,控制开始新一轮事件以在该surfaceview线程中为下一轮加载一组新位图的最佳方法是什么?是从 SurfaceView 线程切换回主游戏活动的最佳方式。到目前为止,我已经尝试过,下面是绘制所有内容的 surfaceview 类的片段。

              if(isCollided(i)){

            if(imgList.indexOf(imgList.get(i)) == 0){

                    activity.runOnUiThread(new Runnable() {
                        public void run() {
                            Toast.makeText(context, "Correct!", Toast.LENGTH_SHORT).show();
                            Intent i = new Intent(context, AnimeAction.class);
                            context.startActivity(i);
                        }});


            }
4

1 回答 1

2

你不应该这样做:

MainActivity activity = new MainActivity();

而是试试这个:

MainActivity activity = MainActivity.this;


就发布一组新的位图而言,我在我的应用程序中这样做了:

public class OurView extends SurfaceView implements Runnable
    {
        Thread t=null;
        SurfaceHolder holder;
        boolean ok;

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

        }
        public void run()
        {
                  //draw the bitmaps
                } 
            public void pause()
        {
            ok=false;
            Log.v("pause()", "ok=false");
            while(true)
            {
                try{

                    t.join();

                }
                catch(InterruptedException e)
                {
                    Log.v("pause()",e.toString());
                }

                break;
            }
           t=null;

        }
        public void resume()
        {
            ok=true;
            if(t==null)
            {
             t=new Thread(this);
               t.start();
               Log.v("resume()", "new thread started");
            }
            else
            {
                Log.v("resume()", "new thread not started as t!=null");
            }
        }
}

在活动的onResume()onCreate()

@Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        Log.v("onPause()", "super.onPause()");
        v.pause();  //V IS AN OBJECT OF THE CLASS OurView

    }
    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        Log.v("onResume()", "super.onResume()");
        v.resume();
      }

我是从这个视频中学到的(以及随后的 1.28 -1.33) - http://youtube.com/watch?v=Z2YogvILjvo

于 2012-11-19T13:43:40.440 回答