Android 渲染之Linear、Sweep、Radial三种方式初使用

这篇文章不难,也没什么技术讲解,小弟不才,就简介的介绍一下LinearGradient、SweepGradient、RadialGradient这三种渲染的使用例子。具体代码分析此处就不写了,也是为自己做个记录,这三种的使用。

首先,为了方便。先自定一了个MyView 继承View ,然后重写onMeasure 和 onDraw 方法。为了更简便,我直接在onMeasure方法中直接定义了宽高为500的正方形。

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(500,500);
    }

就是如此简单的一个View, 好了。下面开始使用三种渲染吧...

LinearGradient

线性渲染,顾名思义,就是和LiearLayout相似的渲染方式,比较整齐的样子吧,一条一条的。

   @Override
    protected void onDraw(Canvas canvas) {
        Paint paint1 = new Paint();
        paint1.setShader(new LinearGradient(0,0,getWidth()/2,getHeight()/2,
                new int[]{Color.RED, Color.WHITE, Color.BLUE},null, Shader.TileMode.MIRROR));
        canvas.drawRect(0,0,getWidth(),getHeight(),paint1);

        super.onDraw(canvas);
    }
首先弄个画笔,然后设置他的setShader属性。我这里直接就new LiearGradient() 了。然后里面会需要传入几个参数:
  /** Create a shader that draws a linear gradient along a line.
        @param x0           The x-coordinate for the start of the gradient line
        @param y0           The y-coordinate for the start of the gradient line
        @param x1           The x-coordinate for the end of the gradient line
        @param y1           The y-coordinate for the end of the gradient line
        @param  colors      The colors to be distributed along the gradient line
        @param  positions   May be null. The relative positions [0..1] of
                            each corresponding color in the colors array. If this is null,
                            the the colors are distributed evenly along the gradient line.
        @param  tile        The Shader tiling mode
    */
    public LinearGradient(float x0, float y0, float x1, float y1, int colors[], float positions[],
            TileMode tile) {
        if (colors.length
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值