最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android中快速便捷的實(shí)現(xiàn)圓角按鈕方法詳解

 更新時(shí)間:2017年05月18日 11:26:56   作者:賊寇  
圓角按鈕在我們現(xiàn)在的界面中常常會(huì)用到,最近在開(kāi)發(fā)中就又遇到了,所以想著有沒(méi)有更快速更便捷的實(shí)現(xiàn)方法呢,所以就有了這篇文章,本文主要給大家介紹了關(guān)于A(yíng)ndroid中如何快速便捷的實(shí)現(xiàn)圓角按鈕的相關(guān)資料,需要的朋友可以參考下。

前言

大家應(yīng)該都知道,圓角按鈕是我們?cè)谧鼋缑鏁r(shí)常常遇到的UI樣式。通常的辦法,是做一個(gè)drawable,比如這樣:

<?xml version="1.0" encoding="UTF-8"?> 
<shape 
 xmlns:android="http://schemas.android.com/apk/res/android" 
 android:shape="rectangle"> 
 <!-- 填充的顏色 --> 
 <solid android:color="#ffae00" /> 

 <!-- 圓角的半徑 --> 
 <corners android:radius="10dp" /> 
</shape>

在Layout文件里Button的background屬性設(shè)上這個(gè)drawable.xml,就可以了。

然而這樣做的話(huà),每次弄個(gè)按鈕都得新做一個(gè)drawable文件,各種drawable多了看著就亂。

是不是可以把color和radius放到Button的屬性里去,這樣就不用每次再拖一個(gè)drawable.xml了是吧?

自定義RoundCornerButton

<widget.RoundCornerButton
 android:id="@+id/btn_commit"
 android:layout_width="100dp"
 android:layout_height="40dp"
 android:gravity="center"
 android:text="我的按鈕"
 app:rcb_backgroundColor="@color/yellow"
 app:rcb_backgroundColorDisabled="@color/light_grey"
 app:rcb_cornerRadius="20dp"
 />

如果可以這樣在Layout文件里直接設(shè)置背景色和圓角半徑,是不是很方便!雖然不如drawable靈活,但是已經(jīng)足以應(yīng)付設(shè)計(jì)同學(xué)給出的圓角按鈕的需求了。

我們就以定義自己的styleable屬性開(kāi)始吧

<declare-styleable name="RoundCornerButton">
 <attr name="rcb_backgroundColor" format="color" />
 <attr name="rcb_backgroundColorDisabled" format="color" />
 <attr name="rcb_cornerRadius" format="dimension" />
</declare-styleable>

從Drawable擴(kuò)展一個(gè)自己的Drawable,很簡(jiǎn)單

  • 從構(gòu)造方法傳入color和radius,并創(chuàng)建一個(gè)實(shí)習(xí)的畫(huà)筆;
  • 覆寫(xiě)draw方法,有現(xiàn)成的畫(huà)圓角矩形的方法可以調(diào)用;
  • 暴露一個(gè)setRect方法給外邊,用于設(shè)置繪制區(qū)域的寬高。
class RoundCornerDrawable extends Drawable {

 final int color;
 final float radius;
 final Paint paint;
 final RectF rectF;

 RoundCornerDrawable(int color, float radius) {
  this.color = color;
  this.radius = radius;

  // 實(shí)心的畫(huà)筆
  this.paint = new Paint();
  paint.setStyle(Paint.Style.FILL);
  paint.setAntiAlias(true);
  paint.setColor(color);

  this.rectF = new RectF();
 }

 // 用于設(shè)置Drawable寬高
 public void setRect(int width, int height) {
  this.rectF.left = 0;
  this.rectF.top = 0;
  this.rectF.right = width;
  this.rectF.bottom = height;
 }

 @Override
 public void draw(@NonNull Canvas canvas) {
  canvas.drawRoundRect(rectF, radius, radius, paint); // 畫(huà)圓角矩形,現(xiàn)成的方法
 }

 // 其余方法略
}

定義自己的Button類(lèi),有這么幾個(gè)要點(diǎn):

  • 與通常的自定義View一樣,覆寫(xiě)三個(gè)構(gòu)造方法;
  • 從AttributeSet里讀取自定義的屬性backgroundColor和cornerRadius,這里暫時(shí)忽略backgroundColorDisabled;
  • 每一種狀態(tài)(例如普通、禁用、按下)是一個(gè)RoundCornerDrawable,組合成一個(gè)StateListDrawable;
  • onLayout的時(shí)候,記得改變每一個(gè)RoundCornerDrawable的尺寸。
public class RoundCornerButton extends AppCompatButton {

 private int colorNormal;
 private float cornerRadius;
 private RoundCornerDrawable bgDrawableNormal = null;

 // 省略三個(gè)構(gòu)造方法
 // 構(gòu)造方法最后一定要調(diào)用initCornerBackground完成初始化

 private void initCornerBackground(AttributeSet attrs, int defStyleAttr) {
  TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.RoundCornerButton, defStyleAttr, 0);

  this.cornerRadius = a.getDimension(R.styleable.RoundCornerButton_rcb_cornerRadius, 0);
  this.colorNormal = a.getColor(R.styleable.RoundCornerButton_rcb_backgroundColor, 0);
  makeBackgroundDrawable();

  a.recycle();
 }

 private void makeBackgroundDrawable() {
  bgDrawableNormal = new RoundCornerDrawable(this.colorNormal, this.cornerRadius);
  bgDrawableNormal.setRect(getWidth(), getHeight());

  // 設(shè)計(jì)通常會(huì)給出禁用時(shí)的樣式以及按下時(shí)的樣式
  // 所以這里用使用StateListDrawable
  StateListDrawable bgDrawable = new StateListDrawable();
  bgDrawable.addState(new int[]{android.R.attr.state_enabled, -android.R.attr.state_pressed}, bgDrawableNormal);
  // 每多一種狀態(tài),在這里多加一項(xiàng)
  setBackgroundDrawable(bgDrawable);
 }

 @Override
 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
  super.onLayout(changed, left, top, right, bottom);

  // layout之后必然會(huì)draw,所以在這里設(shè)置drawable的尺寸
  if (bgDrawableNormal != null) {
   bgDrawableNormal.setRect(right - left, bottom - top);
  }
  // 每多一種狀態(tài),在這里多加一項(xiàng)
 }
}

附上Demo源代碼:https://github.com/realxu/CodeSamples/tree/master/Android/RoundCornerButtonDemo

這就可以啦,我們看看效果:

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

  • Android自定義view實(shí)現(xiàn)列表內(nèi)左滑刪除Item

    Android自定義view實(shí)現(xiàn)列表內(nèi)左滑刪除Item

    這篇文章主要介紹了微信小程序列表中item左滑刪除功能,本文分步驟給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-02-02
  • Android中RecyclerView實(shí)現(xiàn)分頁(yè)滾動(dòng)的方法詳解

    Android中RecyclerView實(shí)現(xiàn)分頁(yè)滾動(dòng)的方法詳解

    RecyclerView實(shí)現(xiàn)滾動(dòng)相信對(duì)大家來(lái)說(shuō)都不陌生,但是本文主要給大家介紹了利用Android中RecyclerView實(shí)現(xiàn)分頁(yè)滾動(dòng)的思路和方法,可以實(shí)現(xiàn)翻頁(yè)功能,一次翻一頁(yè),也可以實(shí)現(xiàn)翻至某一頁(yè)功能。文中給出了詳細(xì)的示例代碼,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2017-04-04
  • Flutter?異步編程之單線(xiàn)程下異步模型圖文示例詳解

    Flutter?異步編程之單線(xiàn)程下異步模型圖文示例詳解

    這篇文章主要為大家介紹了Flutter?異步編程之單線(xiàn)程下異步模型圖文示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • Android實(shí)現(xiàn)從緩存中讀取圖片與異步加載功能類(lèi)

    Android實(shí)現(xiàn)從緩存中讀取圖片與異步加載功能類(lèi)

    這篇文章主要介紹了Android實(shí)現(xiàn)從緩存中讀取圖片與異步加載功能類(lèi),涉及Android針對(duì)緩存的操作及圖片異步加載相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2016-08-08
  • Android Flutter利用貝塞爾曲線(xiàn)畫(huà)一個(gè)小海豚

    Android Flutter利用貝塞爾曲線(xiàn)畫(huà)一個(gè)小海豚

    貝塞爾曲線(xiàn)的應(yīng)用填補(bǔ)了計(jì)算機(jī)繪制與手繪之前的差距,更能表達(dá)人想畫(huà)出的曲線(xiàn)。本文就將利用貝塞爾曲線(xiàn)繪制一個(gè)可愛(ài)的小海豚,需要的可以參考一下
    2022-04-04
  • Android ReboundScrollView仿IOS拖拽回彈效果

    Android ReboundScrollView仿IOS拖拽回彈效果

    這篇文章主要為大家詳細(xì)介紹了Android ReboundScrollView仿IOS拖拽回彈效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • WAC啟動(dòng)Android模擬器 transfer error: Read-only file system錯(cuò)誤解決方法

    WAC啟動(dòng)Android模擬器 transfer error: Read-only file system錯(cuò)誤解決方法

    這篇文章主要為大家分享下WAC啟動(dòng)Android模擬器時(shí)出現(xiàn)transfer error: Read-only file system 問(wèn)題的解決方法
    2013-10-10
  • Android中dataBinding使用的簡(jiǎn)單封裝

    Android中dataBinding使用的簡(jiǎn)單封裝

    前面一段時(shí)間學(xué)習(xí)了一下Android中的DataBinding,但是只是很簡(jiǎn)單地實(shí)現(xiàn)了一下,DataBinding中最強(qiáng)大的地方還沒(méi)有認(rèn)真地學(xué)習(xí)過(guò),有很多地方還不理解,下面這篇文章主要給大家介紹了關(guān)于A(yíng)ndroid中dataBinding使用的簡(jiǎn)單封裝,需要的朋友可以參考下
    2023-06-06
  • Android中的顏色表示的詳解

    Android中的顏色表示的詳解

    這篇文章主要介紹了Android中的顏色表示的詳解的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下
    2017-10-10
  • Android studio實(shí)現(xiàn)簡(jiǎn)易的計(jì)算器功能

    Android studio實(shí)現(xiàn)簡(jiǎn)易的計(jì)算器功能

    這篇文章主要為大家詳細(xì)介紹了Android studio實(shí)現(xiàn)簡(jiǎn)易的計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05

最新評(píng)論

衡南县| 河池市| 临海市| 炎陵县| 郧西县| 象山县| 中方县| 宣武区| 宁远县| 邻水| 安图县| 随州市| 玛纳斯县| 泸西县| 罗山县| 郴州市| 崇左市| 武汉市| 家居| 尤溪县| 思茅市| 开远市| 盐亭县| 南阳市| 吉林省| 普格县| 深泽县| 定远县| 和顺县| 武功县| 周宁县| 沂水县| 黑水县| 永济市| 望奎县| 宜兴市| 含山县| 集贤县| 绥德县| 丰都县| 斗六市|