Android 自定义View,虚线纵向、横向

虚线在shape中配置还是比较麻烦的,所以自定义一个,使用起来会方便很多。

 虚线支持横向、纵向两种方式。并且高宽间隔都可以自定义,使用很灵活。

使用说明:

默认方向:横向。

横向时:默认宽度为40,默认高度为View高度

纵向时:默认宽度为View的宽度,默认高度为40

直接在xml中配置:

    <org.quick.component.widget.DashedLineView
        android:layout_width="match_parent"
        android:layout_height="30dp"
        app:dashLineHeight="2dp"
        app:dashLineWidth="10dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="@+id/selectedTv0" />

    <org.quick.component.widget.DashedLineView
        android:layout_width="30dp"
        android:layout_height="match_parent"
        app:dashLineColor="@color/colorPrimary"
        app:dashLineHeight="10dp"
        app:dashLineWidth="2dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:orientation="vertical" />

下面看一下源码:

/**
 * 虚线
 * * Created by Administrator on 2016/8/1.
 * @author chrisZou
 * @from https://github.com/SpringSmell/quick.library
 * @email chrisSpringSmell@gmail.com
 */
class DashedLineView(context: Context, attrs: AttributeSet) : View(context, attrs) {

    private var dashLineColor: Int
    private var dashLineHeight: Float
    private var dashLineWidth: Float
    private var paint = Paint()
    private var orientation = 0x1

    enum class Orientation(var value: Int) {
        HORIZONTAL(0x1), VERTICAL(0x2)
    }

    init {
        val ta = context.obtainStyledAttributes(attrs, R.styleable.DashedLineView)
        dashLineColor = ta.getColor(R.styleable.DashedLineView_dashLineColor, Color.DKGRAY)
        dashLineHeight = ta.getDimension(R.styleable.DashedLineView_dashLineHeight, -1f)
        dashLineWidth = ta.getDimension(R.styleable.DashedLineView_dashLineWidth, -1f)
        orientation = ta.getInt(R.styleable.DashedLineView_orientation, Orientation.HORIZONTAL.value)
        ta.recycle()
        paint.style = Paint.Style.STROKE
        paint.color = dashLineColor
        paint.isAntiAlias = true
    }


    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        val path = Path()
        paint.pathEffect =
                if (orientation == Orientation.HORIZONTAL.value) {
                    if (dashLineWidth == -1f) dashLineWidth = 40f
                    if (dashLineHeight == -1f) dashLineHeight = height.toFloat()
                    paint.strokeWidth = dashLineHeight
                    path.moveTo(0f, height / 2f)
                    path.lineTo(width.toFloat(), height / 2f)
                    DashPathEffect(floatArrayOf(dashLineWidth, dashLineWidth, dashLineWidth, dashLineWidth), 0f)
                } else {
                    if (dashLineWidth == -1f) dashLineWidth = width.toFloat()
                    if (dashLineHeight == -1f) dashLineHeight = 40f
                    paint.strokeWidth = dashLineWidth
                    path.moveTo(width / 2f, 0f)
                    path.lineTo(width / 2f, height.toFloat())
                    DashPathEffect(floatArrayOf(dashLineHeight, dashLineHeight, dashLineHeight, dashLineHeight), 0f)
                }
        canvas.drawPath(path, paint)
        path.reset()
        path.close()
    }
}

属性:

<!--虚线-->
    <declare-styleable name="DashedLineView">
        
        <attr name="dashLineHeight" format="dimension|integer" />
        <attr name="dashLineColor" format="color|integer" />
        <attr name="dashLineWidth" format="dimension|integer" />
        <!--线条方向-->
        <attr name="orientation" format="enum">
            <enum name="horizontal" value="0x1" />
            <enum name="vertical" value="0x2" />
        </attr>
    </declare-styleable>

 

一款简单的物流状态进度展示自定义View,仅供参考学习效果图使用方法布局文件<ExpressView         android:id="@ id/expressview"         android:layout_width="match_parent"         android:layout_height="match_parent"         express:circleToTextMargin="12dp"         express:expressCircleOuterRadius="8dp"         express:expressCircleRadius="6dp"         express:expressTextMargin="12dp"         express:expressTextSize="14sp"         express:expressTextVecPadding="5dp"         express:expressTimeTextSize="10sp"         express:firstExpressCircleMarginLeft="16dp"         express:firstExpressCircleMarginTop="16dp"         express:isTimeButtonVisible="true" />控件属性介绍firstExpressCircleMarginLeft 第一个物流状态点距离父控件坐边的间距 firstExpressCircleMarginTop 第一个物流状态点距离父控件上边的间距 expressCircleRadius 物流状态点内圈半径 expressCircleOuterRadius 物流状态点外圈半径 circleToTextMargin 物流状态提示圈到文字背景的距离 expressTextMargin 文字距离背景边距 expressTextVecPadding 每个物流信息竖直方向的间距 expressTextSize 文字大小 expressTimeTextSize 时间文字大小 isTimeButtonVisible 是否显示时间和文字按钮客户端        //数据源         final List<ExpressMessageBean> list = new ArrayList<>();        ExpressMessageBean bean = new ExpressMessageBean();         bean.setFlowState(1);         bean.setFlowStateBtRight("购买流程");         bean.setCreateTime(1487259871184l);         bean.setCreateTimeFormat(TimeUtils.millis2String(1487259871184l));         bean.setOpContent("您已付款0.1200元,购买 地下城与勇士/广东区/广东1区帐号,请联系卖家卡罗特将密保手机绑定您的手机号 189****2298");         list.add(bean);         bean = new ExpressMessageBean();         bean.setFlowState(4);         bean.setFlowStateBtLeft("同意退款"); //设置左右按钮文字         bean.setFlowStateBtRight("拒绝退款");         bean.setCreateTime(1487259991260l);         bean.setCreateTimeFormat(TimeUtils.millis2String(1487259991260l));         bean.setOpContent("天空套 0.1200 1个-申请退款");         list.add(bean);         bean = new ExpressMessageBean();         bean.setFlowState(5);         bean.setCreateTime(1487259871184l);         bean.setCreateTimeFormat(TimeUtils.millis2String(1487259871184l));         bean.setOpContent("您已付款0.1200元,购买 地下城与勇士/广东区/广东1区帐号,请联系卖家卡罗特将密保手机绑定您的手机号 189****2298");         list.add(bean);         bean = new ExpressMessageBean();         bean.setFlowState(1);         bean.setFlowStateBtRight("购买流程"); //设置右按钮文字         bean.setCreateTime(1487259991260l);         bean.setCreateTimeFormat(TimeUtils.millis2String(1487259991260l));         bean.setOpContent("天空套 0.1200 1个-申请退款");         list.add(bean);        //数据源适配         ExpressViewAdapter adapter = new ExpressViewAdapter<ExpressMessageBean>(list) {            @Override             public ExpressViewData bindData(ExpressView expressView, int position, ExpressMessageBean expressMessageBean) {                ExpressViewData data = new ExpressViewData();                 data.setContent(expressMessageBean.getOpContent());                 data.setTime(expressMessageBean.getCreateTimeFormat());                 data.setLeftBtnText(expressMessageBean.getFlowStateBtLeft());                 data.setRightBtnText(expressMessageBean.getFlowStateBtRight());                return data;             }         };         expressView.setAdapter(adapter);         adapter.notifyDataChanged();        //处理点击事件         expressView.setOnExpressItemButtonClickListener(new ExpressView.OnExpressItemButtonClickListener() {            @Override             public void onExpressItemButtonClick(int position, int status) {                switch (list.get(position).getFlowState()){                    case 1:                         if(status == 1){ //购买流程                             ToastUtil.ToastBottow(TestActivity.this, list.get(position).getFlowStateBtRight());                         }                        break;                    case 4:                         if(status == 0) { //同意退款                             ToastUtil.ToastBottow(TestActivity.this, list.get(position).getFlowStateBtLeft());                         } else if(status == 1){ //拒绝退款                             ToastUtil.ToastBottow(TestActivity.this, list.get(position).getFlowStateBtRight());                         }                        break;                    default:                         break;                 }             }         });待完善1、处理滑动冲突2、处理滑动到顶部和到底部停止滑动的逻辑3、实现弹性滑动的效果博客文章介绍http://www.jianshu.com/p/2d87f62d5d27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值