2011-05-19 42 views
0

我有下面的代码,它完美地用一个位图持续更新屏幕。Android:当菜单被击中时UI锁定

public class render extends Activity 
{ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(new myView(this)); 
    } 
    static { 
     System.loadLibrary("render"); 
    } 
} 
class myView extends View 
{ 
    private int[] mColors; 
    private Bitmap mBitmap; 
    private static native int[] renderBitmap(); 

    public myView(Context context) 
    { 
     super(context); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) 
    { 
     mColors = renderBitmap(); 

     mBitmap = Bitmap.createBitmap(mColors, 64, 64, Bitmap.Config.ARGB_8888);  
     mBitmap = Bitmap.createScaledBitmap(mBitmap, 256, 256, false); 
     canvas.drawBitmap(mBitmap, 8, 8, null); 

     // force a redraw 
     invalidate(); 
    } 
} 

的问题是因为我已经添加了一个选项菜单。当我按菜单键,我的应用程序冻结,我猜,因为UI线程被阻止。处理这个问题的最好方法是什么?

在此先感谢。

编辑:我曾尝试使用的AsyncTask没有成功:

public class render extends Activity 
{ 
    public Bitmap mBitmap; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(new myView(this)); 
    } 

    private static native int[] renderBitmap(); 

    static 
    { 
     System.loadLibrary("render"); 
    } 

    private class renderTask extends AsyncTask<Void, Void, Bitmap> 
    {  
     @Override 
     protected Bitmap doInBackground(Void... params) 
     { 
      int[] mColors; 

      mColors = renderBitmap(); 
      mBitmap = Bitmap.createBitmap(mColors, 64, 64, Bitmap.Config.ARGB_8888);  
      mBitmap = Bitmap.createScaledBitmap(mBitmap, 256, 256, false); 

      return mBitmap; 
     } 

     @Override 
     protected void onPostExecute(Bitmap result) 
     { 
      mBitmap = result; 
     } 
    } 

    class myView extends View 
    { 
     public myView(Context context) 
     { 
      super(context); 
     } 

     @Override 
     protected void onDraw(Canvas canvas) 
     { 
      new renderTask().execute(); 

      canvas.drawBitmap(mBitmap, 112, 8, null); 

      // force a redraw 
      //invalidate(); 
     } 
    } 
} 
+0

那么你必须告诉我们你在做什么,当你创建/显示上述 – Falmarri 2011-05-19 18:28:46

回答