Android自定义View实现优惠券效果

举报
yechaoa 发表于 2022/05/31 00:07:11 2022/05/31
【摘要】 最近做项目的时候需要一个卡劵的效果,如图: 上面的图片其实和普通的Linearlayout,RelativeLayout一样,只是上下两边多了类似于半圆锯齿的形状。那么只需要处理不同地方。可以在上下两条线上画一个个白色的小圆来实现这种效果。 假如我们上下线的半圆以及半圆与半圆之间的间距是固定的,那么不同尺寸的屏幕肯定会画出不同...

最近做项目的时候需要一个卡劵的效果,如图:


上面的图片其实和普通的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);
              }
          }
  
 



使用:


      <?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>
  
 



原链接:http://www.cnblogs.com/yangqiangyu/p/5499945.html



 

文章来源: blog.csdn.net,作者:yechaoa,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/yechaoa/article/details/59102650

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。