3

我正在尝试将视图的背景设置为具有从 Palette api 生成的颜色的渐变

渐变将从纯色开始淡出,但我希望纯色部分占据大部分背景。现在它开始稳定,然后逐渐淡出视图宽度,我希望它从视图宽度的中心开始淡出。

这是我所做的

            Palette.from(resource!!.toBitmap()).generate {
                if (it != null) {
                    val paletteColor = it.getDarkVibrantColor("#000000".toColorInt())

                    val gradientDrawable = GradientDrawable(
                        GradientDrawable.Orientation.LEFT_RIGHT,
                        intArrayOf(colorWithAlpha(paletteColor, 0f), colorWithAlpha(paletteColor, 1.0f))
                    )
                    gradientDrawable.cornerRadius = 0f
                    _contentTextBackground.background = gradientDrawable
                }
            }

有没有办法将渐变设置为远离视图的末端?

4

6 回答 6

1

您可以通过 XMLcenterXcenterY更改GradientDrawable. 但遗憾的是,对于 Google 问题跟踪器上的讨论GradientDrawable,这不可能以编程方式实现。

于 2021-07-31T22:27:15.947 回答
1

对于低于 29 的 API,请尝试使用带有LinearGradient着色器的ShapeDrawable作为背景。这将为您提供对颜色过渡的精细控制。

对于 API 29+,GradientDrawable允许使用setColors()对颜色转换进行类似的精细控制。

private fun getBackgroundGradient(width: Int): Drawable {
    val colors = intArrayOf(Color.BLUE and 0x00FFFFFF, Color.BLUE, Color.BLUE)
    val offsets = floatArrayOf(0.0f, 0.7f, 1.0f)

    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        // Oddly, there is no constructor that accepts the offsets.
        GradientDrawable().apply {
            orientation = GradientDrawable.Orientation.LEFT_RIGHT
            setColors(colors, offsets)
        }
    } else {
        val shader: Shader = LinearGradient(
            0f, 0f, width.toFloat(), 1f, colors, offsets, Shader.TileMode.CLAMP
        )
        val shape = ShapeDrawable(RectShape())
        shape.paint.shader = shader
        shape
    }
}

我用作0.7f“中心”以在 50% 标记附近进行更好的(IMO)颜色过渡,但该值很容易是和0.5f之间的任何其他值。0f1.0f

在下图中,水平条是屏幕的宽度,只是一个View。垂直的红线将屏幕一分为二以标记过渡。

在此处输入图像描述

于 2021-08-02T00:28:02.413 回答
0

正如@Abdul Mateen 所述,您无法更改 Graient Drawable 中的属性。仅当您通过 XML 执行此操作时才有可能。但是有一个解决方法:

理论上,您可以将背景视图分成两半。然后,您有两个视图,然后您可以根据调色板库更改颜色。然后一个视图将是纯色,另一个视图必须在背景中具有渐变。如果你配置正确,你应该从中间有一个完美的渐变。

于 2021-08-01T09:50:15.793 回答
0

尝试扩展 GradientDrawable 并根据需要设置淡入淡出和颜色。

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

从此参考:https ://stackoverflow.com/a/5241693/14964046

于 2021-08-03T08:54:39.393 回答
0

只需在数组中添加纯色,如下所示

val gradientDrawable = GradientDrawable(
    GradientDrawable.Orientation.LEFT_RIGHT,
    intArrayOf(colorWithAlpha(paletteColor, 0f), colorWithAlpha(paletteColor, 1.0f),  colorWithAlpha(paletteColor, 1.0f))
                    )

它不优雅,但有效:-D

于 2021-08-03T15:29:13.117 回答
0

我最终做的是 Mike 在评论中建议的只是在 GradientDrawable 的颜色数组中添加更多的纯色

所以看起来像这样

val gradientDrawable = GradientDrawable(
                        GradientDrawable.Orientation.LEFT_RIGHT,
                        intArrayOf(colorWithAlpha(paletteColor, 0f), colorWithAlpha(paletteColor, 1.0f), colorWithAlpha(paletteColor, 1.0f), colorWithAlpha(paletteColor, 1.0f))
                    )
于 2021-08-04T12:06:39.937 回答