以编程方式添加带有纯色和描边的渐变 [英] Programmatically add Gradient with solid color and stroke

查看:90
本文介绍了以编程方式添加带有纯色和描边的渐变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前我正在使用此代码添加颜色:

Currently I am using this code to add color:

ShapeDrawable drawable = new ShapeDrawable(new OvalShape());
drawable.getPaint().setColor(color);

现在我需要对它应用一些渐变颜色以及笔触(例如具有不同颜色的边框)。我将其设置为按钮的背景。

Now I need to apply some gradient colors to it along with stroke(like border with different color). I am setting this as background to button.

这是我所期望的,我需要以编程方式进行操作。

Here is what I am expecting, I need to do it programmatically.

推荐答案

RadialGradient 添加到您的 drawable 像这样:

Shader shader = new RadialGradient(x, y, radius, color0, color1, Shader.TileMode.REPEAT);
drawable.getPaint().setShader(shader);

显然,您可以互换 LinearGradient SweepGradient ,以及任何参数。

Obviously, you can interchange LinearGradient, SweepGradient, and any of the parameters.

以下是添加笔触的方法:

Here is how to add the stroke:

drawable.getPaint().setStrokeWidth(3);
drawable.getPaint().setColor(Color.WHITE);
drawable.getPaint().setStyle(Paint.Style.FILL_AND_STROKE);

嗯,我想我必须使用 GradientDrawable

GradientDrawable gd = new GradientDrawable();
gd.setColor(Color.RED);
gd.setCornerRadius(10);
gd.setStroke(2, Color.WHITE);
gd.setShape(GradientDrawable.OVAL);

这篇关于以编程方式添加带有纯色和描边的渐变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆