在视图中运行RunUnUiThread [英] runOnUiThread inside a View
问题描述
我正在制作自定义 ImageView
。其中一种方法是从URL加载图像。我想在线程中检索位图并在UI线程中加载位图。
I'm making a custom ImageView
. One of the methods is to load an image from a URL. And I want to retrieve the Bitmap in a Thread and load the bitmap in the UI thread.
如何制作 runOnUIThread()
是否要求绘制位图?
How can I make a runOnUIThread()
call for painting the bitmap?
是否有某种内置函数?或者我应该在构造函数中创建 Handler
并使用它在UI线程中运行runnables?
Is there some kind of built in function? Or should I create a Handler
in the constructor and use it for running runnables in the UI thread?
推荐答案
通过 AsyncTask
下载图片并在其 onPostExecute 方法中设置为您的视图
Download the Image via AsyncTask
and set to your view in its onPostExecute method
或
从单独的图片下载主题中,使用查看帖子方法
总是在 UI-thread 上运行 Runnable
:
From a separate image downloading thread use the post method of View
which will always run its Runnable
on UI-thread:
yourImageView.post(new Runnable() {
@Override
public void run() {
// set the downloaded image here
}
});
这篇关于在视图中运行RunUnUiThread的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!