Coder Social home page Coder Social logo

dashedline's Introduction

DashedLine

前言

一般情况下, 我们在布局绘制实线和虚线, 可以使用shape来作为View的背景来实现.

  • 实线
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="line">
    <stroke
        android:width="3dp"
        android:color="@color/colorPrimary"/>
</shape>
  • 虚线
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="line">
    <stroke
        android:width="3dp"
        android:color="@color/colorPrimary"
        android:dashGap="3dp"
        android:dashWidth="5dp"/>
</shape>

这个方法绘制实线的时候是没问题的, 但是在绘制虚线时, 很可能无法绘制成功. 因为使用shape绘制虚线需要开启硬件加速. 当然, 有些人觉得那就开启硬件加速呗, 但是开启硬件加速是有利有弊的, 在没有必要开启的时候, 我们完全可以尝试另外的方法, 那就是自定义View.

本项目就是在上诉的情况下建立的, 当然, 不仅仅于绘制直虚线, 还有绘制圆形虚线的控件, 以及后续可能继续开发的具有更多使用实用效果的虚线控件.

效果

绘制直线虚线和圆形虚线的效果如下:


XML代码:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <com.linsh.dashedline.DashedCircleLine
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_gravity="center"
        app:dashColor="@color/colorPrimary"
        app:dashGap="35dp"
        app:dashWidth="2dp"
        app:strokeWidth="20dp"/>

    <com.linsh.dashedline.DashedLine
        android:layout_width="80dp"
        android:layout_height="10dp"
        android:layout_marginTop="10dp"
        app:dashColor="@color/colorPrimary"
        app:dashGap="5dp"
        app:dashOrientation="horizontal"
        app:dashWidth="5dp"
        app:strokeWidth="2dp"/>

    <com.linsh.dashedline.DashedLine
        android:layout_width="10dp"
        android:layout_height="100dp"
        app:dashColor="@color/colorPrimary"
        app:dashGap="5dp"
        app:dashOrientation="vertical"
        app:dashWidth="5dp"/>

    <com.linsh.dashedline.DashedCircleLine
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_gravity="center"
        app:dashColor="@color/colorPrimary"
        app:dashGap="1dp"
        app:dashWidth="2dp"
        app:strokeWidth="1dp"/>

</LinearLayout>

添加依赖

  1. 在项目根目录的build.gradle中添加自定义仓库
allprojects {
  repositories {
    ...
    maven { url 'https://jitpack.io' }
  }
}
  1. 在module里面添加依赖
dependencies {
    compile 'com.github.SenhLinsh:LshUtils:1.2.0'
}

使用方法

attr属性 说明
dashColor 虚线颜色
dashGap 虚线间隔
dashWidth 单个虚线长度
strokeWidth 虚线宽度
dashOrientation 虚线方向 (horizontal--横, vertical--竖)

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.