在运行时更改Android上的渐变背景颜色

Phe*_*ome 22 android gradient background drawable

我正在尝试Drawable背景,到目前为止没有任何问题.

我现在正试图在运行时更改渐变背景颜色.

不幸的是,似乎没有API可以在运行时更改它.甚至没有尝试变异()drawable,如下所述:可绘制的突变

示例XML看起来像这样.它正如预期的那样有效.

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="#330000FF"
        android:endColor="#110000FF"
        android:angle="90"/>
</shape>
Run Code Online (Sandbox Code Playgroud)

遗憾的是,我想要一个具有各种颜色的列表,并且它们必须在运行时以编程方式进行编程.

还有另一种方法可以在运行时创建此渐变背景吗?甚至可能不完全使用XML?

Phe*_*ome 42

是! 找到了办法!

不得不忘记XML,但这是我如何做到的:

在我的getView()重载函数(ListAdapter)上,我只需要:

    int h = v.getHeight();
    ShapeDrawable mDrawable = new ShapeDrawable(new RectShape());
    mDrawable.getPaint().setShader(new LinearGradient(0, 0, 0, h, Color.parseColor("#330000FF"), Color.parseColor("#110000FF"), Shader.TileMode.REPEAT));
    v.setBackgroundDrawable(mDrawable);
Run Code Online (Sandbox Code Playgroud)

这给了我与上面的XML背景相同的结果.现在我可以以编程方式设置背景颜色.

  • 是否可以给出一个角度值? (2认同)

小智 12

我尝试使用Phenome的Button视图解决方案.但不知怎的,它没有用.

我想出了其他的东西:(礼貌:Android API演示示例)

package com.example.testApp;

import android.app.Activity;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.view.View;

public class TetApp extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        View v = findViewById(R.id.btn);
        v.setBackgroundDrawable( new DrawableGradient(new int[] { 0xff666666, 0xff111111, 0xffffffff }, 0).SetTransparency(10));

    }

    public class DrawableGradient extends GradientDrawable {
        DrawableGradient(int[] colors, int cornerRadius) {
            super(GradientDrawable.Orientation.TOP_BOTTOM, colors);

            try {
                this.setShape(GradientDrawable.RECTANGLE);
                this.setGradientType(GradientDrawable.LINEAR_GRADIENT);
                this.setCornerRadius(cornerRadius);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public DrawableGradient SetTransparency(int transparencyPercent) {
            this.setAlpha(255 - ((255 * transparencyPercent) / 100));

            return this;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Raj*_*h.k 5

在下面尝试我的代码:

 int[] colors = new int[2];
            colors[0] = getRandomColor();
            colors[1] = getRandomColor();


            GradientDrawable gd = new GradientDrawable(
                    GradientDrawable.Orientation.TOP_BOTTOM, colors);

            gd.setGradientType(GradientDrawable.RADIAL_GRADIENT);
            gd.setGradientRadius(300f);
            gd.setCornerRadius(0f);
            YourView.setBackground(gd);
Run Code Online (Sandbox Code Playgroud)

产生随机颜色的方法:

public static int getRandomColor(){
    Random rnd = new Random();
    return Color.argb(255, rnd.nextInt(256), rnd.nextInt(56), rnd.nextInt(256));
}
Run Code Online (Sandbox Code Playgroud)