編輯:關於 Android 程式設計
做項目時遇到一個卡券邊緣帶半圓凹凸的效果。由於擔心用圖片會出現適配問題,加上想練練自訂 View,於是決定用自訂 View 來實現。一開始還傻傻地想著用畫矩形去湊邊緣,幸好有高人指點,在此致謝。

仔細看,這個卡券其實和普通 LinearLayout、RelativeLayout 差不多,只是上下兩邊多了類似半圓鋸齒的缺口。要實現它,只要在上下兩條邊上畫一個個白色的小圓來「挖」出缺口即可。
假如半圓半徑與半圓間距固定,那麼不同寬度的螢幕能畫出的半圓數量就不同,因此需要依據控制項寬度計算出可畫的圓數量。觀察圖片不難發現,圓的數量總是「圓間距數量 − 1」,也就是說若圓數量為 circleNum,則圓間距為 circleNum + 1。由此可推得:
circleNum = (int) ((w-gap)/(2*radius+gap));
其中 gap 是圓間距、radius 是圓半徑、w 是 view 寬度。
public class CouponDisplayView extends LinearLayout { private Paint mPaint; /** * 圓間距 */ private float gap = 8; /** * 半徑 */ private float radius = 10; /** * 圓數量 */ private int circleNum; private float remain; public CouponDisplayView(Context context) { super(context); } public CouponDisplayView(Context context, AttributeSet attrs) { super(context, attrs); mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mPaint.setDither(true); mPaint.setColor(Color.WHITE); mPaint.setStyle(Paint.Style.FILL); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); if (remain==0){ remain = (int)(w-gap)%(2*radius+gap); } circleNum = (int) ((w-gap)/(2*radius+gap)); } public CouponDisplayView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } 這裡定義了圓半徑與圓間距,初始化這些值並計算出需要畫的圓數量。接著只要一個個把圓畫出來即可:
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); for (int i=0;i<circleNum;i++){ float x = gap+radius+remain/2+((gap+radius*2)*i); canvas.drawCircle(x,0,radius,mPaint); canvas.drawCircle(x,getHeight(),radius,mPaint); } } 程式碼中的 remain/2 是為了處理「無法整除」的情況。若數量除不盡,右邊最後一個間距會比其它間距寬;因此在畫第一個圓時先加上餘距的一半,就能讓第一個與最後一個間距保持一致,視覺上更對稱。
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingLeft="16dp" android:paddingRight="16dp" android:paddingTop="20dp"> <com.qiangyu.test.view.CouponDisplayView android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/indicator_color" android:padding="20dp"> <ImageView android:layout_width="120dp" android:layout_height="match_parent" android:src="@drawable/goods_test" android:scaleType="centerCrop"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:paddingLeft="16dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18dp" android:text="美食劵" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="12dp" android:padding="5dp" android:text="編號:11223124123213131" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="12dp" android:padding="5dp" android:text="編號:11223124123213131" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="12dp" android:paddingLeft="5dp" android:paddingTop="5dp" android:text="截止日期:2001-09-07" /> </LinearLayout> </com.qiangyu.test.view.CouponDisplayView> </FrameLayout>
效果圖:

以上就是以自訂 View 實現邊緣凹凸卡券效果的完整做法。相較於用圖片切圖,這種以 Canvas 動態繪製的方式能自適應任意寬度,邊緣間距也能保持均勻美觀。
用模板方法模式打造具備 View 自動重用能力的 Adapter
先談 ListView 與 GridView 在 Android 開發中,ListView 與 GridView 是出現頻率極高的兩個控制項。它們的共同特點是透過「資料適
Java 集合源碼解析:ListIterator 雙向迭代器完全指南
ListIterator 根据官方文档介绍,ListIterator 有以下功能: 允许我们向前、向后两个方向遍历 List;在遍历时修改 List 的元素;遍历时获
Android 學習教程(5):分類側滑選單的實作
本文示範一個帶分類切換效果的側滑選單,基於 DrawerLayout 搭配 Circular Reveal 動畫,切換分類時以指點位置為圓心做圓形展開轉場。 class
用 ExpandableListView 做兩層分組列表(仿通訊錄分組效果)
ExpandableListView 專門用來顯示「兩層」的垂直捲動列表,最典型的例子就是通訊錄的群組展開/收合。它的運作方式與 ListView 類似,但資料源需要同時