Android自定義邊緣凹凸的卡劵效果
所謂前人栽樹,后人乘涼,在此感謝博主的貢獻(xiàn)。
原文:邊緣凹凸的卡劵效果
先上效果圖:

我實(shí)現(xiàn)的效果和原博主實(shí)現(xiàn)的效果是不一樣的,我是左右邊緣凹凸,而博主是上下邊緣凹凸。其實(shí)理解了原理,哪個(gè)邊緣實(shí)現(xiàn)這個(gè)效果都是可以的。
實(shí)現(xiàn)原理:
直接在view邊緣上畫一個(gè)個(gè)白色的小圓來實(shí)現(xiàn)這種效果,這個(gè)view:CouponView
可以讓它繼承LinearLayout,通過重寫onDraw()方法實(shí)現(xiàn)重新繪制的效果。最后在布局文件中使用該自定義CouponView即可。
畫圓的個(gè)數(shù)如何確定:
(這是原博主的經(jīng)驗(yàn),總結(jié)的的確很好)
假如我們上下線的半圓以及半圓與半圓之間的間距是固定的,那么不同尺寸的屏幕肯定會(huì)畫出不同數(shù)量的半圓,那么我們只需要根據(jù)控件的寬度來獲取能畫的半圓數(shù)。
大家觀察圖片,很容易發(fā)現(xiàn),圓的數(shù)量總是圓間距數(shù)量-1,也就是,假設(shè)圓的數(shù)量是circleNum,那么圓間距就是circleNum+1。
所以我們可以根據(jù)這個(gè)計(jì)算出
circleNum. circleNum = (int) ((w-gap)/(2*radius+gap));
這里gap就是圓間距,radius是圓半徑,w是view的寬。
自定義LinearLayout:CouponView
/**
* Created by Veyron on 2017/2/20.
* Function:自定義實(shí)現(xiàn)邊緣凹凸卡卷效果
*/
public class CouponView extends LinearLayout {
private Paint mPaint; //畫筆
private float gap = 8; //圓間距
private float radius = 10; //半徑
private int circleNum; //圓數(shù)量
private float remain;
private float width; //視圖寬
public CouponView(Context context) {
super(context);
}
public CouponView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);//設(shè)置是否使用抗鋸齒功能,會(huì)消耗較大資源,繪制圖形速度會(huì)變慢。
mPaint.setDither(true);//設(shè)定是否使用圖像抖動(dòng)處理,會(huì)使繪制出來的圖片顏色更加平滑和飽滿,圖像更加清晰
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);
width = w;
if (remain==0){
//計(jì)算不整除的剩余部分
remain = (int)(h-gap)%(2*radius+gap);
}
circleNum = (int) ((h-gap)/(2*radius+gap)); //計(jì)算圓的數(shù)量
}
public CouponView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
/**
* 上面定義了圓的半徑和圓間距,同時(shí)初始化了這些值并且獲取了需要畫的圓數(shù)量。
接下來只需要一個(gè)一個(gè)將圓畫出來就可以了。
* @param canvas
*/
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//循環(huán)在左右兩個(gè)邊上畫出凹凸效果
for (int i=0;i<circleNum;i++){
float y = gap+radius+remain/2+((gap+radius*2)*i);//計(jì)算出y軸坐標(biāo)
canvas.drawCircle(0,y,radius,mPaint);//在左邊邊畫圓
canvas.drawCircle(width,y,radius,mPaint);//在右邊畫圓
}
}
}
簡(jiǎn)單的根據(jù)circleNum的數(shù)量進(jìn)行了圓的繪制。
這里remain/2是因?yàn)?,可能一些情況,計(jì)算出來的可以畫的數(shù)量不是剛好整除的。這樣就會(huì)出現(xiàn)右邊最后一個(gè)間距會(huì)比其它的間距都要寬。
所以我們?cè)诶L制第一個(gè)的時(shí)候加上了余下的間距的一半,即使是不整除的情況。至少也能保證第一個(gè)和最后一個(gè)間距寬度一致。
布局文件使用該自定義LinearLayout:CouponView
里面的具體布局就看業(yè)務(wù)需求了
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://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"
android:paddingBottom="20dp">
<com.veyron.www.couponview.view.CouponView
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FF5216"
android:padding="20dp">
<ImageView
android:layout_width="120dp"
android:layout_height="120dp"
android:src="@drawable/hanber"
android:scaleType="centerCrop"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="16dp">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="26dp"
android:textColor="#000000"
android:text="優(yōu)惠漢堡劵"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:padding="5dp"
android:text="編號(hào):007"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:padding="5dp"
android:text="優(yōu)惠價(jià):¥18"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12dp"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:text="截止日期:2017-06-09"
/>
</LinearLayout>
</com.veyron.www.couponview.view.CouponView>
</FrameLayout>
源碼:
覺得不錯(cuò),歡迎點(diǎn)個(gè)Star 哈??!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android ActionBar完全解析使用官方推薦的最佳導(dǎo)航欄(下)
這篇文章主要介紹了Android ActionBar完全解析使用官方推薦的最佳導(dǎo)航欄(下) ,需要的朋友可以參考下2017-04-04
android.enableD8.desugaring?=?false引發(fā)問題解決
這篇文章主要為大家介紹了android.enableD8.desugaring?=?false引發(fā)問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
簡(jiǎn)單實(shí)現(xiàn)Android彈出菜單效果
這篇文章主要為大家詳細(xì)介紹了簡(jiǎn)單實(shí)現(xiàn)Android彈出菜單效果的相關(guān)代碼,感興趣的小伙伴們可以參考一下2016-06-06
Android自定義View仿QQ運(yùn)動(dòng)步數(shù)效果
這篇文章主要為大家詳細(xì)介紹了Android自定義View仿QQ運(yùn)動(dòng)步數(shù)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11
Android從系統(tǒng)Gallery獲取圖片具體實(shí)現(xiàn)
這篇文章主要介紹了Android從系統(tǒng)Gallery獲取圖片具體實(shí)現(xiàn),有需要的朋友可以參考一下2013-12-12
Android輸入框添加emoje表情圖標(biāo)的實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android輸入框添加emoje表情圖標(biāo)的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Android實(shí)現(xiàn)帶列表的地圖POI周邊搜索功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)帶列表的地圖POI周邊搜索功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05

