【问题标题】:HashMap with BitmapDrawable not returning drawable带有BitmapDrawable的HashMap不返回drawable
【发布时间】:2013-09-06 05:34:10
【问题描述】:

我有一个这样的 HashMap

HashMap<String, BitmapDrawable> bitmapDrawables = new HashMap<String, BitmapDrawable>();

现在我有一个方法可以返回可绘制对象:

public static BitmapDrawable getDrawable(String fileName, Context context,Bitmap bitmap) {
    BitmapDrawable drawable = bitmapDrawables.get(fileName);
    if (drawable == null) {
        drawable = convertBitmapToDrawable(bitmap, context);// will convert the bitmap to drawable
        bitmapDrawables.put(fileName, drawable);

    }
    return drawable;
    }

它工作正常,但有时它不会返回正确的可绘制对象,就像我有 2 个活动 A 和 B,在活动 A 中我有一个带有可绘制集的按钮,现在我从 A -> B 移动,然后返回A 当时drawable没有加载,但是当我触摸那个按钮时drawable是可见的。

为什么会发生这种情况?,我已经移除了 haspMap 并正确检查了它的负载,当我从 A->B 移动并返回 A 时,但如果我不使用 Hashmap,屏幕会出现延迟。

我正在以这种方式使用 getDrawable():

button.setBackgroundDrawable(getDrawable(name,context,bitmap);

【问题讨论】:

  • 如何以及在何处使用 getDrawable() 方法?
  • @beworker 请看我的编辑
  • 照常在 onCreate() 中
  • 我怀疑问题不在于这个方法,而在于 convertBitmapToDrawable() 之一。
  • @beworker 这就是我转换新 BitmapDrawable(context.getResources(), bitmap); 的方式

标签: android bitmap drawable android-drawable


【解决方案1】:

你可以使用

button.setBackgroundDrawable(getDrawable(name,context,bitmap)

在方法onResume() 中。顺便说一下,不推荐你的实现,容易造成内存泄漏。详情请见this

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-29
    • 2021-02-15
    • 1970-01-01
    • 1970-01-01
    • 2012-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多