Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
你能安全地打电话吗;getDrawable";或;“解码资源”;Android中的UI线程_Android - Fatal编程技术网

你能安全地打电话吗;getDrawable";或;“解码资源”;Android中的UI线程

你能安全地打电话吗;getDrawable";或;“解码资源”;Android中的UI线程,android,Android,打电话是坏习惯吗 getResources().getDrawable()或BitmapFactory.decodeResource() 从Android上的UI线程?我只是想知道,因为我有这样的情况,我想在我的视图中显示一个分层图像,而我执行一个背景操作,但我仍然调用上述任何一种方法。。。我看到的几乎每个项目似乎都会在任何线程上执行这两种方法中的任何一种,但我只是在寻找最佳实践。如果您在UI线程上进行任何GUI更新,这将是一种很好的实践。在任何线程中,你都可以运行你的UI线程,在那里你可以做U

打电话是坏习惯吗

getResources().getDrawable()
BitmapFactory.decodeResource()


从Android上的UI线程?我只是想知道,因为我有这样的情况,我想在我的视图中显示一个分层图像,而我执行一个背景操作,但我仍然调用上述任何一种方法。。。我看到的几乎每个项目似乎都会在任何线程上执行这两种方法中的任何一种,但我只是在寻找最佳实践。

如果您在UI线程上进行任何GUI更新,这将是一种很好的实践。在任何线程中,你都可以运行你的UI线程,在那里你可以做UI工作,或者你也可以使用MessageHandler,它也可以为你做同样的事情

Runnable run_in_ui = new Runnable() {
        @Override
        public void run() {
            // do your UI stuffs here
        }
    };
    runOnUiThread(run_in_ui);

我问这个问题已经快三年了,我想简短的回答是“用毕加索或滑翔吧”。我认为长的答案是“视情况而定”。这取决于图像/可绘制图像的大小。对于
BitmapFactory#decodeResource
,您应该尽一切可能远离主线程。如果我试图显示占位符图像,最佳做法是将其打包到应用程序中,尺寸合理,符合您的设计。

即使Vijay Sharma建议使用毕加索,我还是建议您使用Facebook上的frescolib。它执行所有可能的优化,从硬件缓存开始,到使用流水线概念修改Http请求

与其依靠自己,不如依靠Facebook团队的工作:

build.gradle:

 dependencies {
   // your app's other dependencies
   compile 'com.facebook.fresco:fresco:0.7.0+'
 }
view.xml:

<com.facebook.drawee.view.SimpleDraweeView
    android:id="@+id/my_image_view"
    android:layout_width="20dp"
    android:layout_height="wrap_content"
    fresco:viewAspectRatio="1.33"
    <!-- other attributes -->

就这么简单

是的,我知道在UI线程上更新UI组件是最好的做法。我更想知道getDrawable()和decodeResource()在UI线程上是否被视为“I/O”?或者,它们是否应该总是在工作线程上完成?是的,壁画也是。我只是想告诉大家,我认为使用图书馆和自己动手是最好的选择。
 mSimpleDraweeView.setImageURI(uri);