Android渲染(一)------ Shader之SweepGradient用法(雷达扫描效果)

Shader定义:

官方的定义是这样的:

         Shader是返回水平跨度颜色的对象的基类,绘图期间。 Shader的子类安装在Paint调用中,paint.setShader(着色器)。 之后的任何对象(除了位图之外)都是使用该绘制绘制将从着色器获取其颜色。

  • 它有五个子类,分别是:SweepGradient、LinearGradient、BitmapShader、ComposeShader以及RadialGradient。
  • 这篇介绍的是其一:SweepGradient。

构造方法:

  • 其一:
     /**
      *一个着色器,围绕中心点绘制扫描渐变。
      *
      * @param cx中心的x坐标
      * @param cy中心的y坐标
      * @param colors要在中心周围分配的颜色。
      *阵列中必须至少有2种颜色。
      * @param位置可能为NULL。 的相对位置
      *颜色数组中的每个对应颜色,从头开始
      *为0,以1.0结尾。 如果值不是
      *单调,绘图可能会产生意想不到的结果。
      *如果position为NULL,则颜色自动生成
      *间隔均匀。
      */

      public SweepGradient(float cx, float cy,
            @NonNull @ColorInt int colors[], @Nullable float positions[]) {
        if (colors.length < 2) {
            throw new IllegalArgumentException("needs >= 2 number of colors");
        }
        if (positions != null && colors.length != positions.length) {
            throw new IllegalArgumentException(
                    "color and position arrays must be of equal length");
        }
        mType = TYPE_COLORS_AND_POSITIONS;
        mCx = cx;
        mCy = cy;
        mColors = colors.clone();
        mPositions = positions != null ? positions.clone() : null;
    }
  • 其二:
     /**
      *一个着色器,围绕中心点绘制扫描渐变。
      *
      * @param cx中心的x坐标
      * @param cy中心的y坐标
      * @param color0扫描开始时使用的颜色
      * @param color1扫描结束时使用的颜色
     */
    public SweepGradient(float cx, float cy, @ColorInt int color0, @ColorInt int color1) {
        mType = TYPE_COLOR_START_AND_COLOR_END;
        mCx = cx;
        mCy = cy;
        mColor0 = color0;
        mColor1 = color1;
        mColors = null;
        mPositions = null;
    }
  • 区别:

            第一种方法,可以设置颜色集,能放置多种颜色。

            第二种方法,设置两种颜色,开始扫描和结束扫描的颜色。

  • 用法:

           1、初始化colors集合,放置需要的颜色,然后初始化 sweepGradient,指定中心点,然后给画笔设置着色器。

 int [] colors = new int[]{context.getResources().getColor(R.color.progress_color11),
                context.getResources().getColor(R.color.progress_color12),
                context.getResources().getColor(R.color.progress_color13),
                context.getResources().getColor(R.color.progress_color14),
                context.getResources().getColor(R.color.progress_color15),
                context.getResources().getColor(R.color.progress_color16),
                context.getResources().getColor(R.color.progress_color17)};
sweepGradient = new SweepGradient(width / 2, width / 2, colors, null);
paint.setShader(sweepGradient);

          2、在onDraw()中,画它!

   canvas.drawCircle(width / 2, width / 2, width / 2, paint);

效果图:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值