如何设置包含渐变颜色作为背景色的Drawable?

时间:2019-09-11 06:52:31

标签: android android-studio

我有一个带有ImageView的活动。我想要的是将渐变颜色设置为ImageView的背景。

所以我要做的是在可绘制文件夹中创建了3组渐变色。

这些是我可绘制文件夹中的渐变颜色。

purple_love.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">

            <gradient
                android:startColor="#cc2b5e"
                android:endColor="#753a88"
                android:type="linear"
                android:angle="0">
            </gradient>
        </shape>
    </item>
</selector>

roseanna.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">

            <gradient
                android:startColor="#ffafbd"
                android:endColor="#ffc3a0"
                android:type="linear"
                android:angle="0">
            </gradient>
        </shape>
    </item>
</selector>

sexy_blue.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">

            <gradient
                android:startColor="#2193b0 "
                android:endColor="#6dd5ed"
                android:type="linear"
                android:angle="0">
            </gradient>
        </shape>
    </item>
</selector>

然后,将它们放入values文件夹下名为arrays.xml的文件中的数组中。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer-array name="gradient">
        <item>@drawable/roseanna</item>
        <item>@drawable/sexy_blue</item>
        <item>@drawable/purple_love</item>
        <item>@drawable/mauve</item>
    </integer-array>
</resources>

我想要做的是

1。从数组中随机检索它们。

  1. 在ImageView中将它们设置为BackGround颜色

我已经在MainActivity中尝试过此操作

public class MainActivity extends AppCompatActivity {

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

        TypedArray gradient = getResources().obtainTypedArray(R.array.gradient);

        Random random = new Random();
        int randomColor = random.nextInt(gradient.length());
        imageView = findViewById(R.id.imageView);

        imageView.setBackgroundColor(getResources().getColor((gradient.getResourceId(randomColor,0))));
        gradient.recycle();

    }
}

运行代码时,出现资源未找到异常。问题是什么,我该如何克服?

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.gradienttest, PID: 26869
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gradienttest/com.example.gradienttest.MainActivity}: android.content.res.Resources$NotFoundException: File res/drawable/purple_love.xml from color state list resource ID #0x7f070065
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2319)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2370)
        at android.app.ActivityThread.access$800(ActivityThread.java:155)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5426)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
        at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.content.res.Resources$NotFoundException: File res/drawable/purple_love.xml from color state list resource ID #0x7f070065
        at android.content.res.Resources.loadColorStateList(Resources.java:3632)
        at android.content.res.Resources.getColor(Resources.java:2020)
        at com.example.gradienttest.MainActivity.onCreate(MainActivity.java:26)
        at android.app.Activity.performCreate(Activity.java:5296)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2283)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2370) 
        at android.app.ActivityThread.access$800(ActivityThread.java:155) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:136) 
        at android.app.ActivityThread.main(ActivityThread.java:5426) 
        at java.lang.reflect.Method.invokeNative(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:515) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) 
        at dalvik.system.NativeStart.main(Native Method) 
     Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #3: <item> tag requires a 'android:color' attribute.
        at android.content.res.ColorStateList.inflate(ColorStateList.java:226)
        at android.content.res.ColorStateList.createFromXmlInner(ColorStateList.java:150)
        at android.content.res.ColorStateList.createFromXml(ColorStateList.java:129)
        at android.content.res.Resources.loadColorStateList(Resources.java:3628)
        at android.content.res.Resources.getColor(Resources.java:2020) 
        at com.example.gradienttest.MainActivity.onCreate(MainActivity.java:26) 
        at android.app.Activity.performCreate(Activity.java:5296) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2283) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2370) 
        at android.app.ActivityThread.access$800(ActivityThread.java:155) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:136) 
        at android.app.ActivityThread.main(ActivityThread.java:5426) 
        at java.lang.reflect.Method.invokeNative(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:515) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) 
        at dalvik.system.NativeStart.main(Native Method) 

1 个答案:

答案 0 :(得分:1)

您正在使用Resources.getColor
此方法适用于颜色或ColorStateList,并且您的选择器有所不同。

<item> tag requires a 'android:color' attribute.

您应该使用imageView.setBackground()而不是imageView.setBackgroundColor()来更改代码。通过这种方式,您可以使用可绘制对象。