先看看效果图
preview.png
项目导入
在android工程根目录的build.gradle添加
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
然后在当前模块的build.gradle添加依赖
dependencies {
compile 'com.github.dongjunkun:CouponView:1.0'
}
自定义属性说明
自定义属性 | 格式 | 说明 |
---|---|---|
cv_dash_line_color | color | 虚线的颜色 |
cv_dash_line_gap | dimension | 虚线的间隔 |
cv_dash_line_height | dimension | 虚线的高度 |
cv_dash_line_length | dimension | 虚线的长度 |
cv_semicircle_color | color | 半圆的颜色,一般需要和背景色一致 |
cv_semicircle_gap | dimension | 半圆之前的间隔 |
cv_semicircle_radius | dimension | 半圆的半径 |
cv_is_top_semicircle | boolean | 是否绘制顶部半圆锯齿 |
cv_is_bottom_semicircle | boolean | 是否绘制底部半圆锯齿 |
cv_is_left_semicircle | boolean | 是否绘制左侧半圆锯齿 |
cv_is_right_semicircle | boolean | 是否绘制右侧半圆锯齿 |
cv_is_top_dash_line | boolean | 是否绘制顶部虚线 |
cv_is_bottom_dash_line | boolean | 是否绘制底部虚线 |
cv_is_left_dash_line | boolean | 是否绘制左侧虚线 |
cv_is_right_dash_line | boolean | 是否绘制右侧虚线 |
cv_top_dash_line_margin | dimension | 顶部虚线距离View顶部的距离 |
cv_bottom_dash_line_margin | dimension | 底部虚线距离View底部的距离 |
cv_left_dash_line_margin | dimension | 左侧虚线距离View左侧的距离 |
cv_right_dash_line_margin | dimension | 右侧虚线距离View右侧的距离 |
使用
<yyydjk.com.library.CouponView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/couponView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@color/colorAccent"
android:orientation="vertical"
android:padding="10dp" /*虚线离边缘的距离*/
app:cv_dash_line_color="@android:color/white"
app:cv_dash_line_gap="5dp"
app:cv_dash_line_height="2dp"
app:cv_dash_line_length="10dp"
app:cv_is_bottom_dash_line="true"
app:cv_is_bottom_semicircle="false"
app:cv_is_left_dash_line="true"
app:cv_is_left_semicircle="false"
app:cv_is_right_dash_line="true"
app:cv_is_right_semicircle="false"
app:cv_is_top_dash_line="true"
app:cv_is_top_semicircle="false"
app:cv_semicircle_color="@android:color/white"
app:cv_semicircle_gap="8dp"
app:cv_semicircle_radius="4dp">
</yyydjk.com.library.CouponView>
定制自己的View
CouponView是继承于FrameLayout,除了边缘锯齿和虚线边框外,和普通的FrameLayout没有区别
可以通过CouponViewHelper这个代理类来给其他View(比如LinearLayout,ImageView,TextView)添加锯齿背景,只需要继承其他View然后添加以下代码就可以,完整代码参考CouponView
public class CustomView extends YourView{
private CouponViewHelper helper;
public CustomView(Context context) {
this(context, null);
}
public CustomView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
helper = new CouponViewHelper(this, context, attrs, defStyle);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
helper.onSizeChanged(w, h);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
helper.onDraw(canvas);
}
public float getSemicircleGap() {
return helper.getSemicircleGap();
}
public void setSemicircleGap(float semicircleGap) {
helper.setSemicircleGap(semicircleGap);
}
public float getSemicircleRadius() {
return helper.getSemicircleRadius();
}
public void setSemicircleRadius(float semicircleRadius) {
helper.setSemicircleRadius(semicircleRadius);
}
......
}
比如继承ImageView
<yyydjk.com.couponview.widget.CouponImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/couponView"
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="vertical"
android:src="@mipmap/image"
android:scaleType="centerCrop"
app:cv_dash_line_color="@android:color/white"
app:cv_dash_line_gap="5dp"
app:cv_dash_line_height="2dp"
app:cv_dash_line_length="10dp"
app:cv_is_bottom_dash_line="false"
app:cv_is_bottom_semicircle="true"
app:cv_is_left_dash_line="true"
app:cv_is_left_semicircle="false"
app:cv_is_right_dash_line="true"
app:cv_is_right_semicircle="false"
app:cv_is_top_dash_line="false"
app:cv_is_top_semicircle="true"
app:cv_semicircle_color="@android:color/white"
app:cv_semicircle_gap="8dp"
app:cv_semicircle_radius="6dp"/>
效果图如下
Paste_Image.png
更多自定义属性可以通过我的Demo体验
customCoupon.png
动态效果
coupnView_gif1.gif coupnView_gif2.gif coupnView_gif3.gif
下载demo:CouponView
github地址:CouponView