从设置到资源可绘制图像的差分? [英] Difference of setting an Image from Resource to Drawables?

查看:112
本文介绍了从设置到资源可绘制图像的差分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个例子是setImageResource与setImageDrawable
同样适用于一个背景?

Example of this is for setImageResource vs. setImageDrawable same goes for a Background?

什么是preferred选择呢?

What is the preferred choice then?

推荐答案

按照<一个href=\"https://developer.android.com/reference/android/widget/ImageView.html#setImageResource%28int%29\">documentation setImageResource 做UI线程,这可能导致延迟打嗝位图读取和解码。所以,你的问题会更好,如果你问在什么情况下 setImageResource 超过 setImageDrawable 更好的选择。

According to the documentation setImageResource does Bitmap reading and decoding on the UI thread, which can cause a latency hiccup. So your question would be better if you'd ask in which cases setImageResource is a better choice above setImageDrawable.

我的猜测是,该 setImageResource 用于延迟加载图像和 setImageDrawable 使用时,你已经视图启动时通过加载可绘制图像。哪一个正适合你最好的可能取决于你是否有比较严重的内存限制与否;在我的经验, setImageResource 几乎没有曾经需要。

My guess would be that the setImageResource is used for lazy loading images, and setImageDrawable is used when you already loaded the image through a Drawable during view initiation. Which one is suiting you best probably depends on whether you have more serious memory constraints or not; in my experience, setImageResource is scarcely ever needed.

和回答为什么你的code不工作;这是因为你不能做 getDrawable()从RESOURCEID 0,<一个href=\"https://developer.android.com/reference/android/content/res/Resources.html#getDrawable%28int%29\">this抛出,因为它不能找到资源的一个例外。由于 setImageResource 是懒加载,<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.0.3_r1/android/widget/ImageView.java#ImageView.setImageResource%28int%29\">it检查如果资源是创建绘制对象之前0 ,因此抛出也不例外。
你也许可以重构它到一个如果(选中)setImageDrawable(..)其他setImageDrawable(NULL);

And to answer why your code doesn't work; this is because you can't do getDrawable() from a resourceId 0, this throws an exception as it can't find the resource. Since setImageResource is lazy loading, it checks if the resource is 0 before creating the Drawable, and hence throws no exception. You could probably refactor it to an if(checked) setImageDrawable(..) else setImageDrawable(null);

这篇关于从设置到资源可绘制图像的差分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆