Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 自訂 View 實現邊緣凹凸的卡券效果

編輯:關於 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 動態繪製的方式能自適應任意寬度,邊緣間距也能保持均勻美觀。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved