软件编程
位置:首页>> 软件编程>> Android编程>> Android自定义控件实现边缘凹凸的卡劵效果

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

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

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

前言

最近做项目的时候遇到一个卡劵的效果,由于自己觉得用图片来做的话可以会出现适配效果不好,再加上自己自定义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

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com