Android 自定义优惠券布局
来源:互联网 发布:青华五轴编程视频教程 编辑:程序博客网 时间:2024/04/26 22:49
一个很有意思的折扣优惠代码实现的废话不多说,直接上图:
思路:
上面的图片其实和普通的Linearlayout,RelativeLayout一样,只是上下两边多了类似于半圆锯齿的形状。那么只需要处理不同地方。可以在上下两条线上画一个个白色的小圆来实现这种效果。
假如我们上下线的半圆以及半圆与半圆之间的间距是固定的,那么不同尺寸的屏幕肯定会画出不同数量的半圆,那么我们只需要根据控件的宽度来获取能画的半圆数。
大家观察图片,很容易发现,圆的数量总是圆间距数量-1,也就是,假设圆的数量是circleNum,那么圆间距就是circleNum+1。
所以我们可以根据这个计算出circleNum. circleNum = (int) ((w-gap)/(2*radius+gap)); 这里gap就是圆间距,radius是圆半径,w是view的宽。
import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.util.AttributeSet;import android.widget.LinearLayout;/** * Created by 折扣卷布局 on 2016/5/17. */public class CouponsView extends LinearLayout { private Paint mPaint; /** * 圆间距 */ private float gap = 8; /** * 半径 */ private float radius = 10; /** * 圆数量 */ private int circleNum; private float remain; public CouponsView(Context context) { super(context); } public CouponsView(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 CouponsView(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); } }}
布局文件如下:
<FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:clipChildren="false" android:layout_centerInParent="true" android:background="@android:color/white"> <com.zhy.magicviewpager.sample.CouponsView android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:background="@color/accent_material_light" android:padding="20dp"> <ImageView android:layout_width="120dp" android:layout_height="match_parent" android:src="@mipmap/ic_launcher" 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="编号:2016042304561456" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="12dp" android:padding="5dp" android:text="编号:2016042304561456" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="12dp" android:paddingLeft="5dp" android:paddingTop="5dp" android:text="截止日期:2016-10-05" /> </LinearLayout> </com.zhy.magicviewpager.sample.CouponsView> </FrameLayout>
截止就完成了自定义折扣券了。
具体详细请看原文地址:http://www.cnblogs.com/yangqiangyu/p/5499945.html
有不明白的欢迎留言!
我们一起共同进步!表示谢谢!如果有技术问题欢迎
加入我的QQ群 285526158.
喜欢的可以关注微信公众号,哪里每天都会推荐一篇开源项目Git项目地址在里欢迎订阅
0 0
- Android 自定义优惠券布局
- Android 自定义优惠券布局
- Android自定义优惠券解析
- 自定义优惠券
- 自定义优惠券
- Android自定义View实现优惠券效果
- Android自定义View——自由定制优惠券背景
- Android自定义View——自由定制优惠券背景
- Android 自定义View之边缘凹凸的优惠券效果
- Android 自定义View之边缘凹凸的优惠券效果
- Android自定义View--简单实现凹凸优惠券效果
- android自定义布局
- Android自定义Notification布局
- android 自定义Preferecne布局
- android 自定义Preferecne布局
- android AlertDialog自定义布局
- Android自定义Gallery布局
- 【Android】自定义AlertDialog布局
- 日期的工具类
- 169. Majority Element
- KAlertController可更改外观的提示
- Java集合类: Set、List、Map、Queue使用场景梳理
- android应用程序调用支付宝接口
- Android 自定义优惠券布局
- 【13】把 Elasticsearch 当数据库使:Join
- 每天laravel-20160808| Container -11
- 通过设置nginx的client_max_body_size解决nginx+php上传大文件的问题
- 盘点iOS开发者必看的105个全球最佳博客
- windows7中的“mklink命令”
- 算法课笔记系列(七)—— 平摊分析Amortized Analysis
- android布局属性
- androidstudio无法启动,Failed to create a child event loop