【问题标题】:How to exchange an image in a layer-list by a bitmap?如何通过位图交换图层列表中的图像?
【发布时间】:2010-10-05 15:07:47
【问题描述】:

这是我的位图

Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(url).getContent());
//Need some code to access "dynamicItem" and exchange it with my Bitmap

这是我的图层列表(没什么特别的)。我想用我的位图交换 dynamicItem。

<?xml version="1.0" encoding="utf-8"?>
    <layer-list
      xmlns:android="http://schemas.android.com/apk/res/android">
      <item
        android:id="@+id/dynamicItem"
        android:drawable="@drawable/defaultItem" />
      <item>
        <bitmap

          android:src="@drawable/some_stuff_on_top"
          android:gravity="top|left" />
      </item>
    </layer-list>

【问题讨论】:

    标签: android android-widget android-layout layer


    【解决方案1】:

    LayerDrawable 中的 setDrawableByLayerId 方法采用 (int, Drawable) 而不是 (int, Bitmap),所以首先从您的位图创建一个可绘制对象

    Drawable drawable = new BitmapDrawable(bitmap);
    layerDrawable.setDrawableByLayerId(R.id.dynamicItem, drawable);
    

    【讨论】:

      【解决方案2】:

      为避免在移动图像时拉伸您必须指定重力。就我而言,它有助于上述解决方案:

      android:gravity="top|left" 
      

      或以编程方式:

       private void shiftLayer(LayerDrawable pieceDrawable,int level){
      
              int l=level * shiftSize;
              int r=0;
              int t=0;
              int b=0;
              pieceDrawable.setLayerInset(level, l, t, r,  b);
              ((BitmapDrawable)pieceDrawable.getDrawable(level)).setGravity(Gravity.LEFT|Gravity.TOP);
          }
      

      在屏幕上您可能会看到独立图像在左侧被拉伸和模糊

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-05-11
        • 2012-02-01
        • 2014-12-18
        • 2012-01-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多