【问题标题】:Android LayerDrawable.setDrawableByLayerId not working on HTC Sensation (and others?)Android LayerDrawable.setDrawableByLayerId 不适用于 HTC Sensation(和其他?)
【发布时间】:2013-02-28 00:29:02
【问题描述】:

我正在使用 LayerDrawable 在底层图像(索引 0)上构建一系列热点(索引 1+)。热点是基于用户界面交互添加的,它们的位置是动态的,所以我以编程方式完成所有这些工作,而不是使用 XML。作为进一步的(可能不相关的)细节,我使用 Fragment 来保存 LayerDrawable,因为这是在 FragmentStatePagerAdapter 和 ViewPager 的上下文中。

问题是当我尝试通过调用setDrawableByLayerId更改LayerDrawable中的Drawable来更新图像时,图像没有改变(尽管方法返回true,表明它找到了图层)。

我怎样才能让它工作?

【问题讨论】:

    标签: android drawable


    【解决方案1】:

    在这里回答我自己的问题,希望它对那里的人有用。我可以让它在我的 HTC Sensation 上工作的唯一方法是从头开始重新创建 LayerDrawable,加上更改:

    LayerDrawable oldLayerDrawable = (LayerDrawable) imageView.getDrawable();
    Drawable[] layers = new Drawable[2];
    layers[0] = oldLayerDrawable.getDrawable(0);
    layers[1] = getResources().getDrawable(R.drawable.hotspot_dot);
    LayerDrawable layerDrawable = new LayerDrawable(layers);
    imageView.setImageDrawable(layerDrawable);
    

    这不是很漂亮,但似乎这可能是一个已知问题。

    【讨论】:

      【解决方案2】:
      newdrawable.setBounds(mLayerDrawable.findDrawableByLayerId(R.id.image_source).getBounds());
              mLayerDrawable.setDrawableByLayerId(R.id.image_source, drawable);        
              mLayerDrawable.invalidateSelf();
      

      参考: https://github.com/ytRino/LayerDrawableSample/blob/master/src/net/nessness/android/sample/layerdrawable/LayerDrawableSampleActivity.java

      【讨论】:

        【解决方案3】:

        问题是“index”和“id”是不同的属性。 默认情况下,任何层的 layerId 始终等于 -1。但是可以通过调用setId(index, id)显式设置,然后调用setDrawableByLayerId(id, Drawable)

        【讨论】:

          猜你喜欢
          • 2011-11-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多