Android 自定义View之边缘凹凸的优惠券效果的开发过程

Android 自定义View之边缘凹凸的优惠券效果的开发过程,第1张

概述本篇文章讲的是自定义View之边缘凹凸的优惠券效果,之前有见过很多优惠券的效果都是使用了边缘凹凸的样式。和往常一样,主要总结一下在自定义View的开发过程中需要注意的一些地方。

本篇文章讲的是自定义view之边缘凹凸的优惠券效果,之前有见过很多优惠券的效果都是使用了边缘凹凸的样式。和往常一样,主要总结一下在自定义view的开发过程中需要注意的一些地方。

按照惯例,我们先来看看效果图

一、写代码之前,我们先弄清楚vIEw的启动过程:

之所以想要弄清楚这个问题是因为代码里面用到了onSizeChanged()方法,一开始我有点犹豫onSizeChanged是在什么时候启动的呢,所以看看VIEw的启动流程吧

package per.lijuan.coupondisplayvIEwdome;import androID.content.Context;import androID.graphics.Canvas;import androID.util.AttributeSet;import androID.util.Log;import androID.Widget.linearLayout;/** * 自定义边缘凹凸的优惠券效果vIEw * Created by lijuan on 2016/9/26. */public class CoupondisplayVIEw extends linearLayout {  public CoupondisplayVIEw(Context context) {    this(context,null);    Log.d("mDeBUG","CoupondisplayVIEw:context");  }  public CoupondisplayVIEw(Context context,AttributeSet attrs) {    this(context,attrs,0);    Log.d("mDeBUG","CoupondisplayVIEw:context,attrs");  }  public CoupondisplayVIEw(Context context,AttributeSet attrs,int defStyleAttr) {    super(context,defStyleAttr);    Log.d("mDeBUG",defStyleAttr");  }  @OverrIDe  protected voID onSizeChanged(int w,int h,int olDW,int oldh) {    super.onSizeChanged(w,h,olDW,oldh);    Log.d("mDeBUG","onSizeChanged:w=" + w + ",h=" + h + ",olDW=" + olDW + ",oldh=" + oldh);  }  @OverrIDe  protected voID onDraw(Canvas canvas) {    super.onDraw(canvas);    Log.d("mDeBUG","onDraw");  }}

输出如下:

09-27 15:29:31.957 8210-8210/per.lijuan.coupondisplayvIEwdome D/mDeBUG: CoupondisplayVIEw:context,defStyleAttr09-27 15:29:31.957 8210-8210/per.lijuan.coupondisplayvIEwdome D/mDeBUG: CoupondisplayVIEw:context,attrs09-27 15:29:32.050 8210-8210/per.lijuan.coupondisplayvIEwdome D/mDeBUG: onSizeChanged:w=984,h=361,olDW=0,oldh=009-27 15:29:32.083 8210-8210/per.lijuan.coupondisplayvIEwdome D/mDeBUG: onDraw

在这里可以看到,onSizeChanged()方法的启动是在onDraw之前

二、vIEw的几个常用触发方法

1. onFinishInflate():当VIEw中所有的子控件均被映射成xml后触发
2. onMeasure(int wIDthMeasureSpec,int heightmeasureSpec):确定所有子元素的大小
3. onLayout(boolean changed,int l,int t,int r,int b):当VIEw分配所有的子元素的大小和位置时触发
4. onSizeChanged(int w,int oldh):当vIEw的大小发生变化时触发
5. onDraw(Canvas canvas):负责将VIEw绘制在屏幕上

三、VIEw 的几个构造函数

1、public CoupondisplayVIEw(Context context)
―>Java代码直接new一个CoupondisplayVIEw实例的时候,会调用这个只有一个参数的构造函数;

2、public CoupondisplayVIEw(Context context,AttributeSet attrs)
―>在默认的XML布局文件中创建的时候调用这个有两个参数的构造函数。AttributeSet类型的参数负责把XML布局文件中所自定义的属性通过AttributeSet带入到VIEw内;

3、public CoupondisplayVIEw(Context context,int defStyleAttr)
―>构造函数中第三个参数是默认的Style,这里的默认的Style是指它在当前Application或者Activity所用的theme中的默认Style,且只有在明确调用的时候才会调用

4、public CoupondisplayVIEw(Context context,int defStyleAttr,int defStyleRes)
―>该构造函数是在API21的时候才添加上的

自定义view中,我们需要重写了3个构造方法,在上面的构造方法中说过默认的布局文件调用的是两个参数的构造方法,所以记得让所有的构造方法调用三个参数的构造方法,然后在三个参数的构造方法中获得自定义属性。
一开始一个参数的构造方法和两个参数的构造方法是这样的:

public CoupondisplayVIEw(Context context) {    super(context);  }  public CoupondisplayVIEw(Context context,AttributeSet attrs) {    super(context,attrs);  }

我们需要注意的是super应该改成this,然后让一个参数的构造方法引用两个参数的构造方法,两个参数的构造方法引用三个参数的构造方法,代码如下:

public CoupondisplayVIEw(Context context) {    this(context,null);  }  public CoupondisplayVIEw(Context context,0);  }

四、分析具体的实现思路:

从上面的效果图来看,这个自定义view和普通的linearlayout,relativeLayout一样,只是上下两边多了类似于半圆锯齿的形状,我们需要在上下两条线上画一个个白色的小圆来实现这种效果。

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

我们观察效果图会发现,圆的数量总是圆间距数量-1,也就是说,假设圆的数量是circleNum,那么圆间距就是circleNum+1,所以我们可以根据这个计算出circleNum:

circleNum = (int) ((w-gap)/(2*radius+gap));

这里gap就是圆间距,radius是圆半径,w是vIEw的宽。

五、下面我们就开始来看看代码啦

1、自定义view的属性,首先在res/values/ 下建立一个attr.xml , 在里面定义我们的需要用到的属性以及声明相对应属性的取值类型

<?xml version="1.0" enCoding="utf-8"?><resources>  <!--圆间距-->  <attr name="radius" format="dimension" />  <!--半径-->  <attr name="gap" format="dimension" />  <declare-styleable name="CoupondisplayVIEw">    <attr name="radius" />    <attr name="gap" />  </declare-styleable></resources>

我们定义了圆间距和半径2个属性,format是值该属性的取值类型,format取值类型总共有10种,包括:string,color,demension,integer,enum,reference,float,boolean,fraction和flag。

2、然后在XML布局中声明我们的自定义view

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"  xmlns:custom="http://schemas.androID.com/apk/res-auto"  androID:layout_wIDth="match_parent"  androID:layout_height="match_parent"  androID:layout_margin="16dp">  <per.lijuan.coupondisplayvIEwdome.CoupondisplayVIEw    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content"    androID:background="#FBB039"    androID:orIEntation="horizontal"    androID:padding="16dp"    custom:gap="8dp"    custom:radius="5dp">    <ImageVIEw      androID:layout_wIDth="90dp"      androID:layout_height="match_parent"      androID:scaleType="centerCrop"      androID:src="@mipmap/ic_launcher" />    <linearLayout      androID:layout_wIDth="match_parent"      androID:layout_height="wrap_content"      androID:layout_marginleft="5dp"      androID:orIEntation="vertical">      <TextVIEw        androID:ID="@+ID/name"        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        androID:text="电影新客代金"        androID:textSize="18dp" />      <TextVIEw        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        androID:paddingtop="5dp"        androID:text="编号:525451122312431"        androID:textSize="12dp" />      <TextVIEw        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        androID:paddingtop="5dp"        androID:text="满200元可用、限最新版本客户端使用"        androID:textSize="12dp" />      <TextVIEw        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        androID:paddingtop="5dp"        androID:text="截止日期:2016-11-07"        androID:textSize="12dp" />    </linearLayout>  </per.lijuan.coupondisplayvIEwdome.CoupondisplayVIEw></linearLayout>

一定要引入xmlns:custom=”http://schemas.androID.com/apk/res-auto”,AndroID Studio中我们可以使用res-atuo命名空间,就不用添加自定义view全类名。

3、在VIEw的构造方法中,获得我们的xml布局文件中定义的圆的半径和圆间距

private Paint mPaint;  /**   * 半径   */  private float radius=10;  /**   * 圆间距   */  private float gap=8;  /**   * 圆数量   */  private int circleNum;  private float remain;  public CoupondisplayVIEw(Context context) {    this(context,"CoupondisplayVIEw context");  }  public CoupondisplayVIEw(Context context,"CoupondisplayVIEw context,defStyleAttr");    /**     * 获得我们所定义的自定义样式属性     */    TypedArray a = context.gettheme().obtainStyledAttributes(attrs,R.styleable.CoupondisplayVIEw,defStyleAttr,0);    for (int i = 0; i < a.getIndexCount(); i++) {      int attr = a.getIndex(i);      switch (attr) {        case R.styleable.CoupondisplayVIEw_radius:          radius = a.getDimensionPixelSize(R.styleable.CoupondisplayVIEw_radius,10);          break;        case R.styleable.CoupondisplayVIEw_gap:          gap = a.getDimensionPixelSize(R.styleable.CoupondisplayVIEw_radius,8);          break;      }    }    a.recycle();    mPaint = new Paint(Paint.ANTI_AliAS_FLAG);    mPaint.setDither(true);    mPaint.setcolor(color.WHITE);    mPaint.setStyle(Paint.Style.FILL);  }

4、重写onSizeChanged()方法,根据上面的圆的半径和圆间距来计算需要画的圆数量circleNum

@OverrIDe  protected voID onSizeChanged(int w,"onSizeChanged,w=" + w + ",oldh=" + oldh);    if (remain == 0) {      //计算不整除的剩余部分      remain = (int) (w - gap) % (2 * radius + gap);    }    circleNum = (int) ((w - gap) / (2 * radius + gap));  }

5、接下来只需要重写onDraw()方法,简单的根据circleNum的数量将一个一个的圆绘制在屏幕上就可以了

 @OverrIDe  protected voID onDraw(Canvas canvas) {    super.onDraw(canvas);    Log.d("mDeBUG","onDraw");    for (int i = 0; i < circleNum; i++) {      float x = gap + radius + remain / 2 + ((gap + radius * 2) * i);      canvas.drawCircle(x,radius,mPaint);      canvas.drawCircle(x,getHeight(),mPaint);    }  }

这里remain/2是因为避免有一些情况:当计算出来的圆的数量不是整除时,这样就会出现右边最后一个间距会比其它的间距都要宽,所以我们在绘制第一个的时候加上了余下的间距的一半,即使是不整除的情况,至少也能保证第一个和最后一个间距宽度一致。

总结

以上所述是小编给大家介绍的AndroID 自定义view之边缘凹凸的优惠券效果的开发过程,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!

您可能感兴趣的文章:Android自定义View圆形和拖动圆、跟随手指拖动效果Android 自定义TextView去除paddingTop和paddingBottomAndroid自定义View 仿QQ侧滑菜单的实现代码 总结

以上是内存溢出为你收集整理的Android 自定义View之边缘凹凸的优惠券效果的开发过程全部内容,希望文章能够帮你解决Android 自定义View之边缘凹凸的优惠券效果的开发过程所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1142883.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存