【问题标题】:can one drawable instance be used on multiple imageView?一个drawable实例可以用在多个imageView上吗?
【发布时间】:2017-02-25 19:37:02
【问题描述】:

在列表视图中,对于每个项目,它需要为不同的项目名称显示一些图像(可绘制)。如果名称相同,则显示相同的图像。 drawable 是特定的,并且需要一些时间来制作它(需要远程资源)。 所以想也许缓存可绘制对象,如果它没有被缓存,则只构建新的。 看到一些文章“ANDROID 可绘制实例——不要共享它们!” http://loseyourmarbles.co/2013/09/android-drawable-instances-dont-share/,

这里还需要为drawable设置动画。但是测试似乎没有看到不良行为(可能没有经过足够的测试)?

那么有没有人有过类似的需求并且体验过一个drawable实例是否可以在多个ImageView上使用呢?这种方法是加快列表视图的正确方法吗?

或者一个drawable实例可以用在多个imageView上吗?

HashMap<String, Drawable> mNameToDrawnable = new HashMap<>();

public Drawable getDrawable(Context context, String displayName){

    Drawable cachedD = mNameToDrawnable.get(displayName);
    if (cachedD != null) {
        return cachedD;
    }

    Drawnable d = makeDrawable(displayName);  //new Drawable(context.getResources());, etc ……
    d.setDecodeDimensions(100, 100);

    mNameToDrawnable.put(displayName, d);

    return d;
}

【问题讨论】:

    标签: android caching android-imageview android-drawable


    【解决方案1】:

    如果你想在你的列表视图中使用缓存的drawable并且你担心它的一些问题你可能想在你的drawable上调用mutate()。这可能会让您更好地理解:https://android-developers.googleblog.com/2009/05/drawable-mutations.html?m=1

    【讨论】:

    • 感谢 Piotr!它提到的文章中唯一的区别是列表对于每个列表项都有新的可绘制实例。如果多个项目共享同一个可绘制实例,不确定是否有什么需要担心的。
    • 嗯,我想这真的取决于你的情况。如果您不以任何方式修改可绘制对象,它可能会起作用。我会在各种操作系统版本上检查它,但只是为了确定。
    猜你喜欢
    • 1970-01-01
    • 2011-07-09
    • 2019-09-27
    • 2012-12-13
    • 1970-01-01
    • 2014-08-11
    • 1970-01-01
    • 2014-10-30
    • 1970-01-01
    相关资源
    最近更新 更多