Android GradientDrawable(shape標簽定義) 靜態使用和動態使用(圓角,漸變實現)


Android GradientDrawable使用優勢:

  1. 快速實現一些基本圖形(線,矩形,圓,橢圓,圓環)

  2. 快速實現一些圓角,漸變,陰影等效果

  3. 代替圖片設置為View的背景

  4. 可以減少apk大小,提升用戶下載意願

  5. 還可以減少內存占用

  6. 方便修改與維護

  基於上面幾種優勢,我們很多時候都會選擇使用android的shape,下面分別介紹shape的靜態使用和動態使用

1. GradientDrawable的靜態使用(xml中使用shape標簽定義)

  在drawable中創建一個xml文件,在布局文件中直接引用這個xml文件即可

<?xml version="1.0" encoding="utf-8"?>

 <!-- android:shape=["rectangle" | "oval" | "line" | "ring"] shape的形狀,默認為矩形,可以設置為矩形(rectangle)、橢圓形(oval)、線(line)、環形(ring) 下面的屬性只有在android:shape="ring時可用: android:innerRadius 內環的半徑。 android:innerRadiusRatio 浮點型,以環的寬度比率來表示內環的半徑, 例如,如果android:innerRadiusRatio,表示內環半徑等於環的寬度除以5,這個值是可以被覆蓋的,默認為9. android:thickness 環的厚度 android:thicknessRatio 浮點型,以環的寬度比率來表示環的厚度,例如,如果android:thicknessRatio="2", 那么環的厚度就等於環的寬度除以2。這個值是可以被android:thickness覆蓋的,默認值是3. android:useLevel boolean值,如果當做是LevelListDrawable使用時值為true,否則為false. -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"        android:shape="rectangle" >
    <!--寬度和高度 android:width 整型 寬度 android:height 整型 高度 -->
    <size android:width="50dp" android:height="50dp"/>

    <!--圓角 android:radius     整型 半徑 android:topLeftRadius    整型 左上角半徑 android:topRightRadius 整型 右上角半徑 android:bottomLeftRadius 整型 左下角半徑 android:bottomRightRadius 整型 右下角半徑 -->
    <corners android:radius="10dp"/><!-- 設置圓角半徑,可以分別設置4個角 -->
    
    <!--漸變,這個設置之后一般就不要設置solid填充色了 android:startColor 顏色值 起始顏色 android:endColor 顏色值 結束顏色 android:centerColor 整型 漸變中間顏色,即開始顏色與結束顏色之間的顏色 android:angle 整型 
     漸變角度(PS:當angle=0時,漸變色是從左向右。 然后逆時針方向轉,當angle=90時為從下往上。angle必須為45的整數倍) android:type ["linear" | "radial" | "sweep"] 漸變類型(取值:linear、radial、sweep) linear 線性漸變,這是默認設置 radial 放射性漸變,以開始色為中心。 sweep 掃描線式的漸變。 android:useLevel ["true" | "false"]
     如果要使用LevelListDrawable對象,就要設置為true。設置為true無漸變。false有漸變色 android:gradientRadius 整型 漸變色半徑.當 android:type="radial" 時才使用。單獨使用 android:type="radial"會報錯。 android:centerX 整型 漸變中心X點坐標的相對位置 android:centerY 整型 漸變中心Y點坐標的相對位置
--> <gradient android:startColor="@android:color/white" android:centerColor="@android:color/black" android:endColor="@android:color/black" android:useLevel="true" android:angle="45" android:type="radial" android:centerX="0" android:centerY="0" android:gradientRadius="90"/> <!-- 間隔 內邊距,即內容與邊的距離 android:left 整型 左內邊距 android:top 整型 上內邊距 android:right 整型 右內邊距 android:bottom 整型 下內邊距 --> <padding android:left="5dp" android:top="5dp" android:right="5dp" android:bottom="5dp"/> <!--填充 android:color 顏色值 填充顏色 --> <solid android:color="@android:color/white"/><!-- 填充的顏色 --> <!--描邊 android:width 整型 描邊的寬度 android:color 顏色值 描邊的顏色 android:dashWidth 整型 表示描邊的樣式是虛線的寬度, 值為0時,表示為實線。值大於0則為虛線。 android:dashGap 整型 表示描邊為虛線時,虛線之間的間隔 即“ - - - - ” --> <stroke android:width="1dp" <!-- 邊框寬度 --> android:color="@android:color/black" android:dashWidth="1dp" android:dashGap="2dp"/> </shape>

 

2. 動態創建GradientDrawable並使用

  用shape標簽定義的xml,最終都是轉化為GradientDrawable對象,而不是ShapeDrawable, 也不是起類型對應的 OvalShape,RoundRectShape等。

  GradientDrawable可以動態設置類型如下圖所示,跟xml文件中類型android:shape的值一一對應。

 

View view = null;    // 這個view是你需要設置背景的view
int strokeWidth = 1;     // 1dp 邊框寬度
int roundRadius = 5;     // 5dp 圓角半徑
int strokeColor = Color.parseColor("#FFFF0000");//邊框顏色
int fillColor = Color.parseColor("#FF00FF00"); //內部填充顏色
 GradientDrawable gd = new GradientDrawable();//創建drawable gd.setColor(fillColor); gd.setCornerRadius(roundRadius); gd.setStroke(strokeWidth, strokeColor);
gd.setGradientType(GradientDrawable.RECTANGLE); view.setBackgroundDrawable(gd);
// 創建漸變的shape drawable int colors[] = { 0xff255779 , 0xff3e7492, 0xffa6c0cd };//分別為開始顏色,中間夜色,結束顏色 GradientDrawable gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors); view.setBackgroundDrawable(gd);

3. 動態改變GradientDrawable的屬性

  既然GradientDrawable都能動態創建,那么肯定能過動態修改,我們可以通過先獲取view上設置的background drawable

  如果是GradientDrawable則強制轉換為GradientDrawable,這個時候就可以修改里面的屬性,像動態創建時一樣設置,設置好之后重新設置給view.

GradientDrawable drawable =(GradientDrawable)view.getBackground(); drawable.setColor(fillColor); // 設置填充色 
drawable.gd.setStroke(strokeWidth, strokeColor); // 設置邊框寬度和顏色
gd.setColors(colors);    // 設置漸變顏色數組

 

總結:

  請注意區分 GradientDrawable 和 ShapeDrawable,這兩個 Drawable 官方文檔解釋都是可以使用 shape 標簽來定義,但實際使用過程卻發現使用 shape 標簽定義的 Drawable 屬於 GradientDrawabl。使用 shape 標簽能定義多種多樣的 Drawable,能夠方便實現圓角,漸變等效果,更多 shape 標簽定義請參考 Drawable實戰解析:Android XML shape 標簽使用詳解 。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM