android shape绘制常用的形状

shape是一个强大的东西,除了用代码绘制图形外,我们还可以用shape通过xml来绘制一些常见的控件背景之类的,非常方便,如下:


android shape绘制常用的形状_第1张图片
QQ截图20180308175605.png

1、纯色圆和空心圆shape:

纯色圆:



    
    

空心圆:



    

调用:



    

2、纯色矩形和空心矩形shape:

纯色矩形:



    

空心矩形:



    
    

    
    


调用:



    

3、圆角矩形shape

圆角矩形:



    
    

圆角空心:



    
    

    
    
    


4、半角圆角矩形

实心:



    
    


空心:



    
    

    
    
    


5、变色背景:



    
    
    


6、变色文字:

控件自定义:

/**
 * Created by yy on 2018/3/8.
 * 描述:
 */

public class ColorTextView extends AppCompatTextView {
    private int mViewWidth;
    private Paint mPaint;
    private LinearGradient mLinearGradient;
    private Matrix mMatrix;
    private int mTranslate;

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

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

    public ColorTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        // TODO Auto-generated method stub
        super.onSizeChanged(w, h, oldw, oldh);
        if (mViewWidth == 0) {
            mViewWidth = getMeasuredWidth();
        }
        if (mViewWidth > 0) {
            mPaint = getPaint();
            mLinearGradient = new LinearGradient(
                    0,
                    0,
                    mViewWidth,
                    0,
                    new int[]{Color.BLUE, Color.BLACK, Color.RED, Color.YELLOW},
                    null, Shader.TileMode.MIRROR);
            mPaint.setShader(mLinearGradient);
            mMatrix = new Matrix();
        }

    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        if (mMatrix != null) {
            mTranslate += mViewWidth / 5;
            if (mTranslate > 2 * mViewWidth) {
                mTranslate = -mViewWidth;
            }
            mMatrix.setTranslate(mTranslate, 0);
            mLinearGradient.setLocalMatrix(mMatrix);
            postInvalidateDelayed(100);
        }
    }

}

调用:


github:
android shape绘制常用的形状

你可能感兴趣的:(android shape绘制常用的形状)