简体   繁体   English

如何以编程方式为视图创建渐变色背景?

[英]How to programmatically create gradient color background for a View?

strong text There is an interface component View (simple rectangle) let's call it "my_view". strong text有一个界面组件View(简单的矩形)我们就叫它“my_view”。 ... ...

View myView = (View) findViewById(R.id.my_view);

... I want to programmatically create linear gradient color background for a myView. ...我想以编程方式为 myView 创建线性渐变颜色背景。 The two colors of the gradient are set by variables that will change frequently.渐变的两种颜色由经常变化的变量设置。 So I need to find an optimized way.所以我需要找到一个优化的方法。 I tried to do this:我试图这样做:

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

Everything is working.一切正常。 This code should be executed each time when the progress of the Seekbar changes.每次当 Seekbar 的进度发生变化时,都应执行此代码。 After testing, I realized that this is a very bad method.Everything works as it should, but lags are noticeable.经过测试,我意识到这是一个非常糟糕的方法。一切正常,但滞后很明显。


Upd: The problem was solved!更新:问题解决了! The best method is to create a Custom Drawable!最好的方法是创建一个自定义 Drawable! Thanks to all forum members who gave me tips, especially pskink , who gave the best advice.感谢所有给我提示的论坛成员,尤其是pskink ,他们给出了最好的建议。

use can select color dynamically and use like this.使用可以动态选择颜色并像这样使用。

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

With Kotlin you can do that in just 2 lines使用 Kotlin,您只需 2 行即可完成此操作

Change color values in the array更改数组中的颜色值

              val gradientDrawable = GradientDrawable(
                    GradientDrawable.Orientation.TOP_BOTTOM,
                    intArrayOf(Color.parseColor("#008000"),
                               Color.parseColor("#ADFF2F"))
                );
                gradientDrawable.cornerRadius = 0f;

               //Set Gradient
               linearLayout.setBackground(gradientDrawable);

Result结果

在此处输入图片说明

you can create a shape in the XML file and use this as background drawable.您可以在 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>

You may do something like this and change Orientation and color accordingly.您可以执行类似的操作并相应地更改方向和颜色。

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

in XML like this像这样在 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"
 />

here and here is an example 这里这里是一个例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM