GithubHelp home page GithubHelp logo

viewsofandroidingithub / couponview Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dongjunkun/couponview

0.0 2.0 0.0 1.84 MB

半圆锯齿背景虚线边框组合实现简单优惠券效果,可实现一些简单组合

Home Page: http://www.jianshu.com/p/b27d0f9b3856

Java 100.00%

couponview's Introduction

##介绍 最近项目中刚好需要做优惠券效果,其他的都好说,关键在一个半圆锯齿和虚线边框的绘制,当然可以使用png图片作为背景来实现,这样很简单,但这样做会拉低整个App的档次,效果不好,修改也麻烦,之前看过网上有人用代码实现了这个效果,看了下原理,但始终用起来问题比较多,使用不灵活,自己就稍微总结了下,整理一个可以简单自定义效果的库,可以先看看效果图

preview.png

我自己遇到的场景是第三种,上面是锯齿,其他三侧均为虚线,当然,还有更多的可以自定义选项,稍后介绍。

coupnView_gif1 coupnView_gif2 coupnView_gif3
CouponView

##项目导入 在android工程根目录的build.gradle添加

allprojects {
    repositories {
        ...
        maven { url "https://jitpack.io" }
    }
}

然后在当前模块的build.gradle添加依赖

dependencies {
	    compile 'com.github.dongjunkun:CouponView:1.0.3'
	}

##自定义属性说明

自定义属性 格式 说明
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_semicircle_top boolean 是否绘制顶部半圆锯齿
cv_is_semicircle_bottom boolean 是否绘制底部半圆锯齿
cv_is_semicircle_left boolean 是否绘制左侧半圆锯齿
cv_is_semicircle_right boolean 是否绘制右侧半圆锯齿
cv_is_dash_line_top boolean 是否绘制顶部虚线
cv_is_dash_line_bottom boolean 是否绘制底部虚线
cv_is_dash_line_left boolean 是否绘制左侧虚线
cv_is_dash_line_right boolean 是否绘制右侧虚线
cv_dash_line_margin_top dimension 顶部虚线距离View顶部的距离
cv_dash_line_margin_bottom dimension 底部虚线距离View底部的距离
cv_dash_line_margin_left dimension 左侧虚线距离View左侧的距离
cv_dash_line_margin_right dimension 右侧虚线距离View右侧的距离

##使用 添加到你的xml

<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="0.8dp"
    app:cv_dash_line_length="10dp"
    app:cv_dash_line_margin_bottom="5dp"
    app:cv_dash_line_margin_left="5dp"
    app:cv_dash_line_margin_right="5dp"
    app:cv_dash_line_margin_top="8dp"
    app:cv_is_dash_line_bottom="true"
    app:cv_is_dash_line_left="true"
    app:cv_is_dash_line_right="true"
    app:cv_is_dash_line_top="false"
    app:cv_is_semicircle_bottom="false"
    app:cv_is_semicircle_left="false"
    app:cv_is_semicircle_right="false"
    app:cv_is_semicircle_top="true"
    app:cv_semicircle_color="@android:color/white"
    app:cv_semicircle_gap="4dp"
    app:cv_semicircle_radius="4dp">
</yyydjk.com.library.CouponView>

或者使用代码动态设置

couponTip.png

##定制自己的View 可以通过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_dash_line_bottom="false"
        app:cv_is_semicircle_bottom="true"
        app:cv_is_dash_line_left="true"
        app:cv_is_semicircle_left="false"
        app:cv_is_dash_line_right="true"
        app:cv_is_semicircle_right="false"
        app:cv_is_dash_line_top="false"
        app:cv_is_semicircle_top="true"
        app:cv_semicircle_color="@android:color/white"
        app:cv_semicircle_gap="8dp"
        app:cv_semicircle_radius="6dp"/>

效果图如下

Paste_Image.png

更多自定义属性可以通过我的Demo体验

customCoupon.png

下载demo:CouponView

参考资料:

couponview's People

Contributors

dongjunkun avatar

Watchers

James Cloos avatar hss01248 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.