为什么Android刷新图片有延迟

RuifDu 2013-06-30 01:51:58
我在一个线程结束时向主线程Handler发送了消息申请更新界面,代码如下:

	
private void UpdateUI(Mat Mat2Show)
{
double time = (endTime-startTime)/Core.getTickFrequency();
Message msg = new Message();
Bundle bundle = new Bundle();
bundle.putDouble("Time", time);
bundle.putDouble("x", x);
bundle.putDouble("y", y);
bundle.putDouble("z", z);
msg.setData(bundle);
msg.obj = Mat2Show;
msg.what = Unity.MessageID.UPDATE_UI;
Log.i(Unity.TAG, "SteroVision SendMessage");
handler.sendMessage(msg);
}

其中handler是在线程中通过

Handler handler = new Handler(Looper.getMainLooper());

获得的。
主线程中的handleMessage函数中关于此消息的处理部分为:
			
case Unity.MessageID.UPDATE_UI:
//Update UI
msgBundle = msg.getData();
showMat = ((Mat)msg.obj).clone();
//post(new UIRunnable());
double time = msgBundle.getDouble("Time");
textView.setText("Computing Time is "+time*1000+"ms");
coordinateTextView.setText("x="+msgBundle.getDouble("x")*100+"cm, y="+msgBundle.getDouble("y")*100+"cm, z="+msgBundle.getDouble("z")*100+"cm");
showImage(showMat, imageView);
//textView.invalidate();
//coordinateTextView.invalidate();
//imageView.invalidate();
Log.i(Unity.TAG, "is UpdateUIing");
break;

showImage函数为:

private void showImage(Mat matImage, ImageView imageView)
{
Bitmap bitmap = Bitmap.createBitmap(matImage.width(), matImage.height(), Config.RGB_565);
Utils.matToBitmap(matImage, bitmap);
imageView.setImageBitmap(bitmap);
}

请问为什么不管是imageView还是textView都不能在获得这个消息后立即更新UI而必须要等很久,或者用手点击屏幕之后才更新UI呢?整个程序中没有使用点击屏幕的响应函数。
...全文
243 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
RuifDu 2013-07-12
  • 打赏
  • 举报
回复
感谢楼上各位,给我提供了一种新的思路~
RuifDu 2013-07-01
  • 打赏
  • 举报
回复
引用 2 楼 guoyoulei520 的回复:
你的Handler的looper试着不要用主线程的looper 用
            mHandlerThread = new HandlerThread("WorkerThread");
           
            mHandlerThread.start();
        }

        if (null == mHandler)
            mHandler = new Handler(mHandlerThread.getLooper());
但是更新UI不应该在主线程里面更新吗?
路人贾 2013-07-01
  • 打赏
  • 举报
回复
aysnctask 或是 intentservice
reubenagain 2013-07-01
  • 打赏
  • 举报
回复
如果是一次请求的话,建议使用1楼的方法 AysncTask ,handler确实容易出现混乱,特别是需要大量的异步更新UI的时候
凉凉二点凉 2013-06-30
  • 打赏
  • 举报
回复
你的Handler的looper试着不要用主线程的looper 用
            mHandlerThread = new HandlerThread("WorkerThread");
           
            mHandlerThread.start();
        }

        if (null == mHandler)
            mHandler = new Handler(mHandlerThread.getLooper());
tony4geek 2013-06-30
  • 打赏
  • 举报
回复
你找一下 有个异步线程 ,可以解决延迟问题 ,AysncTask

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧