首页 文章

从surfaceview切换到主要活动类

提问于
浏览
2

我有一个活动,它使用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);
                        }});


            }

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() 和_1356363中:

    @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

相关问题