安卓凹凸自定义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就可以了,这里写多了。。。)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值