本文介绍了如何从一个GradientDrawable的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的EditText 在一个XML文件中定义的背景:

I have an EditText with the background defined in a xml file:

<EditText
    android:id="@+id/myEditText"
    style="@style/Theme.x.EditText.ReadOnly"
    android:layout_marginRight="5dip"
    android:layout_span="7"
    android:nextFocusDown="@+id/myEditText2"
    android:selectAllOnFocus="true" />

XML文件: my_edit_text.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle">
    <gradient android:startColor="@android:color/background_dark" android:endColor="@android:color/background_dark"
        android:angle="0"/>
    <solid android:color="#FA0000"/>
    <stroke android:width="1px" android:color="#5A5D5A"/>
    <corners 
        android:bottomLeftRadius="3dip"
        android:topLeftRadius="3dip"
        android:bottomRightRadius="3dip"
        android:topRightRadius="3dip"
        android:color="@color/crAmarelo"/>
    <padding 
        android:left="5dip"
        android:right="5dip"/>
</shape>

和我需要获得该视图具有色彩:

And I need to get the color that the view has:

EditText et = (EditText) solo.getView(R.id.myEditText);
GradientDrawable gd = (GradientDrawable) et.getBackground();

但我不知道如何从纯色 GradientDrawable

推荐答案

您可以参考GradientDrawable.java然后做一些适当的修改,以使其工作〜

You can reference GradientDrawable.java then make some proper modification to make it work~

import android.content.res.Resources;
...

// it's the same with the GradientDrawable, just make some proper modification to make it compilable
public class ColorGradientDrawable extends Drawable {
    ...
    private int mColor; // this is the color which you try to get
    ...
    // original setColor function with little modification
    public void setColor(int argb) {
        mColor = argb;
        mGradientState.setSolidColor(argb);
        mFillPaint.setColor(argb);
        invalidateSelf();
    }

    // that's how I get the color from this drawable class
    public int getColor() {
        return mColor;
    }
    ...

    // it's the same with GradientState, just make some proper modification to make it compilable
    final public static class GradientState extends ConstantState {
        ...
    }
}

这篇关于如何从一个GradientDrawable的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 04:26