天天看點

安卓凹凸自定義View

這個是産品的效果圖

然後實際運作的結果

那到這個需求感覺還是很簡單的,讓美術出了一張圖,然後我把這個背景圖做成了.9圖,然而,并沒有什麼卵用,最大的原因就是background被拉伸、擠壓,高度在不同的機型顯示的不一樣,但是圖檔的半圓缺角是不變的,是以想想還是寫個View。

自定義屬性設定顔色背景

 public class CouponTextView extends TextView {

    private Paint mPaint;

    private Context mContext;

    private int mColor;

    public CouponTextView(Context context) {

        this(context, null);

    }

public CouponTextView(Context  context, AttributeSet attrs) {

this(context, attrs, 0);

public CouponTextView(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CouponTextView);

        mColor = ContextCompat.getColor(context, R.color.title_orange);

        mColor = array.getColor(R.styleable.CouponTextView_bg_color, mColor);

        mContext = context;

        initPaint();

        array.recycle();

private void initPaint() {

mPaint =new Paint();

        mPaint.setColor(mColor);

        mPaint.setStrokeWidth(12f);

        mPaint.setAntiAlias(true);

@Override

    protected void onDraw(Canvas canvas) {

RectF rectf =new RectF(0, 0, getMeasuredWidth(), getMeasuredHeight());

        canvas.drawRect(rectf, mPaint);

        mPaint.setColor(ContextCompat.getColor(mContext, R.color.white));

      canvas.drawCircle(0, 50,20, mPaint);

        super.onDraw(canvas);

}

代碼非常簡單,不作解釋,很久沒有發文,先水一篇,哈哈哈

ps(再改一下,其實這個背景顔色沒必要設定,畫個半圓就可以了,背景顔色直接設定backGround就可以了,這裡寫多了。。。)

繼續閱讀