Android:点击菜单时 UI 锁定

发布于 2024-11-08 11:56:44 字数 2378 浏览 0 评论 0原文

我有以下代码,可以完美地使用位图持续更新屏幕。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忘你却要生生世世 2024-11-15 11:56:44

您可能想扩展 SurfaceView ,让 android 在单独的线程中管理绘图。

请参阅开发指南以获得更好的解释: http://developer.android.com/指南/主题/图形/index.html

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

零時差 2024-11-15 11:56:44

最好的选择是使用 AsyncTask 来完成繁重的工作背景。 Google 这里有一个有用的教程

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文