Android下基於XML的 Graphics
以前作圖,一般有兩種方式,首先是UI把圖形設計好,我們直接貼,對於那些簡單的圖形,如矩形、扇形這樣的圖
形,一般的系統的API會提供這樣的接口,但是在Android下,有第三種畫圖方式,介於二者之間,結合二者的長處,如
下的代碼:
Java 代碼
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#0055ff88"
android:centerColor="#0055ff00"
android:centerY="0.75"
android:endColor="#00320077"
android:angle="270"
/>
</shape>
</clip>
</item>
這是一個Progress的style裏面的代碼,描述的是進度條的爲達到的圖形,原本以爲這是一個圖片,後來仔細的跟蹤代碼,
發現居然是 xml,像這種shape corners gradient等等這還是第一次碰到。shape 表示是一個圖形,corners表示是有半徑
爲5像素的圓角,然後,gradient表示一個漸變。這樣作圖簡單明瞭,並且可以做出要求很好的圖形,並且節省資源
Java 代碼
<shape xmlns:android="
http://schemas.android.com/apk/res/android
" android:shape="rectangle">
<gradient android:startColor="#FFFF0000" android:endColor="#80FF00FF"
android:angle="270"/>
<padding android:left="50dp" android:top="20dp"
android:right="7dp" android:bottom="7dp" />
<corners android:radius="8dp" />
</shape>
gradient 產生顏色漸變 android:angle 從哪個角度開始變 貌似只有90的整數倍可以
android:shape="rectangle" 默認的也是長方形
Java 代碼
<shape xmlns:android="
http://schemas.android.com/apk/res/android
" android:shape="oval">
<solid android:color="#ff4100ff"/>
<stroke android:width="2dp" android:color="#ee31ff5e"
android:dashWidth="3dp" android:dashGap="2dp" />
<padding android:left="7dp" android:top="7dp"
android:right="7dp" android:bottom="7dp" />
<corners android:radius="6dp" />
</shape>
#ff4100ff藍色#ff4100ff綠色
<solid android:color="#ff4100ff"/>實心的 填充裏面
<stroke 描邊 採用那樣的方式將外形輪廓線畫出來
android:dashWidth="3dp" android:dashGap="2dp" 默認值爲0
android:width="2dp" android:color="#FF00ff00"筆的粗細,
android:dashWidth="5dp" android:dashGap="5dp" 實現- - -這樣的效果,dashWidth指的是一條小橫線的寬度
dashGap 指的是 小橫線與小橫線的間距。 width="2dp" 不能太寬
shape等特殊xml
1.用 shape 作爲背景
<shape xmlns:android="
http://schemas.android.com/apk/res/android
">
<solid android:color="#f0600000"/>
<stroke android:width="3dp" color="#ffff8080"/>
<corners android:radius="3dp" />
<padding android:left="10dp" android:top="10dp"
android:right="10dp" android:bottom="10dp" />
</shape>
一定要注意solid android:color="#f0600000" 是背景色 要用8位 最好不要完全透明不然沒有效果啊 這句話本來就不
是背景色 的意思
2.類似多選的效果:
(1) listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setItemsCanFocus(false);
(2) define list item
CheckedTextView xmlns:android="
http://schemas.android.com/apk/res/android
"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:background="@drawable/txt_view_bg"/>
(3) define drawable txt_view_bg.xml <item
android:drawable="@drawable/selected" android:state_checked="true"
/> <item android:drawable="@drawable/not_selected" />
3.
<LinearLayout android:layout_width ="100dp" android:layout_height="wrap_content" />
LinearLayour ll = new LinearLayout(this);parentView.addView(ll, new LinearLayout.LayoutParams(100, LayoutParams.WRAP_CONTENT));
4. 當設置 TextView setEnabled(false)時 背景顏色你如果用#ffff之類的話可能不會顯示 你最好使用 android:textColor這個屬性而不是使用color。
<TextView android:text="whatever text you want"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/example" />
res/color/example.xml):
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android=" http://schemas.android.com/apk/res/android "> <item android:state_enabled="false" android:color="@color/disabled_color" /> <item android:color="@color/normal_color"/></selector>
http://developer.android.com/intl/zh-CN/reference/android/content/res/ColorStateList.html
5.
http://android.amberfog.com/?p=9
<layer-list xmlns:android="
http://schemas.android.com/apk/res/android
">
<item>
<shape>
<solid android:color="#FFF8F8F8" />
</shape>
</item>
<item android:top="23px">
<shape>
<solid android:color="#FFE7E7E8" />
</shape>
</item>
</layer-list>
You can simple combine several drawables into one using <layer-list> tag.
note: Unfortenately you cannot resize drawables in layer-list. You can only move it.
<layer-list xmlns:android=" http://schemas.android.com/apk/res/android "> <item android:drawable="@drawable/shape_below"/> <item android:top="10px" android:right="10px" android:drawable="@drawable/shape_cover"/></layer-list>
include
You can put similar layout elements into separate XML and use <include> tag to use it.
<RelativeLayout xmlns:android="
http://schemas.android.com/apk/res/android
"
android:layout_width="fill_parent"
android:layout_height="64dip"
android:gravity="center_vertical"
android:ignoreGravity="@+id/icon">
<include layout="@layout/track_list_item_common" />;
</RelativeLayout>
track_list_item_common.xml
<merge xmlns:android="
http://schemas.android.com/apk/res/android
">
<ImageView android:id="@+id/icon"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="4dip"
android:layout_width="60px"
android:layout_height="60px"/>
...
</merge>