apl*_*vin 39 android drawable android-imageview colorfilter shapedrawable
我有一个ImageView与android:src设置为一个ShapedDrawable,即白圈.我想要的是ImageView在响应某些事件的运行时将其着色.imgView.setColorFilter似乎是解决方案,但在使用此(尝试不同的参数)后,图像变得不可见(我在屏幕上看不到它).
怎么解决这个?还有更好的方法来制作彩色圆圈吗?
MH.*_*MH. 102
好吧,我快速玩了一下,注意到你的圆圈问题消失了.如果没有你描述你究竟尝试了什么,我猜你还没有尝试过设置滤色片Drawable吗?(相对于ImageView,似乎只与BitmapDrawables一起使用).
以下语句对于ShapeDrawable使用white作为初始颜色声明的xml非常合适:
ImageView redCircle = (ImageView) findViewById(R.id.circle_red_imageview);
ImageView greenCircle = (ImageView) findViewById(R.id.circle_green_imageview);
ImageView blueCircle = (ImageView) findViewById(R.id.circle_blue_imageview);
// we can create the color values in different ways:
redCircle.getDrawable().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY );
greenCircle.getDrawable().setColorFilter(0xff00ff00, PorterDuff.Mode.MULTIPLY );
blueCircle.getDrawable().setColorFilter(getResources().getColor(R.color.blue), PorterDuff.Mode.MULTIPLY );
Run Code Online (Sandbox Code Playgroud)
该ShapeDrawable物品是否完整:(我设置的大小ImageView,见下文)
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
<solid android:color="@android:color/white" />
</shape>
Run Code Online (Sandbox Code Playgroud)
其中一个ImageView例子如下:
<ImageView
android:id="@+id/circle_red_imageview"
android:layout_width="40dp"
android:layout_height="40dp"
android:padding="5dp"
android:src="@drawable/circle_white" />
Run Code Online (Sandbox Code Playgroud)
视觉效果:

小智 15
如果要更改图像颜色使用
PorterDuff.Mode.SRC_ATOP instead
PorterDuff.Mode.MULTIPLY
Run Code Online (Sandbox Code Playgroud)
在上面的例子中.
您可以android:tint在xml中使用ImageView中的属性.
例:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/your_drawable"
android:tint="@color/your_color" />
Run Code Online (Sandbox Code Playgroud)
在Android 4.1.2和6.0.1上测试过