最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android自定義邊緣凹凸的卡劵效果

 更新時(shí)間:2017年05月24日 09:57:38   作者:吳威龍  
這篇文章主要介紹了Android自定義邊緣凹凸的卡劵效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

所謂前人栽樹,后人乘涼,在此感謝博主的貢獻(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>

源碼:

案例Demo

覺得不錯(cuò),歡迎點(diǎn)個(gè)Star 哈!!

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

江津市| 城口县| 长葛市| 马公市| 故城县| 西城区| 响水县| 营山县| 靖远县| 余江县| 昌乐县| 平昌县| 宜章县| 宝坻区| 焉耆| 长葛市| 西峡县| 平罗县| 墨江| 上思县| 营山县| 遂川县| 汾阳市| 普兰店市| 桃园市| 日土县| 乳山市| 太谷县| 宁城县| 定结县| 顺昌县| 永寿县| 资源县| 德安县| 河曲县| 邛崃市| 宝山区| 伽师县| 肥东县| 和硕县| 保定市|