快捷表徵圖有兩部分組成,一部分是應用的表徵圖,另一部分就是應用的名稱。其實Launcher中的快捷表徵圖只是繼承了TextView控制項,重繪了一下,將背景弄成淺灰色(具體是什麼顏色我也不知道)的橢圓背景,顯示的文字顏色則是白色。TextView有android:drawableTop;drawableBottom(上下左右我這裡就不全寫出來了)屬性,用來顯示應用的表徵圖。
廢話不多說了,直接上例子,大家一步一步來,多敲敲代碼,成長快一點。
第一步:建立一個Android工程,命名為ApplicationDemo.如:
第二步:在values目錄下建立colors.xml檔案,定義一些要用的顏色,代碼如下:
<?xml version="1.0" encoding="utf-8"?><br /><resources><br /> <color name="white">#FFFFFF</color><br /> <color name="black">#000000</color><br /> <color name="bubble_dark_background">#B2191919</color><br /></resources>
第三步:也就是重點了,建立一個BubbleTextView類,繼承TextView,代碼如下:
package com.tutor.application;<br />import android.content.Context;<br />import android.graphics.Canvas;<br />import android.graphics.Paint;<br />import android.graphics.RectF;<br />import android.text.Layout;<br />import android.util.AttributeSet;<br />import android.widget.TextView;<br />public class BubbleTextView extends TextView {<br /> private static final int CORNER_RADIUS = 8;<br /> private static final int PADDING_H = 5;<br /> private static final int PADDING_V = 1;<br /> private final RectF mRect = new RectF();<br /> private Paint mPaint;<br /> public BubbleTextView(Context context) {<br /> super(context);<br /> init();<br /> }<br /> public BubbleTextView(Context context, AttributeSet attrs) {<br /> super(context, attrs);<br /> init();<br /> }<br /> public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {<br /> super(context, attrs, defStyle);<br /> init();<br /> }<br /> private void init() {<br /> setFocusable(true);<br /> // We need extra padding below to prevent the bubble being cut.<br /> setPadding(PADDING_H, 0, PADDING_H, PADDING_V);<br /> mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);<br /> mPaint.setColor(getContext().getResources()<br /> .getColor(R.color.bubble_dark_background));<br /> }<br /> @Override<br /> protected void drawableStateChanged() {<br /> invalidate();<br /> super.drawableStateChanged();<br /> }<br /> @Override<br /> public void draw(Canvas canvas) {<br /> final Layout layout = getLayout();<br /> final RectF rect = mRect;<br /> final int left = getCompoundPaddingLeft();<br /> final int top = getExtendedPaddingTop();<br /> rect.set(left + layout.getLineLeft(0) - PADDING_H,<br /> top + layout.getLineTop(0) - PADDING_V,<br /> Math.min(left + layout.getLineRight(0) + PADDING_H,<br /> getScrollX() + getRight() - getLeft()),<br /> top + layout.getLineBottom(0) + PADDING_V);<br /> canvas.drawRoundRect(rect, CORNER_RADIUS, CORNER_RADIUS, mPaint);<br /> super.draw(canvas);<br /> }<br />}
第四步:修改main.xml布局檔案,代碼如下:
<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="vertical"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br /> ><br /> <TextView<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:drawableTop="@drawable/icon"<br /> android:text="ApplicationDemo"<br /> android:textColor="@color/black"<br /> /><br /> <com.tutor.application.BubbleTextView<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:drawableTop="@drawable/icon"<br /> android:textColor="@color/white"<br /> android:text="ApplicationDemo"<br /> /><br /></LinearLayout>
第五步:修改AndroidManifest.xml檔案,注意這裡我們在Activity裡增加了一個透明的樣式,Launcher其實就是透明的Activity。
代碼如下(第8行代碼):
<?xml version="1.0" encoding="utf-8"?><br /><manifest xmlns:android="http://schemas.android.com/apk/res/android"<br /> package="com.tutor.application"<br /> android:versionCode="1"<br /> android:versionName="1.0"><br /> <application android:icon="@drawable/icon" android:label="@string/app_name"><br /> <activity android:name=".ApplicationDemo"<br /> android:theme="@android:style/Theme.Wallpaper.NoTitleBar"<br /> android:label="@string/app_name"><br /> <intent-filter><br /> <action android:name="android.intent.action.MAIN" /><br /> <category android:name="android.intent.category.LAUNCHER" /><br /> </intent-filter><br /> </activity><br /> </application><br /> <uses-sdk android:minSdkVersion="7" /><br /></manifest>
第六步:運行上述工程,查看效果如下:
將android:drawableLeft修改為android:drawableTop,效果如下:
搞定!大功告成!