在图层列表中给位图着色

26

不再相关! 此问题与旧版 Android 4.x 中的错误有关。如下示例中所示,android:tint 现在应该可以正确工作。

我正在尝试将色彩加入 <layer-list> 内的位图。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape  xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
            <solid android:color="@color/grey" />
            <size android:width="45dp" android:height="45dp"/>
        </shape>
    </item>
    <item android:left="5dp" android:right="5dp" android:top="5dp" android:bottom="5dp">
        <bitmap android:src="@drawable/ic_action" android:tint="#FF000000"   />
    </item>
</layer-list>

预览显示应该可以工作,来自Android-Studio:enter image description here

但是在设备上部署时没有进行着色:enter image description here

如果我在我的布局中使用ImageView,则可以正确进行着色,但是与layer-list一起失败。 我相信我已经尝试了所有的tintMode,但没有结果。


2
这个有进展了吗?会很好的。 - user291701
1
我将这个提交到了 Google:https://code.google.com/p/android/issues/detail?id=78631&thanks=78631&ts=1415028902 - somerandomusername
1
问题已在主分支中解决,将在未来的发布中生效。 - alanv
我尝试通过仅使用位图作为可绘制资源的根视图来简化它。但问题仍然存在,表明这不是层列表问题,而是位图本身的问题。此外,我想指出setTint()只适用于棒棒糖及更高版本,也许与此有关。在棒棒糖设备上测试应用程序可以帮助排除这个问题。 - reubenjohn
1
我投票关闭此问题,因为OP自己声明它已不再相关,并且是由于Android中的一个错误引起的,而这个错误在此期间已经被修复了。 - Ridcully
显示剩余2条评论
3个回答

17

对于其他可能遇到这个问题的人,以下是我所做的:

设置

  • Android Studio 2.3.2

Gradle

  • minSdkVersion 15
  • targetSdkVersion 25
  • compile 'com.android.support:appcompat-v7:25.3.1'

图层绘制 我在包含位图的项目中添加了android:id=@+id/bitmapID

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape android:shape="oval">
            <solid android:color="#9e9e9e" />
            <size android:width="45dp" android:height="45dp"/>
        </shape>
    </item>
    <item
        android:id="@+id/tintDrawableImg"
        android:left="5dp"
        android:right="5dp"
        android:top="5dp"
        android:bottom="5dp">
        <bitmap android:src="@mipmap/ic_pencil" android:tint="#860000"   />
    </item>
</layer-list>

活动布局 我向 ImageView 添加了图层可绘制项。

<ImageView
    android:id="@+id/tintLayerTest"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:src="@drawable/test_layer"/>

主活动 在 onCreate() 方法中,我们可以使用 findViewByID 方法从层可绘制对象中查找位图。

public class MainActivity extends AppCompatActivity {

    ImageView iv;
    LayerDrawable ld;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Declare ImageView containing LayerDrawable
        iv = (ImageView)findViewById(R.id.tintLayerTest);
        //Get LayerDrawable from ImageView
        ld = (LayerDrawable)iv.getDrawable();
        //Get specific Drawable/Bitmap from within LayerDrawable
        //by ID and pass it as an independent Drawable
        Drawable ldDrawable = ld.findDrawableByLayerId(R.id.tintDrawableImg);
        
        //Pass your new Drawable to DrawableCompat.setTint
        //and define your color int
        DrawableCompat.setTint(ldDrawable, ContextCompat.getColor(this, R.color.colorAccent));

    }
}

我希望这能帮助到其他遇到这个问题的人。


1
这还有用吗?他们(据说)在3年前就修复了这个bug。 - somerandomusername
我最近也遇到了同样的问题,试图做类似的事情。这是解决我的方法。 - Steve C.
请注意,如果在同一页上显示多个具有不同色调的层列表可绘制对象,则此解决方案无法正常工作: Android 可绘制缓存在填充相同资源时使用相同的可绘制实例,因此手动设置其色调会为页面上显示的所有相同可绘制对象设置相同的色调。 - Nit
使用视图上的getDrawable方法起作用了!我之前使用的是resources.getDrawable方法。 - anoo_radha
1
@somerandomusername 仅使用android:tint即可在2019年正常运行,无需任何黑客技巧。 - lucidbrot
显示剩余2条评论

1
正如在评论中提到的那样,@lucidbrot提到android:tint现在应该可以正常工作,无需任何hack。

这个问题在 Android 上出现了近7年。当然,它早已经被解决了很久以前。 - somerandomusername
1
确实如此,但当您从搜索中遇到此问题并看到不再相关的解决方案时,会感到困惑。 - fobo66

1
尝试使用位图标签的xml可绘制对象。请保存以下文件xml custom_image.xml并替换您的图标和色调颜色。
<?xml version="1.0" encoding="utf-8"?>
    <bitmap
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:src="@drawable/iv_veg"
      android:tint="@color/red">
 </bitmap>

这是Android Studio 2中的旧问题。我相信很久以前就已经修复了。 - somerandomusername

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接