如何根据 Android Kotlin 中的宽度将圆角应用于视图
How to apply rounded corner to a view based on its width in Android Kotlin
我正在制作自定义进度条,如下图所示:
基本上,我创建了一个可绘制的 xml 背景文件:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="@color/jungleGreen"/>
<corners
android:bottomLeftRadius="40dp"
android:topLeftRadius="40dp"/>
</shape>
然后我将它应用到我正在使用的视图中:
<View
android:id="@+id/progress_bar_placeholder_view"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginEnd="45dp"
android:background="@drawable/background_filled_patronage_progressbar"
app:layout_constraintTop_toTopOf="parent"/>
完全没问题,我可以实现方案 1 和方案 2,但是当条形图接近尾端时,我如何以编程方式设置视图右上角和右下角的圆角,直到看起来像照片 3?
谢谢。
试试这个
public static void customView(View v, int backgroundColor, int borderColor)
{
GradientDrawable shape = new GradientDrawable();
shape.setShape(GradientDrawable.RECTANGLE);
shape.setCornerRadii(new float[] { 8, 8, 8, 8, 0, 0, 0, 0 });
shape.setColor(backgroundColor);
shape.setStroke(3, borderColor);
v.setBackground(shape);
}
来源:
Madhav 的解决方案对我有用,所以基本上我需要传入视图的宽度,然后计算角半径数并将其放入 gradientDrawable 中,如下所示:
private fun setupGraphBackground(view: View, graphWidth: Int) {
val gradientDrawable = GradientDrawable()
gradientDrawable.shape = GradientDrawable.RECTANGLE
gradientDrawable.setColor(resources.getColor(R.color.jungleGreen))
gradientDrawable.setStroke(0, null)
gradientDrawable.cornerRadii = floatArrayOf(45f, 45f,
graphWidth * calculationRules, graphWidth * calculationRules,
graphWidth * calculationRules, graphWidth * calculationRules,
45f, 45f)
view.background = gradientDrawable
}
因为 calculationRules 是我想要的规则,所以我可以获得右上角和右下角的正确半径。
我正在制作自定义进度条,如下图所示:
基本上,我创建了一个可绘制的 xml 背景文件:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="@color/jungleGreen"/>
<corners
android:bottomLeftRadius="40dp"
android:topLeftRadius="40dp"/>
</shape>
然后我将它应用到我正在使用的视图中:
<View
android:id="@+id/progress_bar_placeholder_view"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginEnd="45dp"
android:background="@drawable/background_filled_patronage_progressbar"
app:layout_constraintTop_toTopOf="parent"/>
完全没问题,我可以实现方案 1 和方案 2,但是当条形图接近尾端时,我如何以编程方式设置视图右上角和右下角的圆角,直到看起来像照片 3?
谢谢。
试试这个
public static void customView(View v, int backgroundColor, int borderColor)
{
GradientDrawable shape = new GradientDrawable();
shape.setShape(GradientDrawable.RECTANGLE);
shape.setCornerRadii(new float[] { 8, 8, 8, 8, 0, 0, 0, 0 });
shape.setColor(backgroundColor);
shape.setStroke(3, borderColor);
v.setBackground(shape);
}
来源:
Madhav 的解决方案对我有用,所以基本上我需要传入视图的宽度,然后计算角半径数并将其放入 gradientDrawable 中,如下所示:
private fun setupGraphBackground(view: View, graphWidth: Int) {
val gradientDrawable = GradientDrawable()
gradientDrawable.shape = GradientDrawable.RECTANGLE
gradientDrawable.setColor(resources.getColor(R.color.jungleGreen))
gradientDrawable.setStroke(0, null)
gradientDrawable.cornerRadii = floatArrayOf(45f, 45f,
graphWidth * calculationRules, graphWidth * calculationRules,
graphWidth * calculationRules, graphWidth * calculationRules,
45f, 45f)
view.background = gradientDrawable
}
因为 calculationRules 是我想要的规则,所以我可以获得右上角和右下角的正确半径。