开发者

Android: UI locks when menu is hit

开发者 https://www.devze.com 2023-03-07 06:31 出处:网络
I have the following code which works perfectly to continuously update the screen with a bitmap. public class render extends Activity

I have the following code which works perfectly to continuously update the screen with a bitmap.

public class render extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super开发者_如何学Go.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();
    }
}

The problem is I have since added an options menu. When I press the menu key, my app freezes, I'm guessing because the UI thread is blocked. What is the best way to handle this?

Thanks in advance.

Edit: I have tried to use Asynctask without success:

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();
        }
    }
}


You may want to extend SurfaceView instead, to let android manage the drawing in a separate thread.

See the dev guide for a better explanation: http://developer.android.com/guide/topics/graphics/index.html


Your best bet is to use an AsyncTask to do the heavy lifting in the background. Google has a helpful tutorial here.

0

精彩评论

暂无评论...
验证码 换一张
取 消