我在一个线程结束时向主线程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呢?整个程序中没有使用点击屏幕的响应函数。AndroidUI

解决方案 »

  1.   

    你找一下  有个异步线程 ,可以解决延迟问题 ,AysncTask
      

  2.   

    你的Handler的looper试着不要用主线程的looper用
                mHandlerThread = new HandlerThread("WorkerThread");
               
                mHandlerThread.start();
            }        if (null == mHandler)
                mHandler = new Handler(mHandlerThread.getLooper());
      

  3.   

    如果是一次请求的话,建议使用1楼的方法 AysncTask ,handler确实容易出现混乱,特别是需要大量的异步更新UI的时候
      

  4.   

    aysnctask 或是 intentservice