如何在Android中为TextView提供渐变?[英] How to give gradient to TextView in android?

本文是小编为大家收集整理的关于如何在Android中为TextView提供渐变?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我从Android中的TextView中搜索.但是没有得到适当的解决方案.

我有这样的屏幕,如此图像中的文本实际上具有梯度

如果我将图像放在文本中,但我有许多具有渐变的TextView,然后应用程序的大小可能很大. 所以如果有人知道解决方案,请发布. 谢谢.

推荐答案

在这里,

int[] color = {Color.DKGRAY,Color.CYAN};
        float[] position = {0, 1};
        TileMode tile_mode = TileMode.REPEAT;
        LinearGradient lin_grad = new LinearGradient(0, 0, 0, 35,color,position, tile_mode);
        Shader shader_gradient = lin_grad;
        your_text_view.getPaint().setShader(shader_gradient);

编辑:

嗨@android开发人员,

如果要对视图执行一些垂直或任何渐变,

请使用,

RadialGradient而不是LinearGradient.

只有你必须调整,

x coordinate of the center of the radius.
y coordinate of the center of the radius.

and also,

radius value of the view.

并使用瓷砖模式作为镜像而不是重复.

我有样本,但它可能无法精确你想要的东西,

TileMode tile_mode = TileMode.MIRROR or TileMode.REPEAT;
        RadialGradient rad_grad = new RadialGradient(0, 3, 5, Color.BLUE,Color.RED,tile_mode, tile_mode);
        Shader shader_gradient = rad_grad;
        your_text_view.getPaint().setShader(shader_gradient);

,您也可以使用SweepGradient而不是LinearGradient.

本文地址:https://www.itbaoku.cn/post/102614.html

问题描述

i search from while for giving gradient to textView in android.but not getting proper solution any where.

i have the screen like thisthe text in the image is actually having the gradient

if i put the images as text, but i have many textview which having gradient,then application may be large in size. so if anybody know the solution please post it. Thanks.

推荐答案

Here It goes,

int[] color = {Color.DKGRAY,Color.CYAN};
        float[] position = {0, 1};
        TileMode tile_mode = TileMode.REPEAT;
        LinearGradient lin_grad = new LinearGradient(0, 0, 0, 35,color,position, tile_mode);
        Shader shader_gradient = lin_grad;
        your_text_view.getPaint().setShader(shader_gradient);

Edited:

Hi @android developer,

If you want to do some vertical or any gradients to the view,

Please use,

RadialGradient instead of LinearGradient.

Only thing is you have to adjust,

x coordinate of the center of the radius.
y coordinate of the center of the radius.

and also,

radius value of the view.

and use Tile Mode as Mirror instead of Repeat.

I have sample but it may not exact of what you want,

TileMode tile_mode = TileMode.MIRROR or TileMode.REPEAT;
        RadialGradient rad_grad = new RadialGradient(0, 3, 5, Color.BLUE,Color.RED,tile_mode, tile_mode);
        Shader shader_gradient = rad_grad;
        your_text_view.getPaint().setShader(shader_gradient);

And you can use SweepGradient also instead of LinearGradient.