改变图片中某个部分的颜色

时间:2018-03-27 20:31:08

标签: android android-layout android-studio android-fragments

我正在做这种类型的项目,在我的项目中,我想改变我的图片的一部分颜色,例如当我点击一个按钮我希望蓝色变成绿色时,我想知道是否有有任何方法可以做到这一点或它是不可能的 https://i.stack.imgur.com/yPhur.png

1 个答案:

答案 0 :(得分:0)

要实现上述功能,您可以定义一个形状并将其放在按钮上方,然后更改onClick上的颜色。一个简单的例子是:

shape.xml

num.imag

现在使用以下代码更改形状的颜色。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners
    android:topLeftRadius="4dp"
    android:topRightRadius="4dp"
    android:bottomRightRadius="4dp"
    android:bottomLeftRadius="4dp">
</corners>
<solid
android:color="#4848ea">
</solid>
</shape>

将上述代码放在按钮GradientDrawable Shape = (GradientDrawable)shape.getBackground(); Shape.setColor(Color.GREEN);