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();
}
}
}
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能想扩展
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
最好的选择是使用 AsyncTask 来完成繁重的工作背景。 Google 这里有一个有用的教程。
Your best bet is to use an AsyncTask to do the heavy lifting in the background. Google has a helpful tutorial here.