程序化的android drawable引用

时间:2013-12-12 01:52:26

标签: java android android-drawable

在Android程序中,我在java中创建了一个gradientDrawable

private Drawable getShape(){
        GradientDrawable ib = new GradientDrawable(GradientDrawable.Orientation.TL_BR, new int[] { 0xff555555,
                0xff999999, 0xffff6600});
        ib.setSize(250, 250);
        ib.setBounds(0, 0, 250, 250);
        ib.setGradientType(GradientDrawable.RECTANGLE);
        ib.setCornerRadius(15);
        ib.setGradientCenter(0.0f, 0.45f);

        return ib;
    }

此drawable将根据将来使用的输入进行更改。是否可以在drawable文件夹中的xml文件中使用此drawable。像这样:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/ib"/>

我看到的问题是,drawable / ib.png中没有任何内容,但我需要能够调用它。

我开始使用shaperawable xmls并且工作正常,但是当我希望用户能够根据他们的输入更改图像时,我遇到了问题。原谅我的漫无边际,但我是新手。我的想法在脑海中,但我无法将我的大脑包裹在如何做到这一点,或者甚至是否可能。提前谢谢。

1 个答案:

答案 0 :(得分:0)

GradientDrawable的独特之处在于它可以以编程方式支持关卡,因此它对EditText反馈非常有用。以下是我如何使用它,但它在动态路径上走得更远,而不是xml路径。

这个用于勾勒出灰色的文本视图(带有圆角的一些组合),然后使用该级别将轮廓更改为红色或绿色。

public class FeedbackDrawable extends GradientDrawable {
    private int bgColor;
    private int stroke;
    private int strokeWeight;
    private float dipScale = 1.0f;
    private int radius = 10;

    public static final int FEEDBACK_VALID = 0;
    public static final int FEEDBACK_NEUTRAL = 1;
    public static final int FEEDBACK_ERROR = 2;
    public static final int TOP_LEFT = 0x01;
    public static final int TOP_RIGHT = 0x02;
    public static final int BOTTOM_LEFT = 0x04;
    public static final int BOTTOM_RIGHT = 0x08;
    public static final int TOP_CORNERS = TOP_LEFT|TOP_RIGHT;
    public static final int BOTTOM_CORNERS = BOTTOM_LEFT|BOTTOM_RIGHT;

    public FeedbackDrawable(Context context, int dipRadius, int strokeWeight, int corners) {
        stroke = Color.BLACK;  // or pass them in
        bgColor = Color.WHITE;

        dipScale = context.getResources().getDisplayMetrics().density;
        radius = (int)(dipRadius * dipScale);

        this.strokeWeight = strokeWeight;

        float[] radii = new float[]{0,0,0,0, 0,0,0,0};
        if ( (corners & TOP_LEFT) > 0){
            radii[0] = radii[1] = radius;
        }
        if ( (corners & TOP_RIGHT) > 0){
            radii[2] = radii[3] = radius;
        }
        if ( (corners & BOTTOM_LEFT) > 0){
            radii[4] = radii[5] = radius;
        }
        if ( (corners & BOTTOM_RIGHT) > 0){
            radii[6] = radii[7] = radius;
        }
        setCornerRadii(radii);
        setUseLevel(true);
        setShape(GradientDrawable.RECTANGLE);
        setColor(bgColor);
        setStroke(strokeWeight, stroke);
        setBounds(0, -1, 0, 0);

        setLevel(FEEDBACK_NEUTRAL);
    }




    @Override
    protected boolean onLevelChange(int level) {
        switch (level){
            case FEEDBACK_ERROR:
                setStroke(strokeWeight, Color.RED);
                break;
            case FEEDBACK_NEUTRAL:
                setStroke(strokeWeight, Color.LTGRAY);
                break;
            case FEEDBACK_VALID:
                setStroke(strokeWeight, Color.parseColor("#4AA02C"));
                break;
            default:
                setStroke(strokeWeight, stroke);
                break;
        }
        return true;
    }
}

然后使用它:

  TextView tv = (TextView) rootView.findViewById(R.id.text);
  // 10 is the corner radius in dips, 3 is the stroke weight 
  tv.setBackgroundDrawable(new FeedbackDrawable(getActivity(), 10, 3, FeedbackDrawable.BOTTOM_CORNERS));
  tv.getBackground().setLevel(FeedbackDrawable.FEEDBACK_NEUTRAL) ;

随时您可以通过更改级别来更改反馈

  tv.getBackground().setLevel(FeedbackDrawable.FEEDBACK_ERROR) ;