Java 如何以编程方式为视图创建渐变色背景?
strong文本有一个界面组件视图(简单的矩形),我们称之为“我的视图”。 。。。 我想以编程方式为myView创建线性渐变色背景。 渐变的两种颜色由经常变化的变量设置。所以我需要找到一个优化的方法。 我试着这样做:Java 如何以编程方式为视图创建渐变色背景?,java,android,Java,Android,strong文本有一个界面组件视图(简单的矩形),我们称之为“我的视图”。 。。。 我想以编程方式为myView创建线性渐变色背景。 渐变的两种颜色由经常变化的变量设置。所以我需要找到一个优化的方法。 我试着这样做: myView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { @Override public boolean on
myView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener()
{
@Override
public boolean onPreDraw()
{
myView.getViewTreeObserver().removeOnPreDrawListener(this);
int view_height = myView.getHeight();
ShapeDrawable myDrawable = new ShapeDrawable(new RectShape());
myDrawable.getPaint().setShader(new LinearGradient(0, 0, 0, view_height, color1, color2, Shader.TileMode.CLAMP));
myView.setBackgroundDrawable(myDrawable);
return false;
}
});
一切正常。每次Seekbar的进度发生变化时,都应执行此代码。经过测试,我意识到这是一个非常糟糕的方法。一切都正常工作,但滞后是显而易见的
Upd:
问题解决了!最好的方法是创建一个自定义的可绘制!感谢所有给我提示的论坛成员,特别是提供了最佳建议的pskink。您可以在XML文件中创建一个形状,并将其用作背景绘图
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:type="linear" android:gradientRadius="10"
android:startColor="#E9E9E9" android:endColor="#D4D4D4" />
</shape>
您可以这样做,并相应地更改方向和颜色
public void perform_action(View v)
{
Button btn = (Button) findViewById(R.id.push_button);
//Color.parseColor() method allow us to convert
// a hexadecimal color string to an integer value (int color)
int[] colors = {Color.parseColor("#008000"),Color.parseColor("#ADFF2F")};
//create a new gradient color
GradientDrawable gd = new GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM, colors);
gd.setCornerRadius(0f);
//apply the button background to newly created drawable gradient
btn.setBackground(gd);
}
在XML中,如下所示
<Button
android:id ="@+id/push_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button Gradient Background"
android:padding="15dp"
android:onClick="perform_action"
/>
例如,用户可以动态选择颜色并这样使用
int[] colors = {Color.parseColor("#008000"),Color.parseColor("#ADFF2F")};
//create a new gradient color
GradientDrawable gd = new GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM, colors);
gd.setCornerRadius(0f);
//apply the button background to newly created drawable gradient
view.setBackground(gd);
有了Kotlin,你只需要两行就可以做到
更改数组中的颜色值
val gradientDrawable = GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM,
intArrayOf(Color.parseColor("#008000"),
Color.parseColor("#ADFF2F"))
);
gradientDrawable.cornerRadius = 0f;
//Set Gradient
linearLayout.setBackground(gradientDrawable);
结果
使用自定义视图并通过调用
Canvas#drawPaint
来覆盖onSizeChanged
和onDraw
方法当然更好的解决方案是自定义可绘制的类,但我认为对于像你这样的“新贡献者”来说这太复杂了我正确理解你,你的意思是,同时覆盖onBoundsChange(Rect bounds)
并设置LinearGradient
,我的颜色变量通过Seekbar的进度值发生变化。提出的方法能解决这个问题吗?但我的渐变颜色应该通过代码设置,这将是一个动态的gradient@NokiandrX我添加了新的答案,请看
val gradientDrawable = GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM,
intArrayOf(Color.parseColor("#008000"),
Color.parseColor("#ADFF2F"))
);
gradientDrawable.cornerRadius = 0f;
//Set Gradient
linearLayout.setBackground(gradientDrawable);