gpt4 book ai didi

android - 以编程方式更改 ShapeDrawable 的笔划宽度

转载 作者:行者123 更新时间:2023-11-29 00:17:56 26 4
gpt4 key购买 nike

我有一个在 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 的颜色,但是,我无法对 Stroke 宽度和圆角半径做同样的事情。有人能告诉我怎么做吗?这是我的代码 fragment -

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);
}
}
}
}

最佳答案

您可以使用 GradientDrawable 为您的 View 设置 Stroke...

GradientDrawable gd = new GradientDrawable(Orientation.BOTTOM_TOP, new int[]{Color.BLUE, Color.GREEN});
gd.setStroke(5, Color.RED);

关于android - 以编程方式更改 ShapeDrawable 的笔划宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25237196/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com