android自定義漸變色圓角View

先看效果圖:

效果圖.png

分析這是線性漸變色,圓弧半徑剛好是高度的一半翁狐,文字居中處理,大小凌蔬,顏色可調(diào)
首先開始自定義控件的基本步驟
1露懒、style自定義屬性

<!--自定義控件屬性-->
<declare-styleable name="MyGradientRoundButton">
    <attr name="colorStart" format="reference" />
    <attr name="colorEnd" format="reference" />
    <attr name="round" format="dimension" />
    <attr name="clickEffect" format="boolean" />
    <attr name="colorPressStart" format="reference" />
    <attr name="colorPressEnd" format="reference" />
    <attr name="btnText" format="reference" />
    <attr name="btnTextSize" format="dimension" />
    <attr name="btnTextColor" format="reference" />
</declare-styleable>

注:原本我是想還有點(diǎn)擊效果的
2、繼承view核心代碼

public class MyGradientRoundButton extends View {
private int colorStart;
private int colorEnd;
private int colorPressStart;
private int colorPressEnd;
private int colorS;
private int colorE;
private String text;
private int textColor;
private float textSize;
private float round;
private boolean clickEffect;
private RectF mBackGroundRect;
private LinearGradient backGradient;

//默認(rèn)畫筆
private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private Paint mPaintText = new Paint();

public MyGradientRoundButton(Context context, AttributeSet attrs) {
    super(context, attrs);

    //獲取自定義屬性
    if (attrs != null) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyGradientRoundButton);
        colorStart = typedArray.getColor(R.styleable.MyGradientRoundButton_colorStart, getResources().getColor(R.color.btn_color_start));
        colorEnd = typedArray.getColor(R.styleable.MyGradientRoundButton_colorEnd, getResources().getColor(R.color.btn_color_end));
        round = typedArray.getDimension(R.styleable.MyGradientRoundButton_round, Utils.dip2px(context, 10));
        clickEffect = typedArray.getBoolean(R.styleable.MyGradientRoundButton_clickEffect, false);
        colorPressStart = typedArray.getColor(R.styleable.MyGradientRoundButton_colorPressStart, getResources().getColor(R.color.btn_color_PStart));
        colorPressEnd = typedArray.getColor(R.styleable.MyGradientRoundButton_colorPressEnd, getResources().getColor(R.color.btn_color_PEnd));
        text = typedArray.getString(R.styleable.MyGradientRoundButton_btnText);
        textColor = typedArray.getColor(R.styleable.MyGradientRoundButton_btnTextColor, getResources().getColor(R.color.black));
        textSize = typedArray.getDimension(R.styleable.MyGradientRoundButton_btnTextSize, 16);
        colorS = colorStart;
        colorE = colorEnd;
        typedArray.recycle();
    }
    //必須加砂心,否則onTouchEvent只響應(yīng)ACTION_DOWN
    setClickable(true);
    //設(shè)置抗鋸齒
    mPaint.setAntiAlias(true);
    //設(shè)置防抖動(dòng)
    mPaint.setDither(true);
    mPaint.setStyle(Paint.Style.FILL);

    mPaintText.setAntiAlias(true);
    mPaintText.setDither(true);
}


@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    mBackGroundRect = new RectF(0, 0, w, h);
    backGradient = new LinearGradient(0, 0, w, 0, new int[]{colorS, colorE}, null, Shader.TileMode.CLAMP);
}


@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    mPaint.setShader(backGradient);
    //繪制背景 圓角矩形
    if (mBackGroundRect != null) {
        canvas.drawRoundRect(mBackGroundRect, round, round, mPaint);
    }
    //繪制文字
    mPaintText.setTextSize(textSize);
    mPaintText.setColor(textColor);
    mPaintText.setTextAlign(Paint.Align.CENTER);
    Paint.FontMetricsInt fontMetrics = mPaintText.getFontMetricsInt();
    float baseline = mBackGroundRect.top + (mBackGroundRect.bottom - mBackGroundRect.top - fontMetrics.bottom + fontMetrics.top) / 2 - fontMetrics.top;
    canvas.drawText(text, canvas.getWidth() / 2, baseline, mPaintText);

}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (clickEffect) {
        //刷新
        invalidate();
        //判斷點(diǎn)擊操作
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                colorS = colorPressStart;
                colorE = colorPressEnd;
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
                colorS = colorStart;
                colorE = colorEnd;
                break;
            case MotionEvent.ACTION_CANCEL:
                break;
        }
    }

    return super.onTouchEvent(event);
}


public void setText(String text) {
    if (!TextUtils.isEmpty(text)) {
        this.text = text;
        invalidate();
    }
}}

關(guān)鍵代碼講解:
onDraw():繪制圖形過程懈词,canvas.drawXXX()就是在繪制一層層圖層,所以drawRoundRect要在drawText之前
LinearGradient:線性漸變色
RectF:正方形辩诞,配合drawRoundRect就可以畫出帶圓角的圖形
onSizeChanged():發(fā)生在onDraw()之前坎弯,一般可以做些不變的參數(shù)設(shè)定,調(diào)用onDraw(),它也不調(diào)用的
invalidate():每調(diào)一次,也就調(diào)用了一次onDraw()方法荞怒,這樣就可以做點(diǎn)擊效果了

3、引用列子

<com.yiban1314.yiyue.widge.MyGradientRoundButton
    android:id="@+id/dialog_guide_reg"
    android:layout_width="match_parent"
    android:layout_height="@dimen/d80px"
    app:clickEffect="false"
    app:round="@dimen/d40px"
    app:colorStart="@color/btn_color_start"
    app:colorEnd="@color/btn_color_end"
    app:btnText="@string/register"
    app:btnTextColor="@color/c_22"
    app:btnTextSize="@dimen/s32px"
    android:layout_marginBottom="@dimen/d64px"
    android:layout_marginTop="@dimen/d64px"/>

這個(gè)自定義控件有個(gè)缺陷寬高不能用wrap_content屬性秧秉。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末褐桌,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子象迎,更是在濱河造成了極大的恐慌荧嵌,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,464評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件砾淌,死亡現(xiàn)場(chǎng)離奇詭異啦撮,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)汪厨,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,033評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門赃春,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人劫乱,你說我怎么就攤上這事织中。” “怎么了衷戈?”我有些...
    開封第一講書人閱讀 169,078評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵狭吼,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我殖妇,道長(zhǎng)刁笙,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,979評(píng)論 1 299
  • 正文 為了忘掉前任谦趣,我火速辦了婚禮疲吸,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘蔚润。我一直安慰自己磅氨,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,001評(píng)論 6 398
  • 文/花漫 我一把揭開白布嫡纠。 她就那樣靜靜地躺著烦租,像睡著了一般。 火紅的嫁衣襯著肌膚如雪除盏。 梳的紋絲不亂的頭發(fā)上叉橱,一...
    開封第一講書人閱讀 52,584評(píng)論 1 312
  • 那天,我揣著相機(jī)與錄音者蠕,去河邊找鬼窃祝。 笑死,一個(gè)胖子當(dāng)著我的面吹牛踱侣,可吹牛的內(nèi)容都是我干的粪小。 我是一名探鬼主播大磺,決...
    沈念sama閱讀 41,085評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼探膊!你這毒婦竟也來(lái)了杠愧?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,023評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤逞壁,失蹤者是張志新(化名)和其女友劉穎流济,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體腌闯,經(jīng)...
    沈念sama閱讀 46,555評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡绳瘟,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,626評(píng)論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了姿骏。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片糖声。...
    茶點(diǎn)故事閱讀 40,769評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖工腋,靈堂內(nèi)的尸體忽然破棺而出姨丈,到底是詐尸還是另有隱情,我是刑警寧澤擅腰,帶...
    沈念sama閱讀 36,439評(píng)論 5 351
  • 正文 年R本政府宣布蟋恬,位于F島的核電站,受9級(jí)特大地震影響趁冈,放射性物質(zhì)發(fā)生泄漏歼争。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,115評(píng)論 3 335
  • 文/蒙蒙 一渗勘、第九天 我趴在偏房一處隱蔽的房頂上張望沐绒。 院中可真熱鬧,春花似錦旺坠、人聲如沸乔遮。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,601評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)蹋肮。三九已至,卻和暖如春璧疗,著一層夾襖步出監(jiān)牢的瞬間坯辩,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,702評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工崩侠, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留漆魔,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 49,191評(píng)論 3 378
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像改抡,于是被迫代替她去往敵國(guó)和親矢炼。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,781評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,318評(píng)論 25 707
  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個(gè)線程阿纤,因...
    小菜c閱讀 6,449評(píng)論 0 17
  • 不少人嘆氣時(shí)會(huì)想裸删,要是我中了500萬(wàn)就好了。當(dāng)然這是十年前阵赠,現(xiàn)在大概得1000萬(wàn)才夠。 那么肌稻,中了彩票會(huì)如何改變?nèi)?..
    不喜歡水的魚閱讀 413評(píng)論 0 0
  • 1清蚀、 統(tǒng)計(jì)局:5月,官方制造業(yè)PMI51.2持平爹谭,連續(xù)8個(gè)月位于51以上的擴(kuò)張區(qū)間枷邪;非制造業(yè)PMI54.5,高于去...
    每天一點(diǎn)典閱讀 329評(píng)論 0 0
  • 今天我打了兩個(gè)松果诺凡,一個(gè)大的东揣、一個(gè)小的。我放在妹妹面前讓她分松果腹泌。我以為她會(huì)把大的給自己嘶卧,小的給我。結(jié)果...
    許諾123456閱讀 338評(píng)論 1 3