編輯:關於 Android 程式設計
新建於 drawable 目錄下,示例如下:
各屬性含義如下:
android:antialias="true" android:dither="true" android:filter="true" android:gravity="center" android:mipMap="false" android:src="@drawable/ic_launcher" android:tileMode="disabled" />
上述排版僅為方便查看,不能直接複製使用。
基本示例代碼:
各標簽詳細說明:
<shape> 表示圖形形狀,有 rectangle(矩形)、oval(橢圓)、line(橫線)、ring(圓環)四種,默認矩形。使用 line 和 ring 時需通過 stroke 指定線寬與顏色,否則顯示異常。設置 ring 時有五個特殊屬性:innerRadius(內半徑,與 innerRadiusRatio 同時存在時以前者為準)、thickness(圓環厚度,即外半徑減內半徑)、innerRadiusRatio(內半徑占寬度比例,默認 9)、thicknessRatio(厚度占比例,默認 3,厚度=寬度/n)、useLevel(一般為 false,除非作為 LevelListDrawable 使用)。
<corners> 表示四個角的角度,僅適用於矩形,用 px 表示圓角程度。radius 為四角統一設置角度但優先級較低,topLeftRadius、topRightRadius、bottomLeftRadius、bottomRightRadius 分別對應四個角。
<gradient> 表示漸變填充,與 solid 互斥。屬性包括:angle(漸變角度,默認 0,必須為 45 的倍數)、centerX/centerY(漸變中心點座標)、startColor/centerColor/endColor(起止與中間色)、gradientRadius(漸變半徑)、useLevel(一般為 false)、type(linear 線性、radial 徑向、sweep 掃描線,默認線性)。
<solid> 用於純色填充,通過 android:color 指定填充顏色。
<stroke> 為描邊屬性:width(描邊寬度)、color(描邊顏色)、dashWidth(虛線線段寬度)、dashGap(線段間隔)。
<padding> 為內邊距,包含 top、bottom、left、right 四個屬性,用法與在 view 中加邊距類似。
<size> 包含 android:width 與 android:height,表示 view 的寬高,會視情況拉伸。
它是多個 Drawable 的集合,類似圖層概念,通過將不同 Drawable 放在不同層上達到疊加效果,語法如下:
-
一個 layer-list 可包含多個 item,每個 item 代表一個 Drawable。上下左右四個屬性表示 Drawable 相對 view 的偏移量(像素),可用 drawable 直接引用資源,也可在 item 下自定義 Drawable。默認所有 Drawable 都會被縮放到 view 大小;由於存在層次概念,下面的 Drawable 會覆蓋上面的,合理分層即可實現特殊的疊加效果。
選擇器對應 <selector> 標簽,也是 Drawable 的集合,常用於根據當前狀態改變顏色:
android:constantSize="true" android:dither="true" android:variablePadding="true"> - android:drawable="@drawable/ic_launcher" android:state_pressed="true" android:state_focused="true" android:state_hovered="true" android:state_selected="true" android:state_checkable="true" android:state_checked="true" android:state_enabled="true" android:state_activated="true" android:state_window_focused="true">
注意:若有多個 item,程序會從上到下匹配,最先匹配者生效(並非最佳匹配)。沒有任何狀態說明的 item 可被任意狀態匹配,因此默認 item 應放在最後一條,否則會導致下面的選項失效。
LevelListDrawable 對應 <level-list> 標簽,集合中每個 Drawable 都有一個等級,根據不同等級切換為對應的 Drawable:
- android:drawable = "@drawable/ic_lunch" android:maxLevel = "2" android:minLevel = "1">
用於實現兩個 Drawable 之間的淡入淡出效果,使用方法如下:
定義漸變:
-
-
將漸變添加到背景:
在代碼中控制:
TextView textView = (TextView)findViewById(R.id.button); TransitionDrawable drawable = (TransitionDrawable)textView.getBackground(); drawable.startTransition(1000);
可將 Drawable 內嵌到自己內部並在四周留出空隙,常用於希望背景比實際區域小的 view:
android:drawable="@drawable/ic_launcher" android:insetBottom="2dp" android:insetLeft="2dp" android:insetRight="2dp" android:insetTop="2dp" >
對應代碼如下:
android:drawable="@drawable/ic_launcher" android:scaleGravity="center" android:scaleHeight="50%" android:scaleWidth="50%" >
使用時必須設置 Drawable 的級別,否則無法生效:
View testScale = findViewById(R.id.test_scale); scaleDrawable testScaleDrawable = (ScaleDrawable)testScale.getBackground(); testScaleDrawable.setLevel(1);//范圍是1~10000,只要不為零就可以
可根據當前等級裁剪另一個 Drawable:
android:clipOrientation="horizontal" android:drawable="@+id/icon" android:gravity="top" >
gravity 取值說明:top(頂部,豎直裁剪時從底部開始裁)、bottom(底部,從頂部開始裁)、left(左邊,水平裁剪時從右開始裁,默認)、right(右邊,從左邊開始裁)、center_vertical(豎直居中,上下同時裁)、fill_vertical(豎直填充,等級為 0 時才有裁剪行為)、center(中部居中)、fill(充滿整個 Drawable,等級為 0 時有效)、clip_vertical/clip_horizontal(附加選項,表示豎直/水平方向裁剪)。水平方向也有 center_horizontal 和 fill_horizontal 等對應屬性。
使用時需在代碼中設置等級:
ImageViwe testClip = (ImageView)findViewById(R.id.test_clip); ClipDrawable testClipDrawable = (ClipDrawable)testClip.getDrawable(); //等級為從0到10000,等級0表示完全裁剪,等級10000表示不裁剪 testClipDrawable.setLevel(5000);
Android 資料庫實戰筆記(二):建表語句與 LitePal 快速上手
上一篇我們把 SQLite 的常用命令摸了一遍,不過那些操作都是在命令行裡完成的。數據庫終究要回到程式碼裡才有價值,單獨對一個 .db 檔增刪改查,練習意義大於實用意義。
Android 屬性動畫實現衛星式動態菜單
Android 動態菜單 先上效果圖: 點一下主按鈕,幾個子菜單項沿著一段圓弧散開,帶著縮放與淡入,收起的時候再沿原路徑回去。看著複雜,核心其實只有兩件事:屬性動畫
Android 自定義圖片選擇元件:相冊、拍照、裁剪與刪除動畫
元件目標 本文實作一個類似社群貼文選圖的自定義元件,支援從相冊選擇圖片、拍照取得圖片、長按顯示刪除按鈕、動畫提示,以及達到數量上限後隱藏新增按鈕。 加號圖片永遠在最
Android 使用 HttpURLConnection 下載圖片
談到 http,就不得不先理清 URI 與 URL 這兩個概念。URI(uniform resource identifier)是統一資源標識符;URL(uniform