SweepGradient扫描渐变的(float cx, float cy, int[] colors, float[] positions)参数的意义

SweepGradient 作为画图时,用到的扫描渐变。
有两个方法。
第一个方法:
public SweepGradient(float cx, float cy, int color0, int color1)

Parameters:

  • cx 渲染中心点x 坐标
  • cy 渲染中心点y 坐标
  • color0 起始渲染颜色
  • color1 结束渲染颜色

第二个方法:
public SweepGradient(float cx, float cy, int[] colors, float[] positions)

Parameters:

  • cx 渲染中心点x 坐标
  • cy 渲染中心y 点坐标
  • colors 围绕中心渲染的颜色数组,至少要有两种颜色值
  • positions 相对位置的颜色数组,可为null,  若为null,可为null,颜色沿渐变线均匀分布

很明显,第一个方法是第二个方法的简化。
所以,我只用讲下面一个方法,就能两个方法都懂了。
查阅了API:

 /**
     * A subclass of Shader that draws a sweep gradient around a center point.
     *
     * @param cx       The x-coordinate of the center
     * @param cy       The y-coordinate of the center
     * @param colors   The colors to be distributed between around the center.
     *                 There must be at least 2 colors in the array.
     * @param positions May be NULL. The relative position of
     *                 each corresponding color in the colors array, beginning
     *                 with 0 and ending with 1.0. If the values are not
     *                 monotonic, the drawing may produce unexpected results.
     *                 If positions is NULL, then the colors are automatically
     *                 spaced evenly.
     */

前三个参数很容易理解:
- cx 渲染中心点x 坐标
- cy 渲染中心y 点坐标
- colors 围绕中心渲染的颜色数组,至少要有两种颜色值
而对于positions这个 float[]数组,写过代码的人,可能反而会很糊涂。
这个positions数组指定的是颜色在圆上的渲染分布。
- 假如要绘制完整的圆,positions指定为null就可以了,默认会按照颜色数组的顺序帮你渲染。
- 但,假如是像我要做的控件,是非均匀变色的圆,则需要我们去指定各个颜色的位置。
注意事项:渲染的方向是顺时针,位置从0开始,到1结束,而且数组要呈单调递增,不然可效果会错误。
所以举例:

image.png

颜色数组为{“绿色”,”黑色”,”红色”,”蓝色”,”黄色”},
绿色在180度,所以他对应的位置参数是0.5 (顺时针算)
以此类推,黑色225度,0.625f
红色 270度,0.75f
蓝色315度,0.875f
黄色360度,1
所以对应的positions数组就是{ 0.5f, 0.625f, 0.75f, 0.875f, 1f }

package com.hencoder.hencoderpracticedraw2.practice;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

public class Practice03SweepGradientView extends View {
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

    public Practice03SweepGradientView(Context context) {
        super(context);
    }

    public Practice03SweepGradientView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public Practice03SweepGradientView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    {
        // 用 Paint.setShader(shader) 设置一个 SweepGradient
        // SweepGradient 的参数:圆心坐标:(300, 300);颜色:#E91E63 到 #2196F3

        Shader shader = new SweepGradient(300,300, new int[]{ Color.GREEN, Color.BLACK, Color.RED, Color.BLUE,Color.YELLOW},
                new float[]{0.5f, 0.625f, 0.75f, 0.875f, 1f}
        );
        paint.setShader(shader);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(300, 300, 200, paint);
    }
}

这样对第二个更详细的方法,就能理解了,至于第一个就是两种颜色的均匀分布,很好理解。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值