android 横向渐变颜色,Android开发之渐变色

54b458d54ef8

Android开发之渐变色

在android.graphics中提供了有关Gradient类,包含LinearGradient线性渐变、 RadialGradient径向渐变和SweepGradient梯度渐变三种,它们的基类为android.graphics.Shader。

1. LinearGradient 线性渐变

构造体

LinearGradient(float x0, float y0, float x1, float y1, int[] colors, float[] positions, Shader.TileMode tile)

LinearGradient(float x0, float y0, float x1, float y1, int color0, int color1, Shader.TileMode tile)

参数

说明

x0

渐变线起点的x坐标

y0

渐变线起点的y坐标

x1

渐变线末端的x坐标

y1

渐变线末端的y坐标

colors

要沿着渐变线分布的颜色数组

color0

渐变线开始处的颜色

color1

渐变线末端的颜色

positions

颜色数组中每个对应颜色的相对位置[0,1]。如果为null,则颜色沿线均匀分布。

tile

着色器平铺模式

例子如下

Paint paint =new Paint();

//两个坐标形成变量,规定了渐变的方向和间距大小,着色器为镜像

LinearGradient linearGradient =new LinearGradient(0,0,200,0, Color.RED,Color.BLUE, Shader.TileMode.MIRROR);

paint.setShader(linearGradient);

paint.setStrokeWidth(50);

canvas.drawLine(0,getMeasuredHeight()/2,getMeasuredWidth(),getMeasuredHeight()/2, paint);

54b458d54ef8

LinearGradient

2. RadialGradient 径向/放射渐变

构造体

RadialGradient(float x, float y, float radius, int[] colors, float[] positions, Shader.TileMode tile)

RadialGradient(float x, float y, float radius, int color0, int color1, Shader.TileMode tile)

参数

说明

x

半径中心的x坐标

y

半径中心的y坐标

radius

必须是积极的。此渐变的圆的半径

colors

颜色分布在圆的中心和边缘之间

color0

圆圈中心的颜色

color1

圆圈边缘的颜色

positions

颜色数组中每个对应颜色的相对位置[0,1]。如果为null,则颜色沿线均匀分布。

tile

着色器平铺模式

例子如下

paint =new Paint();

radialGradient =new RadialGradient(240,360,200, new int[]{Color.BLUE, Color.GREEN, Color.RED },null, Shader.TileMode.CLAMP);

paint.setShader(radialGradient);

canvas.drawCircle(240,360,200,paint);

54b458d54ef8

RadialGradient

3. SweepGradient 扫描/梯度/扇形渐变

构造体

SweepGradient(float x, float y, int[] colors, float[] positions)

SweepGradient(float x, float y, int color0, int color1)

参数

说明

x

中心的x坐标

y

中心的y坐标

colors

颜色分布在中心周围,阵列中必须至少有2种颜色。

color0

在扫描开始时使用的颜色

color1

在扫描结束时使用的颜色

positions

颜色数组中每个对应颜色的相对位置[0,1]。如果为null,则颜色沿线均匀分布。

例子

paint =new Paint();

int[] colors = new int[]{Color.GREEN, Color.GREEN, Color.BLUE, Color.RED, Color.RED};

sweepGradient = new SweepGradient(240, 360,colors,null);

paint.setShader(sweepGradient);

canvas.drawCircle(x,y,200,paint);

54b458d54ef8

SweepGradient

\( ^o ^)/了解!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用渐变动画和属性动画,以及自定义 View 来实现横向渐变无限循环的控件。 首先,您需要创建一个自定义 View,实现它的 onDraw() 方法,来绘制渐变背景。您可以使用 LinearGradient 类来创建横向渐变的 Shader,然后在 onDraw() 方法中将其设置为画笔的颜色: ```java Paint paint = new Paint(); LinearGradient gradient = new LinearGradient(0, 0, getWidth(), 0, colors, null, Shader.TileMode.REPEAT); paint.setShader(gradient); canvas.drawRect(0, 0, getWidth(), getHeight(), paint); ``` 其中,colors 是一个颜色数组,表示渐变的起始和结束颜色。 接下来,您需要使用属性动画来实现控件的滚动。您可以使用 ValueAnimator 类来创建一个动画,然后在 onAnimationUpdate() 方法中更新 View 的左边界,并在动画结束时重新开始动画: ```java ValueAnimator animator = ValueAnimator.ofInt(0, getWidth()); animator.setDuration(5000); animator.setRepeatCount(ValueAnimator.INFINITE); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { int value = (int) animation.getAnimatedValue(); setLeft(value); setRight(value + getWidth()); } }); animator.start(); ``` 最后,您需要在布局文件中使用您的自定义 View,将其添加到一个水平方向的 LinearLayout 中,以便它可以水平滚动。例如: ```xml <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <com.example.GradientView android:layout_width="match_parent" android:layout_height="100dp" /> </LinearLayout> ``` 这样,您就可以创建一个横向渐变无限循环的控件了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值