gpt4 book ai didi

android - 一个drawable实例可以用在多个imageView上吗?

转载 作者:行者123 更新时间:2023-11-30 00:41:47 25 4
gpt4 key购买 nike

在 ListView 中,对于每个项目,它需要为不同的项目名称显示一些图像(可绘制)。如果名称相同,则它们显示相同的图像。可绘制对象是特定的,制作它需要一些时间(需要远程资源)。所以认为可能会缓存可绘制对象,并且只有在未被缓存的情况下才构建新的可绘制对象。看到一些文章“Android DRAWABLE INSTANCES – DON'T SHARE THEM!” http://loseyourmarbles.co/2013/09/android-drawable-instances-dont-share/ ,

这里还需要为可绘制对象设置动画。但是测试似乎没有看到不良行为(可能测试不够)?

所以有没有人有过类似的需求,体验过一个drawable实例是否可以在多个ImageView上使用?这种方法是加速 ListView 的正确方法吗?

或者一个 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;
}

最佳答案

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

关于android - 一个drawable实例可以用在多个imageView上吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42460718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com