使用GradientDrawable让渐变色的起始点离视图底部更远

3

我正在尝试将视图的背景设置为从调色板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
                }
            }

有没有一种方法可以使渐变离视图的末端更远?

1
我不确定您想要哪种淡入淡出效果,但如果我正确理解了其余部分,那么您只需要在数组中间添加另一个实心颜色值;例如:intArrayOf(colorWithAlpha(paletteColor, 0f), colorWithAlpha(paletteColor, 1.0f), colorWithAlpha(paletteColor, 1.0f)) - Mike M.
1
为了确保我们在同一页面上,这里有一个简单的演示来展示我所说的效果,透明的端点在左侧,我认为这就是给定代码片段中的情况:https://i.stack.imgur.com/54lA5.png。 - Mike M.
@MikeM,如果您想将此作为答案提交,我会接受,因为这是我选择的路线,并且效果最好。 - tyczj
我很好。只是一个快速的建议。很高兴能帮到你。请随意按照您认为合适的方式继续进行。不过还是谢谢你的提议。干杯! - Mike M.
6个回答

1
对于API 29以下的版本,请尝试使用ShapeDrawableLinearGradient Shader作为背景。这将使您对颜色过渡拥有更精细的控制。
对于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%附近有更好的(在我看来)颜色过渡,但该值可以很容易地是0.5f0f1.0f之间的任何其他值。

在下面的图像中,水平条是屏幕的宽度,只是一个视图。垂直的红线将屏幕分成两部分以标记转换点。

enter image description here


1

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://dev59.com/ZG445IYBdhLWcg3wR4VO#5241693


0
只需在数组中再添加一个实心颜色,如下所示。
val gradientDrawable = GradientDrawable(
    GradientDrawable.Orientation.LEFT_RIGHT,
    intArrayOf(colorWithAlpha(paletteColor, 0f), colorWithAlpha(paletteColor, 1.0f),  colorWithAlpha(paletteColor, 1.0f))
                    )

虽然不太优雅,但是能用就行了 :-D


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

0

正如@Abdul Mateen所说,你不能在渐变图形中更改属性。只有通过XML才能实现这一点。但是有一个变通方法:

理论上,你可以将背景视图分成两半。然后你有两个视图,你可以根据调色板库来改变颜色。一个视图将成为纯色,另一个视图则需要具有渐变背景。如果正确配置,你应该得到一个完美的从中间渐变的效果。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接