自定义优惠券
来源:互联网 发布:微信防伪查询系统源码 编辑:程序博客网 时间:2024/04/24 09:47
一、简述
- 或多或少老哥们都了解自定义View是啥,本篇文章选了一个比较简单的例子来当做示例,希望大佬们能够喜欢。
- 知识点:自定义属性attrs的简单使用,通过继承View重写onDraw方法使用Canvas来绘制,关于Canvas的用法和View基础可以看我前面写的文章。
二、重写onDraw方法
在开始之前,默认老哥您了解Canvas及View的位置参数,如果不了解请到我的博客瞅一眼。
我在处理边距时是将多余的区域平分放到了两侧,也就是相当于内容居中,详细内容可以看代码,注释很详细。
1234567891011121314151617181920212223242526272829
protected void onDraw(Canvas canvas) {super.onDraw(canvas);// 获取宽度与高度int width = getWidth();int height = getHeight();//圆的个数int count;try {// 计算可以显示多少个圆count = (width - gap) / (radius * 2 + gap);} catch (Exception e) {count = 0;Log.e(TAG, "onDraw: ", e);}// 圆的直径int h = (radius * 2);// 圆以外的长度int cgap = (width - (count * h));// 两侧端点的长度,int x1 = (cgap + gap - (gap * count)) / 2;// 绘制for (int i = 0; i < count; i++) {int x = x1 + radius + (h + gap) * i;canvas.drawCircle(x, 0, radius, mPaint);canvas.drawCircle(x, height, radius, mPaint);}}
三、供外部类操作属性的公有方法
12345678910
// 读取圆的半径public int getRadius() {return Utils.px2dp(mContext, radius);}// 设置圆的半径public void setRadius(int radius) {this.radius = Utils.dp2px(mContext, radius);// 刷新视图invalidate();}
四、View自定义属性
创建属性文件(res/values/attrs.xml)
12345678<?xml version="1.0" encoding="utf-8"?><resources><declare-styleable name="ViewZ2View"><attr name="circle_color" format="color" /><attr name="radius" format="integer" /><attr name="gap" format="integer" /></declare-styleable></resources>- 上面的XML文件声明了一个名字为ViewZ2View的自定义属性集合。
- name是自定义属性的名字。
- format是自定义属性的类型,color是颜色属性,integer是基本数据类型,除此之外还有很多,可以阅读文档或直接使用as的代码提示。
在布局文件中添加属性
1234567xmlns:app="http://schemas.android.com/apk/res-auto"... ...<com.sdwfqin.sample.view.viewz2.ViewZ2View... ...app:circle_color="#ffffff"app:gap="10"app:radius="10" />在代码中读取设置的属性
123456789
private void init(Context context, @Nullable AttributeSet attrs) {// 加载自定义属性集合TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ViewZ2View);// Color.WHITE为默认颜色mColor = typedArray.getColor(R.styleable.ViewZ2View_circle_color, Color.WHITE);radius = Utils.dp2px(context, typedArray.getInteger(R.styleable.ViewZ2View_radius, 10));gap = Utils.dp2px(context, typedArray.getInteger(R.styleable.ViewZ2View_gap, 10));typedArray.recycle();}
五、示例代码
Java文件
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104public class ViewZ2View extends LinearLayout {private static final String TAG = "ViewZ2View";private Context mContext;private int mColor = Color.WHITE;private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);// 要提高精度可以使用float//圆的半径private int radius;//圆之间的间距private int gap;public ViewZ2View(Context context) {super(context);init(context);}public ViewZ2View(Context context, @Nullable AttributeSet attrs) {super(context, attrs);init(context, attrs);}public ViewZ2View(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);init(context, attrs);}private void init(Context context) {mContext = context;mPaint.setColor(mColor);gap = Utils.dp2px(context, 10);}private void init(Context context, @Nullable AttributeSet attrs) {mContext = context;TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ViewZ2View);// Color.WHITE为默认颜色mColor = typedArray.getColor(R.styleable.ViewZ2View_circle_color, Color.WHITE);radius = Utils.dp2px(context, typedArray.getInteger(R.styleable.ViewZ2View_radius, 10));gap = Utils.dp2px(context, typedArray.getInteger(R.styleable.ViewZ2View_gap, 10));typedArray.recycle();mPaint.setColor(mColor);}public int getColor() {return mColor;}public void setColor(int color) {this.mColor = color;mPaint.setColor(mColor);// 刷新视图invalidate();}// 读取圆的半径public int getRadius() {return Utils.px2dp(mContext, radius);}// 设置圆的半径public void setRadius(int radius) {this.radius = Utils.dp2px(mContext, radius);// 刷新视图invalidate();}public int getGap() {return Utils.px2dp(mContext, gap);}public void setGap(int gap) {this.gap = Utils.dp2px(mContext, gap);invalidate();}protected void onDraw(Canvas canvas) {super.onDraw(canvas);// 获取宽度与高度int width = getWidth();int height = getHeight();//圆的个数int count;try {// 计算可以显示多少个圆count = (width - gap) / (radius * 2 + gap);} catch (Exception e) {count = 0;Log.e(TAG, "onDraw: ", e);}// 圆的直径int h = (radius * 2);// 圆以外的长度int cgap = (width - (count * h));// 两侧端点的长度,int x1 = (cgap + gap - (gap * count)) / 2;// 绘制for (int i = 0; i < count; i++) {int x = x1 + radius + (h + gap) * i;canvas.drawCircle(x, 0, radius, mPaint);canvas.drawCircle(x, height, radius, mPaint);}}}xml文件
12345678910111213141516<com.sdwfqin.sample.view.viewz2.ViewZ2Viewandroid:id="@+id/viewz2_main"android:layout_width="match_parent"android:layout_height="200dp"android:layout_margin="20dp"android:background="@color/bar"android:gravity="center"app:circle_color="#ffffff"app:gap="10"app:radius="10"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="抽奖券" /></com.sdwfqin.sample.view.viewz2.ViewZ2View>
1 0
- 自定义优惠券
- 自定义优惠券
- Android 自定义优惠券布局
- Android 自定义优惠券布局
- Android自定义优惠券解析
- Android自定义View实现优惠券效果
- 自定义控件——边缘凹凸的优惠券效果
- Android自定义View——自由定制优惠券背景
- Android自定义View——自由定制优惠券背景
- Android 自定义View之边缘凹凸的优惠券效果
- Android 自定义View之边缘凹凸的优惠券效果
- Android自定义View--简单实现凹凸优惠券效果
- 微信摇一摇优惠券
- PAT优惠券
- 优惠券2
- 网购优惠券
- 生成会员卡或优惠券
- 七天优惠券密码
- multiDexEnabled 在android4.x上 NoClassDefFoundError 的坑
- druid基本理解
- 用户行为路径分析(一)
- Random的nextInt用法
- Git 提示fatal: remote origin already exists 错误解决办法
- 自定义优惠券
- 合并两个排序的链表
- linux 驱动module_init()本质--->不同驱动加载顺序对应不同的优先级
- Android问题,知识点汇总
- Android Service和IntentService区别及知识点详细总结
- C语言第三课 流程结构
- MD5,是非对称的加密,加了密之后,没人知道加密前是什么,no body
- 网站统计中的数据收集原理及实现
- FIFO管道/命名管道(半双工)