对于可能遇到此问题的其他任何人。这就是我所做的。
设置
摇篮
- minSdkVersion 15
- targetSdkVersion 25
- 编译'com.android.support:appcompat-v7:25.3.1'
图层 Drawable
我添加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"/>
MainActivity
在 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));
}
}
我希望这可以帮助遇到这个问题的其他人。