Android自定义控件实现边缘凹凸的卡劵效果

作者:yissan 时间:2022-10-01 01:20:51 

前言

最近做项目的时候遇到一个卡劵的效果,由于自己觉得用图片来做的话可以会出现适配效果不好,再加上自己自定义view方面的知识比较薄弱,所以想试试用自定义View来实现。但是由于自己知识点薄弱,一开始居然想着用画矩形来设置边缘实现,后面一个哥们指导了我,在这里感谢他。

Android自定义控件实现边缘凹凸的卡劵效果

实现分析

上面的图片其实和普通的Linearlayout,RelativeLayout一样,只是上下两边多了类似于半圆锯齿的形状。那么只需要处理不同地方。可以在上下两条线上画一个个白色的小圆来实现这种效果。

假如我们上下线的半圆以及半圆与半圆之间的间距是固定的,那么不同尺寸的屏幕肯定会画出不同数量的半圆,那么我们只需要根据控件的宽度来获取能画的半圆数。

大家观察图片,很容易发现,圆的数量总是圆间距数量-1,也就是,假设圆的数量是circleNum,那么圆间距就是circleNum+1。

所以我们可以根据这个计算出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;
/**
 * 圆数量
 */
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);
}

上面定义了圆的半径和圆间距,同时初始化了这些值并且获取了需要画的圆数量。

接下来只需要一个一个将圆画出来就可以了。


@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);
 }
}

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

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

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

这样就实现了。

看看效果


<?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="编号:11223124123213131"
   />
  <TextView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:textSize="12dp"
   android:padding="5dp"
   android:text="编号: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>

效果图:

Android自定义控件实现边缘凹凸的卡劵效果

源码下载:http://xiazai.jb51.net/201607/yuanma/CouponDisplayView(jb51.net).rar

来源:http://blog.csdn.net/yissan/article/details/51429281

标签:Android,自定义控件,卡劵
0
投稿

猜你喜欢

  • java 装饰模式(Decorator Pattern)详解

    2023-08-10 09:56:21
  • 关于MVC与SpringMVC的介绍、区别、执行流程

    2023-11-28 02:25:56
  • 使用Java visualVM监控远程JVM的流程分析

    2022-03-19 16:56:02
  • WinForm项目开发中NPOI用法实例解析

    2021-09-17 16:53:57
  • OnSharedPreferenceChangeListener详解及出现不触发解决办法

    2021-10-25 19:06:36
  • springboot 打包部署 共享依赖包(分布式开发集中式部署微服务)

    2022-12-26 09:13:43
  • java中日期格式化的大坑

    2021-07-02 09:42:59
  • 基于C#的图表控件库 ScottPlot编译visual studio 2022

    2022-05-02 10:24:13
  • 【Java IO流】字节流和字符流的实例讲解

    2023-08-08 20:45:58
  • SpringBoot常用注解详细整理

    2022-12-15 18:53:01
  • java组件commons-fileupload实现文件上传

    2022-10-02 08:33:14
  • UGUI绘制多点连续的平滑曲线

    2022-01-16 06:22:45
  • C# 线程同步的方法

    2022-12-11 01:46:05
  • C# 汉字与拼音互转的实现示例

    2022-03-06 06:50:20
  • 关于Springboot中JSCH的使用及说明

    2023-11-28 02:32:16
  • 基于Java Socket实现一个简易在线聊天功能(一)

    2023-09-23 17:18:35
  • 深入解析kafka 架构原理

    2023-11-18 13:40:14
  • Java下载文件时文件名乱码问题解决办法

    2023-08-23 17:37:03
  • Java将Date日期类型字段转换成json字符串的方法

    2023-02-18 19:57:09
  • Android 开发使用PopupWindow实现弹出警告框的复用类示例

    2022-04-07 03:43:42
  • asp之家 软件编程 m.aspxhome.com