当前位置: 移动技术网 > 移动技术>移动开发>Android > Android自定义边缘凹凸的卡劵效果

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

2019年07月24日  | 移动技术网移动技术  | 我要评论

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

原文:

先上效果图:

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

实现原理:

直接在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 哈!!

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网