【问题标题】:Using a gradientDrawable with more than three colors set使用设置了三种以上颜色的 gradientDrawable
【发布时间】:2012-12-24 11:05:33
【问题描述】:

根据我的阅读,您可以使用gradientDrawable 并为其设置三种颜色,例如:

<gradient startColor="#00FF00" centerColor="#FFFF00" endColor="#FFFFFF"/>

但是如果我想要三种以上的颜色,不仅如此,我还希望能够设置每种颜色的放置位置(重量/百分比)?

是否可以使用 API 或者我应该制作自己的自定义可绘制对象?如果我需要制作自己的自定义drawable,我应该怎么做?

【问题讨论】:

  • 我想你可以找到答案here

标签: android colors gradient android-drawable


【解决方案1】:

将此代码放入您的 onCreate() 方法中:

ShapeDrawable.ShaderFactory shaderFactory = new ShapeDrawable.ShaderFactory() {
    @Override
    public Shader resize(int width, int height) {
        LinearGradient linearGradient = new LinearGradient(0, 0, width, height,
            new int[] { 
                0xFF1e5799, 
                0xFF207cca, 
                0xFF2989d8, 
                0xFF207cca }, //substitute the correct colors for these
            new float[] {
                0, 0.40f, 0.60f, 1 },
            Shader.TileMode.REPEAT);
         return linearGradient;
    }
};
PaintDrawable paint = new PaintDrawable();
paint.setShape(new RectShape());
paint.setShaderFactory(shaderFactory);

并使用这个drawable作为背景。

您也可以通过创建图层在 xml 中添加三种以上的颜色。但是在 XML 中它是相当复杂的。

【讨论】:

  • 这看起来不错,但我该如何使用呢?另外,是否可以将其作为自定义的可绘制对象?
  • 如果你必须设置动作栏背景:getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); getActionBar().setBackgroundDrawable(paint);或者如果你有一个按钮://在你的 xml 文件中定义一个按钮,ID 为按钮 Button button = (Button)findViewById(R.id.button); button.setBackgroundDrawable((Drawable)paint);
  • 这看起来很酷。所以如果我想要一个自定义的drawable,我需要做的就是扩展PaintDrawable,然后把你设置的代码放在自己身上?
  • 请尝试展示样品。在测试你的代码时,我得到一个对角线渐变。
  • 这个答案很好用。对于从上到下的渐变,将渐变的创建替换为以下内容:“LinearGradient(0, 0, 0, height,..." (因此将第二个渐变点替换为 0|height 而不是 width|height)
【解决方案2】:

不可能在 xml 文件中做,但是你可以在你的 java 代码中使用 GradientDrawable 类应用 +3 颜色渐变:

GradientDrawable gradientDrawable = new GradientDrawable(
                Orientation.TOP_BOTTOM,
                new int[]{ContextCompat.getColor(this, R.color.color1),
                        ContextCompat.getColor(this, R.color.color2),
                        ContextCompat.getColor(this, R.color.color3),
                        ContextCompat.getColor(this, R.color.color4)});

        findViewById(R.id.background).setBackground(gradientDrawable);

【讨论】:

  • 这也是一个不错的解决方案,而且很简短。
  • 但是如何设置颜色的位置呢?
  • 数组颜色位置对应渐变中的颜色位置。请注意您设置的方向。
【解决方案3】:

使用 GradientDrawble 我们可以做到这一点

GradientDrawable gradientInsta = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT,
                new int[] {
                        Color.parseColor("#F9ED32"),
                        Color.parseColor("#F6C83F"),
                        Color.parseColor("#F2735F"), 
                        Color.parseColor("#EF3E73"),
                        Color.parseColor("#EE2A7B"),
                        Color.parseColor("#D22A8A"),
                        Color.parseColor("#8B2AB1"),
                        Color.parseColor("#1C2AEF"),
                        Color.parseColor("#002AFF"),
                        ContextCompat.getColor(MainActivity.this, R.color.colorPrimary)
        });
findViewById(R.id.insta).setBackground(gradientInsta);

【讨论】:

  • 我认为是我的浏览器错误将“翻译成带引号的字符串,现在可以正常工作了。 ?
【解决方案4】:

我认为以下是可能的解决方案。

  • 您可以使用渐变创建多个形状并形成更大的 形状。
  • 您可以通过扩展 GradientDrawable Class 参考以下文档。

  • Gradient Drawable Documentation

【讨论】:

  • 你能举个例子说明如何做第二个吗?
【解决方案5】:

在 Kotlin 中你可以这样做:

将 color1,2,..n 替换为您的颜色值

                 //Create Gradient 
                val gradientDrawable = GradientDrawable(
                    GradientDrawable.Orientation.TOP_BOTTOM,
                    intArrayOf(color1,color1 ,color1, colorn)
                );
                gradientDrawable.cornerRadius = 0f;

【讨论】:

    猜你喜欢
    • 2021-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-05
    • 1970-01-01
    • 1970-01-01
    • 2020-08-27
    • 2014-12-17
    相关资源
    最近更新 更多