Android自定义边缘凹凸的卡劵效果

所谓前人栽树,后人乘凉,在此感谢博主的贡献。

原文:边缘凹凸的卡劵效果

先上效果图:

我实现的效果和原博主实现的效果是不一样的,我是左右边缘凹凸,而博主是上下边缘凹凸。其实理解了原理,哪个边缘实现这个效果都是可以的。

实现原理:

直接在view边缘上画一个个白色的小圆来实现这种效果,这个view:CouponView 可以让它继承LinearLayout,通过重写onDraw()方法实现重新绘制的效果。最后在布局文件中使用该自定义CouponView即可。画圆的个数如何确定:

(这是原博主的经验,总结的的确很好)

假如我们上下线的半圆以及半圆与半圆之间的间距是固定的,那么不同尺寸的屏幕肯定会画出不同数量的半圆,那么我们只需要根据控件的宽度来获取能画的半圆数。 大家观察图片,很容易发现,圆的数量总是圆间距数量-1,也就是,假设圆的数量是circleNum,那么圆间距就是circleNum+1。 所以我们可以根据这个计算出 circleNum. circleNum = (int) ((w-gap)/(2*radius+gap)); 这里gap就是圆间距,radius是圆半径,w是view的宽。

自定义LinearLayout:CouponView

/** * Created by Veyron on 2017/2/20. * Function:自定义实现边缘凹凸卡卷效果 */public class CouponView extends LinearLayout { private Paint mPaint;  //画笔 private float gap = 8;  //圆间距  private float radius = 10; //半径  private int circleNum;  //圆数量 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);//设置是否使用抗锯齿功能,会消耗较大资源,绘制图形速度会变慢。  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);  width = w;  if (remain==0){   //计算不整除的剩余部分   remain = (int)(h-gap)%(2*radius+gap);  }  circleNum = (int) ((h-gap)/(2*radius+gap)); //计算圆的数量 } public CouponView(Context context, AttributeSet attrs, int defStyleAttr) {  super(context, attrs, defStyleAttr); } /**  * 上面定义了圆的半径和圆间距,同时初始化了这些值并且获取了需要画的圆数量。  接下来只需要一个一个将圆画出来就可以了。  * @param canvas  */ @Override protected void onDraw(Canvas canvas) {  super.onDraw(canvas);  //循环在左右两个边上画出凹凸效果  for (int i=0;i<circleNum;i++){   float y = gap+radius+remain/2+((gap+radius*2)*i);//计算出y轴坐标   canvas.drawCircle(0,y,radius,mPaint);//在左边边画圆   canvas.drawCircle(width,y,radius,mPaint);//在右边画圆  } }}

简单的根据circleNum的数量进行了圆的绘制。

这里remain/2是因为,可能一些情况,计算出来的可以画的数量不是刚好整除的。这样就会出现右边最后一个间距会比其它的间距都要宽。

所以我们在绘制第一个的时候加上了余下的间距的一半,即使是不整除的情况。至少也能保证第一个和最后一个间距宽度一致。

布局文件使用该自定义LinearLayout:CouponView

里面的具体布局就看业务需求了

<?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="优惠汉堡劵"    />   <TextView    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:textSize="18dp"    android:padding="5dp"    android:text="编号:007"    />   <TextView    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:textSize="20dp"    android:padding="5dp"    android:text="优惠价:¥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

觉得不错,欢迎点个Star 哈!!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

无神的瞳孔,我迫切想逃离这周遭被钢筋混凝土堆架的城市,

Android自定义边缘凹凸的卡劵效果

相关文章:

你感兴趣的文章:

标签云: