LinearGradient的用法

2018-04-07  本文已影响0人  名字_都被占了

LinearGradient线性渲染一个矩形和一个文字

public class ZiDingYiView extends View {
    private Paint mPaint;
    private LinearGradient linearGradient;
    public ZiDingYiView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        mPaint.setStyle(Paint.Style.FILL);
    }
 @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setShader(linearGradient);
        mPaint.setTextSize(60);
        canvas.drawRect(0,0,width/2,height/2,mPaint);
        canvas.drawText("我是自定义字体",width/2,height/2,mPaint);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        linearGradient=new LinearGradient(getMeasuredWidth()/2,getMeasuredHeight()/2,getMeasuredWidth(),getMeasuredHeight(),new int[]{Color.BLACK,Color.BLUE,Color.GREEN},null, Shader.TileMode.CLAMP);//第六个参数的取值范围是0到1的浮点数,代表了颜色所占的比重
    }
 @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
        int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
        int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);

        if (widthSpecMode == MeasureSpec.AT_MOST && heightSpecMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(500, 500);
        } else if (widthSpecMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(500, heightSpecSize);
        } else if (heightSpecMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(widthSpecSize, 500);
        }
    }
}

结合Matrix实现闪动的字

public class ZiDingYiView extends View {
    private Paint mPaint;
    private LinearGradient linearGradient;
    private Matrix matrix;
    private int weiYi;
    public ZiDingYiView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        mPaint.setStyle(Paint.Style.FILL);
    }
 @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        weiYi+=getMeasuredWidth()/5;
        if(weiYi>getMeasuredWidth()){
            weiYi=-getMeasuredWidth();
        }
        matrix.setTranslate(weiYi,0);
        linearGradient.setLocalMatrix(matrix);
        mPaint.setShader(linearGradient);
        mPaint.setTextSize(60);
        canvas.drawRect(0,0,width/2,height/2,mPaint);
        canvas.drawText("我是自定义字体",width/2,height/2,mPaint);
        postInvalidateDelayed(100);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        linearGradient=new LinearGradient(getMeasuredWidth()/2,getMeasuredHeight()/2,getMeasuredWidth(),getMeasuredHeight(),new int[]{Color.BLACK,Color.BLUE,Color.GREEN},null, Shader.TileMode.CLAMP);//第六个参数的取值范围是0到1的浮点数,代表了颜色所占的比重
        matrix=new Matrix();
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
        int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
        int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);

        if (widthSpecMode == MeasureSpec.AT_MOST && heightSpecMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(500, 500);
        } else if (widthSpecMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(500, heightSpecSize);
        } else if (heightSpecMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(widthSpecSize, 500);
        }
    }
}

参考文章如下:
https://www.jianshu.com/p/a9d09cb7577f
https://www.jianshu.com/p/6ab058329ca8

上一篇下一篇

猜你喜欢

热点阅读