如何在Android中以编程/动态方式创建此shape.xml。设置笔划颜色(包括图片/代码)
我需要以编程方式创建此形状(我需要根据用户的选择更改其笔划颜色)如何在Android中以编程/动态方式创建此shape.xml。设置笔划颜色(包括图片/代码),android,Android,我需要以编程方式创建此形状(我需要根据用户的选择更改其笔划颜色) 出于某种原因,您需要使用GradientDrawable(强制使用纯色而非真实渐变): 这是使用上述代码得到的结果: 要实现阴影效果,请使用“高程”属性或使用阴影层创建阴影 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape
出于某种原因,您需要使用
GradientDrawable
(强制使用纯色而非真实渐变):
这是使用上述代码得到的结果:
要实现阴影效果,请使用“高程”属性或使用阴影层创建阴影
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
<solid
android:color="#FFEECC"
/>
<size
android:width="100dp"
android:height="100dp"
/>
<stroke android:width="5dp" android:color="#444444" />
</shape>
ShapeDrawable sd = new ShapeDrawable();
sd.setShape(new OvalShape());
sd.getPaint().setStrokeWidth(20);
sd.getPaint().setColor(Color.RED);
int backgroundColor = Color.RED;
int strokeColor = Color.GRAY;
int strokeSize = 10;
GradientDrawable drawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, new int[]{backgroundColor, backgroundColor});
drawable.setShape(GradientDrawable.OVAL);
drawable.setStroke(strokeSize, strokeColor);
//...use your drawable