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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

吴威龙   2021-03-24 我要评论
想了解Android自定义边缘凹凸的卡劵效果的相关内容吗,吴威龙在本文为您仔细讲解Android边缘凹凸卡劵效果的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android边缘凹凸卡劵效果,Android凹凸卡劵效果,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 哈!!

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们