安卓凹凸自定義View

這個(gè)是產(chǎn)品的效果圖
然后實(shí)際運(yùn)行的結(jié)果

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


自定義屬性設(shè)置顏色背景


?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);

? ? }

}


代碼非常簡(jiǎn)單,不作解釋,很久沒有發(fā)文,先水一篇,哈哈哈

ps(再改一下,其實(shí)這個(gè)背景顏色沒必要設(shè)置,畫個(gè)半圓就可以了,背景顏色直接設(shè)置backGround就可以了,這里寫多了。。。)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容