[英]When creating shape background programmatically, how to set stroke color using android.graphics.Color?
[英]How can I create this shape.xml programmatically/dynamically in Android. Set stroke color(pics/code included)
我需要以编程方式创建此形状(我需要根据用户选择更改其笔触颜色)
<?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 但它似乎没有笔触颜色选项? 此外,无论您为它们设置什么宽度或高度,形状都会倾向于拉伸? 谁能解释一下?
试过这个
ShapeDrawable sd = new ShapeDrawable();
sd.setShape(new OvalShape());
sd.getPaint().setStrokeWidth(20);
sd.getPaint().setColor(Color.RED);
出于某种原因,您需要使用GradientDrawable
(强制使用纯色而不是真正的渐变):
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
这是你使用上面的代码得到的:
为了实现阴影效果,使用elevation属性或创建一个带有阴影层的LayerListDrawable 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.