Android自定義控件實(shí)現(xiàn)邊緣凹凸的卡劵效果
前言
最近做項(xiàng)目的時(shí)候遇到一個(gè)卡劵的效果,由于自己覺得用圖片來做的話可以會(huì)出現(xiàn)適配效果不好,再加上自己自定義view方面的知識(shí)比較薄弱,所以想試試用自定義View來實(shí)現(xiàn)。但是由于自己知識(shí)點(diǎn)薄弱,一開始居然想著用畫矩形來設(shè)置邊緣實(shí)現(xiàn),后面一個(gè)哥們指導(dǎo)了我,在這里感謝他。

實(shí)現(xiàn)分析
上面的圖片其實(shí)和普通的Linearlayout,RelativeLayout一樣,只是上下兩邊多了類似于半圓鋸齒的形狀。那么只需要處理不同地方??梢栽谏舷聝蓷l線上畫一個(gè)個(gè)白色的小圓來實(shí)現(xiàn)這種效果。
假如我們上下線的半圓以及半圓與半圓之間的間距是固定的,那么不同尺寸的屏幕肯定會(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的寬。
看代碼
public class CouponDisplayView extends LinearLayout {
private Paint mPaint;
/**
* 圓間距
*/
private float gap = 8;
/**
* 半徑
*/
private float radius = 10;
/**
* 圓數(shù)量
*/
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);
}
上面定義了圓的半徑和圓間距,同時(shí)初始化了這些值并且獲取了需要畫的圓數(shù)量。
接下來只需要一個(gè)一個(gè)將圓畫出來就可以了。
@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);
}
}
簡(jiǎn)單的根據(jù)circleNum的數(shù)量進(jìn)行了圓的繪制。
這里remain/2是因?yàn)?,可以一些情況,計(jì)算出來的可以畫的數(shù)量不是剛好整除的。這樣就會(huì)出現(xiàn)右邊最后一個(gè)間距會(huì)比其它的間距都要寬。
所以我們?cè)诶L制第一個(gè)的時(shí)候加上了余下的間距的一半,即使是不整除的情況。至少也能保證第一個(gè)和最后一個(gè)間距寬度一致。
這樣就實(shí)現(xiàn)了。
看看效果
<?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">
<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="編號(hào):11223124123213131"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12dp"
android:padding="5dp"
android:text="編號(hào):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>
效果圖:

源碼下載:http://xiazai.jb51.net/201607/yuanma/CouponDisplayView(jb51.net).rar
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android local.properties 文件讀取實(shí)例詳解
這篇文章主要介紹了Android local.properties 文件讀取實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05
Android調(diào)用系統(tǒng)自帶的分享功能實(shí)例代碼
本篇文章主要介紹了Android調(diào)用系統(tǒng)自帶的分享功能實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-04-04
android內(nèi)存優(yōu)化之圖片優(yōu)化
對(duì)圖片本身進(jìn)行操作。盡量不要使用setImageBitmap、setImageResource、BitmapFactory.decodeResource來設(shè)置一張大圖,因?yàn)檫@些方法在完成decode后,最終都是通過java層的createBitmap來完成的,需要消耗更多內(nèi)存2012-12-12
Android中l(wèi)istview嵌套scrollveiw沖突的解決方法
這篇文章主要為大家詳細(xì)介紹了Android中l(wèi)istview嵌套scrollveiw沖突的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
android圖像繪制(七)ClipRect局部繪圖/切割原圖繪制總結(jié)
這幾天開始學(xué)游戲地圖制作,今天小小的總結(jié)一下Canvas的clipRect()接口的使用,接下來介紹ClipRect局部繪圖/切割原圖繪制感興趣的朋友可以了解下2013-01-01
TextInputLayout輸入框控件的懸浮標(biāo)簽
這篇文章主要為大家詳細(xì)介紹了TextInputLayout輸入框控件的懸浮標(biāo)簽,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android在layout xml中使用ViewStub完成動(dòng)態(tài)加載問題
這篇文章主要介紹了Android在layout xml中使用ViewStub完成動(dòng)態(tài)加載問題,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-08-08

