我在XML中定义了ShapeDrawable
,具有给定的笔触宽度和角半径。
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
android:width="5dp"
android:color="@color/focus_highlight" /> <!-- Color is YELLOW -->
<corners android:radius="5dp" />
</shape>
我想以编程方式更改笔画宽度和可绘制的颜色。我可以更改Drawable
的颜色,但是,我无法对笔划宽度和角半径执行相同的操作。有人可以告诉你怎么做吗?这是我的代码段 -
public class MainActivity extends Activity {
.......
@Override
protected void onCreate(Bundle savedInstanceState) {
......
RelativeLayout relLayout = (RelativeLayout) findViewById(R.id.relLayout);
drawView = new DrawView(this);
relLayout.addView(drawView);
.....
}
class DrawView extends View {
.....
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
TypedValue value = new TypedValue();
final boolean resolved = mContext.getTheme().resolveAttribute(
R.attr.acc, value, true);
if (resolved) {
drawable.setBounds(100, 300, 300, 500);
drawable.setColorFilter(Color.RED, Mode.XOR); // Changed color to RED
drawable.draw(canvas);
}
}
}
}
答案 0 :(得分:2)
您可以使用GradientDrawable
为视图设置描边...
GradientDrawable gd = new GradientDrawable(Orientation.BOTTOM_TOP, new int[]{Color.BLUE, Color.GREEN});
gd.setStroke(5, Color.RED);