《Android群英传》-代码01

版权声明:本文为博主原创文章,欢迎转载,转载请注明出处。 https://blog.csdn.net/qq_18945757/article/details/86532946
  1. 自定义控件(背景):
        public class TextView_01 extends android.support.v7.widget.AppCompatTextView {
        //先继承原生的TextView
        public TextView_01(Context context) {
            super(context);
        }
    
        public TextView_01(Context context, AttributeSet attrs) {
            super(context,attrs);
        }
    
        public TextView_01(Context context, AttributeSet attrs, int defStyle) {
            super(context,attrs,defStyle);
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
    
            //设置画笔
            Paint mPaint1 = new Paint();
            mPaint1.setColor(getResources().getColor(android.R.color.holo_blue_light));
            mPaint1.setStyle(Paint.Style.FILL);
    
            Paint mPaint2 = new Paint();
            mPaint2.setColor(Color.YELLOW);
            mPaint2.setStyle(Paint.Style.FILL);
    
            //画矩形
            canvas.drawRect(0, 0,getMeasuredWidth(),getMeasuredHeight(),mPaint1);
            canvas.drawRect(20,20,getMeasuredWidth()-20,getMeasuredHeight()-20,mPaint2);
            canvas.save();//保存
            canvas.translate(20,0);//往前平移10像素
    
            super.onDraw(canvas);
            canvas.restore();
        }
    }

调用此自定义控件的效果如下:
在这里插入图片描述
3. 自定义控件(动态文字):

public class TextView_01 extends android.support.v7.widget.AppCompatTextView {
	//先继承原生的TextView
	
    int mViewWidth;
    Matrix mGradientMatrix;
    int mTranslate;
    LinearGradient mLinearGradient;
    Paint mPaint;

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

    public TextView_01(Context context, AttributeSet attrs) {
        super(context,attrs);
    }

    public TextView_01(Context context, AttributeSet attrs, int defStyle) { super(context,attrs,defStyle); }

    @Override
    protected void onDraw(Canvas canvas) {

        //设置画笔->用于画背景的
        Paint mPaint1 = new Paint();
        mPaint1.setColor(getResources().getColor(android.R.color.holo_blue_light));
        mPaint1.setStyle(Paint.Style.FILL);

        Paint mPaint2 = new Paint();
        mPaint2.setColor(Color.YELLOW);
        mPaint2.setStyle(Paint.Style.FILL);

        //画矩形->背景
        canvas.drawRect(0, 0,getMeasuredWidth(),getMeasuredHeight(),mPaint1);
        canvas.drawRect(20,20,getMeasuredWidth()-20,getMeasuredHeight()-20,mPaint2);
        canvas.save();//保存

        canvas.translate(10,0);//平移当前的矩阵(=平移画布),为接下来画文字做准备

        super.onDraw(canvas);
        if (mGradientMatrix != null){//此模块设置接下来要用到的平移矩阵,并把此矩阵用于LinearGradient中
            mTranslate += mViewWidth / 5;
            if (mTranslate > 2*mViewWidth){
                mTranslate = -mViewWidth;
            }
            mGradientMatrix.setTranslate(mTranslate,0);//矩阵平移长度设置,=平移速度
            mLinearGradient.setLocalMatrix(mGradientMatrix);//通过矩阵方式来不断平移渐变效果
            postInvalidateDelayed(100);//设置延迟,=更新时间
        }

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {//绘图时会先触发此方法,然后触发onDraw()方法
        super.onSizeChanged(w, h, oldw, oldh);
        if (mViewWidth == 0){
            mViewWidth = getMeasuredWidth();
            if (mViewWidth > 0){
                mPaint = getPaint();//获得当前绘制TextView的Paint对象->绘制文字的画笔
                mLinearGradient = new LinearGradient(0,0,mViewWidth,0,new int[]{
                        Color.BLUE,Color.RED,Color.YELLOW,Color.BLUE},null, Shader.TileMode.CLAMP);
                mPaint.setShader(mLinearGradient);//给当前Paint对象设置LinearGradient属性
                mGradientMatrix = new Matrix();
            }
        }
    }
}

调用此自定义控件的效果如下:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_18945757/article/details/86532946