├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── colors.xml
│ │ │ │ └── styles.xml
│ │ │ ├── mipmap-hdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-anydpi-v26
│ │ │ │ ├── ic_launcher.xml
│ │ │ │ └── ic_launcher_round.xml
│ │ │ ├── drawable-v24
│ │ │ │ └── ic_launcher_foreground.xml
│ │ │ ├── layout
│ │ │ │ ├── activity_vertical_time_ruler.xml
│ │ │ │ └── activity_main.xml
│ │ │ └── drawable
│ │ │ │ └── ic_launcher_background.xml
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── kubi
│ │ │ │ └── timruledemo
│ │ │ │ ├── VideoBean.java
│ │ │ │ ├── TimeBean.java
│ │ │ │ ├── MainActivity.kt
│ │ │ │ └── VerticalTimeRulerActivity.kt
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── kubi
│ │ │ └── timruledemo
│ │ │ └── ExampleUnitTest.kt
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── kubi
│ │ └── timruledemo
│ │ └── ExampleInstrumentedTest.kt
├── proguard-rules.pro
└── build.gradle
├── .idea
├── .name
├── codeStyles
│ ├── codeStyleConfig.xml
│ └── Project.xml
├── compiler.xml
├── misc.xml
├── modules.xml
├── runConfigurations.xml
└── jarRepositories.xml
├── TimeRuler
├── .gitignore
├── consumer-rules.pro
├── src
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── kubi
│ │ │ └── timeruler
│ │ │ └── ExampleUnitTest.kt
│ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── kubi
│ │ │ │ └── timeruler
│ │ │ │ ├── utils
│ │ │ │ └── SizeUtils.java
│ │ │ │ ├── TimeRulerBar.java
│ │ │ │ ├── TimeRulerBarVertical.java
│ │ │ │ ├── BaseScaleBar.java
│ │ │ │ └── BaseScaleBarVertical.java
│ │ └── res
│ │ │ └── values
│ │ │ └── attrs.xml
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── kubi
│ │ └── timeruler
│ │ └── ExampleInstrumentedTest.kt
├── proguard-rules.pro
├── maven-release-aar.gradle
├── build.gradle
└── VERTICAL_SCROLLING_README.md
├── pic
└── view.gif
├── repo
└── com
│ └── kubi
│ └── timeruler
│ └── scaleBar
│ ├── maven-metadata.xml.md5
│ ├── 1.0
│ ├── scaleBar-1.0.aar.md5
│ ├── scaleBar-1.0.pom.md5
│ ├── scaleBar-1.0.aar.sha1
│ ├── scaleBar-1.0.pom.sha1
│ ├── scaleBar-1.0.aar
│ └── scaleBar-1.0.pom
│ ├── maven-metadata.xml.sha1
│ └── maven-metadata.xml
├── settings.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradle.properties
├── .gitignore
├── gradlew.bat
├── README.md
├── gradlew
└── LICENSE
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
--------------------------------------------------------------------------------
/.idea/.name:
--------------------------------------------------------------------------------
1 | TimeRuleViewDemo
--------------------------------------------------------------------------------
/TimeRuler/.gitignore:
--------------------------------------------------------------------------------
1 | /build
--------------------------------------------------------------------------------
/TimeRuler/consumer-rules.pro:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/pic/view.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Liberations/TimeRuler/HEAD/pic/view.gif
--------------------------------------------------------------------------------
/repo/com/kubi/timeruler/scaleBar/maven-metadata.xml.md5:
--------------------------------------------------------------------------------
1 | 78c65c57d4ff29d2e2c4fa35906da20f
--------------------------------------------------------------------------------
/repo/com/kubi/timeruler/scaleBar/1.0/scaleBar-1.0.aar.md5:
--------------------------------------------------------------------------------
1 | 55db574c8711a2dc74887cd91db1a053
--------------------------------------------------------------------------------
/repo/com/kubi/timeruler/scaleBar/1.0/scaleBar-1.0.pom.md5:
--------------------------------------------------------------------------------
1 | 6b4c174609528964397177da49cd845a
--------------------------------------------------------------------------------
/repo/com/kubi/timeruler/scaleBar/1.0/scaleBar-1.0.aar.sha1:
--------------------------------------------------------------------------------
1 | 2e6e3860a3684b4b50cf56fe49c22a8234cf21d3
--------------------------------------------------------------------------------
/repo/com/kubi/timeruler/scaleBar/1.0/scaleBar-1.0.pom.sha1:
--------------------------------------------------------------------------------
1 | 3c1ea4dbe4b2866024e9395fded6e2022b35002b
--------------------------------------------------------------------------------
/repo/com/kubi/timeruler/scaleBar/maven-metadata.xml.sha1:
--------------------------------------------------------------------------------
1 | 86131960cfec8921eeaedd16c377e74f7afb173a
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':TimeRuler'
2 | include ':app'
3 | rootProject.name = "TimeRuleViewDemo"
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | TimeRuleViewDemo
3 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Liberations/TimeRuler/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Liberations/TimeRuler/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Liberations/TimeRuler/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Liberations/TimeRuler/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Liberations/TimeRuler/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Liberations/TimeRuler/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Liberations/TimeRuler/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Liberations/TimeRuler/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Liberations/TimeRuler/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Liberations/TimeRuler/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Liberations/TimeRuler/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/repo/com/kubi/timeruler/scaleBar/1.0/scaleBar-1.0.aar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Liberations/TimeRuler/HEAD/repo/com/kubi/timeruler/scaleBar/1.0/scaleBar-1.0.aar
--------------------------------------------------------------------------------
/.idea/codeStyles/codeStyleConfig.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #6200EE
4 | #3700B3
5 | #03DAC5
6 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed Mar 31 10:26:38 CST 2021
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.14.3-bin.zip
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/repo/com/kubi/timeruler/scaleBar/maven-metadata.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | com.kubi.timeruler
4 | scaleBar
5 |
6 | 1.0
7 |
8 | 1.0
9 |
10 | 20200824043532
11 |
12 |
13 |
--------------------------------------------------------------------------------
/app/src/main/java/com/kubi/timruledemo/VideoBean.java:
--------------------------------------------------------------------------------
1 | package com.kubi.timruledemo;
2 |
3 | public class VideoBean {
4 | public long startTime;
5 | public long endTime;
6 | public boolean isSos;
7 |
8 | public VideoBean(long startTime, long endTime, boolean isSos) {
9 | this.startTime = startTime;
10 | this.endTime = endTime;
11 | this.isSos = isSos;
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
9 |
10 |
--------------------------------------------------------------------------------
/app/src/test/java/com/kubi/timruledemo/ExampleUnitTest.kt:
--------------------------------------------------------------------------------
1 | package com.kubi.timruledemo
2 |
3 | import org.junit.Test
4 |
5 | import org.junit.Assert.*
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * See [testing documentation](http://d.android.com/tools/testing).
11 | */
12 | class ExampleUnitTest {
13 | @Test
14 | fun addition_isCorrect() {
15 | assertEquals(4, 2 + 2)
16 | }
17 | }
--------------------------------------------------------------------------------
/TimeRuler/src/test/java/com/kubi/timeruler/ExampleUnitTest.kt:
--------------------------------------------------------------------------------
1 | package com.kubi.timeruler
2 |
3 | import org.junit.Test
4 |
5 | import org.junit.Assert.*
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * See [testing documentation](http://d.android.com/tools/testing).
11 | */
12 | class ExampleUnitTest {
13 | @Test
14 | fun addition_isCorrect() {
15 | assertEquals(4, 2 + 2)
16 | }
17 | }
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
12 |
13 |
--------------------------------------------------------------------------------
/TimeRuler/src/main/java/com/kubi/timeruler/utils/SizeUtils.java:
--------------------------------------------------------------------------------
1 | package com.kubi.timeruler.utils;
2 |
3 | import android.content.Context;
4 | import android.util.TypedValue;
5 |
6 | public class SizeUtils {
7 | public static int dp2px(Context context, float dpValue) {
8 | return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue, context.getResources().getDisplayMetrics());
9 | }
10 |
11 | public static int sp2px(Context context, float spValue) {
12 | return (int) (spValue * context.getResources().getDisplayMetrics().scaledDensity + 0.5f);
13 | }
14 |
15 | public static int px2sp(Context context, float pxValue) {
16 | return (int) (pxValue / context.getResources().getDisplayMetrics().scaledDensity + 0.5f);
17 | }
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/kubi/timruledemo/ExampleInstrumentedTest.kt:
--------------------------------------------------------------------------------
1 | package com.kubi.timruledemo
2 |
3 | import androidx.test.platform.app.InstrumentationRegistry
4 | import androidx.test.ext.junit.runners.AndroidJUnit4
5 |
6 | import org.junit.Test
7 | import org.junit.runner.RunWith
8 |
9 | import org.junit.Assert.*
10 |
11 | /**
12 | * Instrumented test, which will execute on an Android device.
13 | *
14 | * See [testing documentation](http://d.android.com/tools/testing).
15 | */
16 | @RunWith(AndroidJUnit4::class)
17 | class ExampleInstrumentedTest {
18 | @Test
19 | fun useAppContext() {
20 | // Context of the app under test.
21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22 | assertEquals("com.kubi.timruledemo", appContext.packageName)
23 | }
24 | }
--------------------------------------------------------------------------------
/TimeRuler/src/androidTest/java/com/kubi/timeruler/ExampleInstrumentedTest.kt:
--------------------------------------------------------------------------------
1 | package com.kubi.timeruler
2 |
3 | import androidx.test.platform.app.InstrumentationRegistry
4 | import androidx.test.ext.junit.runners.AndroidJUnit4
5 |
6 | import org.junit.Test
7 | import org.junit.runner.RunWith
8 |
9 | import org.junit.Assert.*
10 |
11 | /**
12 | * Instrumented test, which will execute on an Android device.
13 | *
14 | * See [testing documentation](http://d.android.com/tools/testing).
15 | */
16 | @RunWith(AndroidJUnit4::class)
17 | class ExampleInstrumentedTest {
18 | @Test
19 | fun useAppContext() {
20 | // Context of the app under test.
21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22 | assertEquals("com.kubi.timeruler.test", appContext.packageName)
23 | }
24 | }
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
--------------------------------------------------------------------------------
/TimeRuler/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
--------------------------------------------------------------------------------
/app/src/main/java/com/kubi/timruledemo/TimeBean.java:
--------------------------------------------------------------------------------
1 | package com.kubi.timruledemo;
2 |
3 | import android.graphics.Color;
4 |
5 | import com.kubi.timeruler.TimeRulerBar;
6 |
7 | import java.util.List;
8 |
9 | public class TimeBean implements TimeRulerBar.ColorScale {
10 | List videoBeans;
11 |
12 | public TimeBean(List videoBeans) {
13 | this.videoBeans = videoBeans;
14 | }
15 |
16 | @Override
17 | public int getSize() {
18 | return videoBeans.size();
19 | }
20 |
21 | @Override
22 | public long getStart(int index) {
23 | return videoBeans.get(index).startTime;
24 | }
25 |
26 | @Override
27 | public long getEnd(int index) {
28 | return videoBeans.get(index).endTime;
29 | }
30 |
31 | @Override
32 | public int getColor(int index) {
33 | return videoBeans.get(index).isSos ? Color.RED : Color.GREEN;
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
11 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/TimeRuler/maven-release-aar.gradle:
--------------------------------------------------------------------------------
1 | // 1.maven-插件
2 | apply plugin: 'maven'
3 | // 2.maven-信息
4 | ext {// ext is a gradle closure allowing the declaration of global properties
5 | PUBLISH_GROUP_ID = 'com.kubi.timeruler'
6 | PUBLISH_ARTIFACT_ID = 'scaleBar'
7 | PUBLISH_VERSION = "1.0"
8 | }
9 | // 3.maven-路径
10 | uploadArchives {
11 | repositories.mavenDeployer {
12 | //Maven仓库本地路径(可以自己新建文件夹,复制链接过来)
13 | //注意”file://“ + 路径,有三个斜杠,别漏了
14 | repository(url: uri('../repo')) //定义本地maven仓库的地址
15 |
16 | pom.project {
17 | groupId project.PUBLISH_GROUP_ID
18 | artifactId project.PUBLISH_ARTIFACT_ID
19 | version project.PUBLISH_VERSION
20 | }
21 | }
22 | }
23 |
24 | //以下代码会生成 jar 包源文件,如果是不开源码,请不要输入这段
25 | //aar包内包含注释
26 | /*
27 | task androidSourcesJar(type: Jar) {
28 | classifier = 'sources'
29 | from android.sourceSets.main.java.sourceFiles
30 | }
31 |
32 | artifacts {
33 | archives androidSourcesJar
34 | }*/
35 |
--------------------------------------------------------------------------------
/TimeRuler/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | //aar打包
3 | //apply from: 'maven-release-aar.gradle'
4 | //apply plugin: 'com.github.dcendents.android-maven'
5 | //group='com.github.fiui'
6 | //version = '1.0'
7 | //apply from: '../gradle/build_upload.gradle'
8 |
9 | android {
10 | compileSdkVersion 36
11 |
12 | namespace "com.kubi.timeruler"
13 |
14 | defaultConfig {
15 | minSdkVersion 21
16 | targetSdkVersion 36
17 | versionCode 1
18 | versionName "1.0"
19 |
20 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
21 | consumerProguardFiles "consumer-rules.pro"
22 | }
23 |
24 | buildTypes {
25 | release {
26 | minifyEnabled false
27 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
28 | }
29 | }
30 | }
31 |
32 | dependencies {
33 | implementation fileTree(dir: "libs", include: ["*.jar"])
34 | implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
35 | implementation 'androidx.core:core-ktx:1.17.0'
36 | implementation 'androidx.appcompat:appcompat:1.7.1'
37 |
38 | }
39 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 | apply plugin: 'kotlin-android'
3 | android {
4 | compileSdkVersion 36
5 | namespace "com.kubi.timruledemo"
6 |
7 | defaultConfig {
8 | applicationId "com.kubi.timruledemo"
9 | minSdkVersion 21
10 | targetSdkVersion 36
11 | versionCode 1
12 | versionName "1.0"
13 |
14 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
15 | }
16 |
17 | buildFeatures {
18 | viewBinding true
19 | }
20 |
21 | kotlinOptions {
22 | jvmTarget = "1.8"
23 | }
24 |
25 |
26 |
27 | buildTypes {
28 | release {
29 | minifyEnabled false
30 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
31 | }
32 | }
33 | }
34 |
35 | dependencies {
36 | implementation fileTree(dir: "libs", include: ["*.jar"])
37 | implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
38 | implementation 'androidx.core:core-ktx:1.17.0'
39 | implementation 'androidx.appcompat:appcompat:1.7.1'
40 | implementation 'androidx.constraintlayout:constraintlayout:2.2.1'
41 | implementation project(':TimeRuler')
42 | }
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx2048m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 | # AndroidX package structure to make it clearer which packages are bundled with the
15 | # Android operating system, and which are packaged with your app"s APK
16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn
17 | android.useAndroidX=true
18 | # Automatically convert third-party libraries to use AndroidX
19 | android.enableJetifier=true
20 | # Kotlin code style for this project: "official" or "obsolete":
21 | kotlin.code.style=official
--------------------------------------------------------------------------------
/repo/com/kubi/timeruler/scaleBar/1.0/scaleBar-1.0.pom:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 | com.kubi.timeruler
6 | scaleBar
7 | 1.0
8 | aar
9 |
10 |
11 | org.jetbrains.kotlin
12 | kotlin-android-extensions-runtime
13 | 1.3.72
14 | compile
15 |
16 |
17 | org.jetbrains.kotlin
18 | kotlin-stdlib
19 | 1.3.72
20 | compile
21 |
22 |
23 | androidx.core
24 | core-ktx
25 | 1.3.1
26 | compile
27 |
28 |
29 | androidx.appcompat
30 | appcompat
31 | 1.2.0
32 | compile
33 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Built application files
2 | *.apk
3 | *.ap_
4 | *.aab
5 |
6 | # Files for the ART/Dalvik VM
7 | *.dex
8 |
9 | # Java class files
10 | *.class
11 |
12 | # Generated files
13 | bin/
14 | gen/
15 | out/
16 |
17 | # Gradle files
18 | .gradle/
19 | build/
20 |
21 | # Local configuration file (sdk path, etc)
22 | local.properties
23 |
24 | # Proguard folder generated by Eclipse
25 | proguard/
26 |
27 | # Log Files
28 | *.log
29 |
30 | # Android Studio Navigation editor temp files
31 | .navigation/
32 |
33 | # Android Studio captures folder
34 | captures/
35 |
36 | # IntelliJ
37 | *.iml
38 | .idea/workspace.xml
39 | .idea/tasks.xml
40 | .idea/gradle.xml
41 | .idea/assetWizardSettings.xml
42 | .idea/dictionaries
43 | .idea/libraries
44 | .idea/caches
45 |
46 | # Keystore files
47 | # Uncomment the following lines if you do not want to check your keystore files in.
48 | #*.jks
49 | #*.keystore
50 |
51 | # External native build folder generated in Android Studio 2.2 and later
52 | .externalNativeBuild
53 |
54 | # Google Services (e.g. APIs or Firebase)
55 | # google-services.json
56 |
57 | # Freeline
58 | freeline.py
59 | freeline/
60 | freeline_project_description.json
61 |
62 | # fastlane
63 | fastlane/report.xml
64 | fastlane/Preview.html
65 | fastlane/screenshots
66 | fastlane/test_output
67 | fastlane/readme.md
68 |
69 | # Version control
70 | vcs.xml
71 |
72 | # lint
73 | lint/intermediates/
74 | lint/generated/
75 | lint/outputs/
76 | lint/tmp/
77 | # lint/reports/
78 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
8 |
9 |
15 |
18 |
21 |
22 |
23 |
24 |
30 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/.idea/jarRepositories.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # TimeRuler
2 |
3 | [](https://jitpack.io/#Liberations/TimeRuler)
4 |
5 | 可以缩放平移的时间刻度尺,方便自定义UI需求。仿萤石云历史录像时间轴
6 |
7 | ## 效果:
8 | 
9 |
10 | Add it to your build.gradle with:
11 | ```gradle
12 | allprojects {
13 | repositories {
14 | maven { url "https://jitpack.io" }
15 | }
16 | }
17 | ```
18 | and:
19 |
20 | ```gradle
21 | dependencies {
22 | implementation 'com.github.Liberations:TimeRuler:{latest version}'
23 | }
24 | ```
25 |
26 | ## BaseScaleBar支持的属性设置(Attributes):
27 | |name|format|description|
28 | |:---:|:---:|:---:|
29 | |keyTickHeight|dimension,reference|关键刻度高度|
30 | |tickValueOffset|dimension,reference|刻度文字偏移|
31 | |cursorPosition|float|游标相对view width的位置 0~ 1|
32 | |baselinePosition|float|刻度尺横线相对view height的位置 0~1|
33 | |tickColor|color,reference|刻度线颜色|
34 | |showTickLine|boolean|是否显示刻度横线|
35 | |showCursorLine|boolean|是否显示游标线|
36 | |showTickValue|boolean|是否显示刻度值|
37 | |tickDirectionUp|boolean|刻度线开口方向|
38 | |cursorLineColor|color,reference|游标线颜色|
39 | |maxScaleValueSize|color,reference|刻度值最大规格(字体大小)|
40 |
41 |
42 | ## TimeRulerBar支持的额外属性(Attributes):
43 | |name|format|description|
44 | |:---:|:---:|:---:|
45 | |tickValueColor|color,reference|刻度值颜色|
46 | |colorScaleBackground|color,reference|颜色区域背景色|
47 | |cursorBackgroundColor|color,reference|游标背景色|
48 | |drawCursorContent|boolean|是否绘制游标内容|
49 | |tickValueSize|dimension,reference|刻度值文字大小|
50 | |videoAreaOffset|dimension,reference|绘制颜色区域相对于顶部的偏移量|
51 | |videoAreaHeight|dimension,reference|绘制颜色区域的高度|
52 | |tickValueColor|color,reference|刻度值颜色|
53 |
54 | ## 基本使用
55 |
56 | ```
57 | val calendar = Calendar.getInstance()
58 |
59 | // 00:00:00 000
60 | calendar[Calendar.HOUR_OF_DAY] = 0
61 | calendar[Calendar.MINUTE] = 0
62 | calendar[Calendar.SECOND] = 0
63 | calendar[Calendar.MILLISECOND] = 0
64 | var startTime = calendar.timeInMillis
65 |
66 | // 23:59:59 999
67 | calendar[Calendar.HOUR_OF_DAY] = 23
68 | calendar[Calendar.MINUTE] = 59
69 | calendar[Calendar.SECOND] = 59
70 | calendar[Calendar.MILLISECOND] = 999
71 | var endTime = calendar.timeInMillis
72 | //设置刻度尺时间范围
73 | timeBar.setRange(startTime, endTime)
74 | //设置初始化缩放模式
75 | timeBar.setMode(TimeRulerBar.MODE_UINT_30_MIN)
76 | //设置当前刻度值
77 | timeBar.setCursorValue(System.currentTimeMillis())
78 | ```
79 |
80 | ## 滑动事件监听
81 |
82 | ```
83 | timeBar.setOnCursorListener(object : BaseScaleBar.OnCursorListener {
84 | override fun onStartTrackingTouch(cursorValue: Long) {
85 | //开滑动
86 |
87 | }
88 |
89 | override fun onProgressChanged(cursorValue: Long,fromeUser:Boolean) {
90 | //刻度发生变化 fromeUser是否触摸事件触发
91 | }
92 |
93 | override fun onStopTrackingTouch(cursorValue: Long) {
94 | //结束滑动
95 |
96 | }
97 | })
98 | ```
99 | ## 更多方法参照Demo
100 |
--------------------------------------------------------------------------------
/.idea/codeStyles/Project.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 | xmlns:android
17 |
18 | ^$
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 | xmlns:.*
28 |
29 | ^$
30 |
31 |
32 | BY_NAME
33 |
34 |
35 |
36 |
37 |
38 |
39 | .*:id
40 |
41 | http://schemas.android.com/apk/res/android
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 | .*:name
51 |
52 | http://schemas.android.com/apk/res/android
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 | name
62 |
63 | ^$
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 | style
73 |
74 | ^$
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 | .*
84 |
85 | ^$
86 |
87 |
88 | BY_NAME
89 |
90 |
91 |
92 |
93 |
94 |
95 | .*
96 |
97 | http://schemas.android.com/apk/res/android
98 |
99 |
100 | ANDROID_ATTRIBUTE_ORDER
101 |
102 |
103 |
104 |
105 |
106 |
107 | .*
108 |
109 | .*
110 |
111 |
112 | BY_NAME
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
--------------------------------------------------------------------------------
/TimeRuler/src/main/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
--------------------------------------------------------------------------------
/TimeRuler/VERTICAL_SCROLLING_README.md:
--------------------------------------------------------------------------------
1 | # TimeRuler 纵向滚动功能
2 |
3 | ## 概述
4 |
5 | TimeRuler库现在支持纵向滚动功能!除了原有的横向滚动`TimeRulerBar`,我们新增了纵向滚动的`TimeRulerBarVertical`组件。
6 |
7 | ## 新增组件
8 |
9 | ### 1. BaseScaleBarVertical
10 | - 纵向滚动的基础类
11 | - 支持垂直方向的时间轴滚动
12 | - 提供与横向版本相同的功能,但适配了纵向布局
13 |
14 | ### 2. TimeRulerBarVertical
15 | - 纵向滚动的时间尺组件
16 | - 继承自`BaseScaleBarVertical`
17 | - 提供完整的时间轴功能,包括缩放、拖拽、惯性滚动等
18 |
19 | ## 使用方法
20 |
21 | ### 在XML布局中使用
22 |
23 | ```xml
24 |
41 | ```
42 |
43 | ### 在代码中配置
44 |
45 | ```kotlin
46 | // 设置时间范围
47 | val calendar = Calendar.getInstance()
48 | calendar[Calendar.HOUR_OF_DAY] = 0
49 | calendar[Calendar.MINUTE] = 0
50 | calendar[Calendar.SECOND] = 0
51 | calendar[Calendar.MILLISECOND] = 0
52 | val startTime = calendar.timeInMillis
53 |
54 | calendar[Calendar.HOUR_OF_DAY] = 23
55 | calendar[Calendar.MINUTE] = 59
56 | calendar[Calendar.SECOND] = 59
57 | calendar[Calendar.MILLISECOND] = 999
58 | val endTime = calendar.timeInMillis
59 |
60 | timeBarVertical.setRange(startTime, endTime)
61 | timeBarVertical.setMode(TimeRulerBarVertical.MODE_UINT_30_MIN)
62 | timeBarVertical.setCursorValue(System.currentTimeMillis())
63 |
64 | // 设置监听器
65 | timeBarVertical.setOnCursorListener(object : BaseScaleBarVertical.OnCursorListener {
66 | override fun onStartTrackingTouch(cursorValue: Long) {
67 | // 开始拖拽
68 | }
69 |
70 | override fun onProgressChanged(cursorValue: Long, fromUser: Boolean) {
71 | // 时间值变化
72 | val timeText = SimpleDateFormat("HH:mm:ss").format(Date(cursorValue))
73 | }
74 |
75 | override fun onStopTrackingTouch(cursorValue: Long) {
76 | // 停止拖拽
77 | }
78 | })
79 | ```
80 |
81 | ## 主要特性
82 |
83 | ### 1. 纵向滚动支持
84 | - 支持垂直方向的拖拽滚动
85 | - 支持惯性滚动
86 | - 支持缩放功能
87 |
88 | ### 2. 刻度线方向控制
89 | - `tickDirectionLeft`: 控制刻度线向左还是向右延伸
90 | - 默认值为`true`(向左)
91 |
92 | ### 3. 游标位置控制
93 | - `cursorPosition`: 游标在视图中的相对位置(0-1)
94 | - 默认值为0.5(中间位置)
95 |
96 | ### 4. 基线位置控制
97 | - `baselinePosition`: 刻度尺基线在视图中的相对位置(0-1)
98 | - 默认值为0.67
99 |
100 | ## 属性说明
101 |
102 | ### BaseScaleBarVertical 属性
103 | - `keyTickWidthVertical`: 关键刻度宽度
104 | - `tickValueOffsetVertical`: 文字偏移量
105 | - `normalTickRatioVertical`: 普通刻度与关键刻度的比率
106 | - `cursorPositionVertical`: 游标相对位置
107 | - `baselinePositionVertical`: 基线相对位置
108 | - `tickColorVertical`: 刻度线颜色
109 | - `showTickLineVertical`: 是否显示刻度线
110 | - `showCursorLineVertical`: 是否显示游标线
111 | - `showTickValueVertical`: 是否显示刻度值
112 | - `tickDirectionLeftVertical`: 刻度线方向
113 | - `cursorLineColorVertical`: 游标线颜色
114 | - `maxScaleValueSizeVertical`: 刻度值最大字体大小
115 |
116 | ### TimeRulerBarVertical 属性
117 | - `tickValueColorVertical`: 刻度值颜色
118 | - `videoAreaWidthVertical`: 视频区域宽度
119 | - `videoAreaOffsetVertical`: 视频区域偏移量
120 | - `tickValueSizeVertical`: 刻度值字体大小
121 | - `drawCursorContentVertical`: 是否绘制游标内容
122 | - `cursorBackgroundColorVertical`: 游标背景颜色
123 | - `cursorValueSizeVertical`: 游标值字体大小
124 | - `colorScaleBackgroundVertical`: 颜色刻度尺背景
125 |
126 | ## 模式支持
127 |
128 | 纵向滚动组件支持与横向版本相同的时间模式:
129 |
130 | - `MODE_UINT_1_MIN`: 1分钟单位
131 | - `MODE_UINT_5_MIN`: 5分钟单位
132 | - `MODE_UINT_10_MIN`: 10分钟单位
133 | - `MODE_UINT_30_MIN`: 30分钟单位
134 | - `MODE_UINT_1_HOUR`: 1小时单位
135 |
136 | ## 示例项目
137 |
138 | 查看 `VerticalTimeRulerActivity` 了解完整的使用示例,包括:
139 | - 基本配置
140 | - 事件监听
141 | - 缩放控制
142 | - 游标显示控制
143 | - 刻度线方向控制
144 |
145 | ## 注意事项
146 |
147 | 1. 纵向滚动组件使用画布旋转来实现垂直布局,性能与横向版本相当
148 | 2. 触摸事件已适配纵向滚动,使用`distanceY`而不是`distanceX`
149 | 3. 惯性滚动同样适配了纵向方向
150 | 4. 所有原有的功能都得到保留,只是方向从横向变为纵向
151 | 5. 游标绘制和颜色区域绘制已完全适配纵向布局
152 | 6. 所有public方法都添加了详细的JavaDoc注释
153 |
154 | ## 兼容性
155 |
156 | - 与原有横向滚动组件完全兼容
157 | - 可以同时使用横向和纵向组件
158 | - 共享相同的数据模型和接口
159 |
--------------------------------------------------------------------------------
/app/src/main/java/com/kubi/timruledemo/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package com.kubi.timruledemo
2 |
3 | import android.content.Intent
4 | import android.os.Bundle
5 | import android.widget.SeekBar
6 | import androidx.appcompat.app.AppCompatActivity
7 | import com.kubi.timeruler.BaseScaleBar
8 | import com.kubi.timeruler.TimeRulerBar
9 | import com.kubi.timruledemo.databinding.ActivityMainBinding
10 | import java.text.SimpleDateFormat
11 | import java.util.*
12 |
13 | class MainActivity : AppCompatActivity() {
14 | lateinit var binding: ActivityMainBinding
15 | val cursorDateFormat = SimpleDateFormat("MM月dd日 HH:mm:ss")
16 | var nowTime = 1
17 | override fun onCreate(savedInstanceState: Bundle?) {
18 | super.onCreate(savedInstanceState)
19 | binding = ActivityMainBinding.inflate(layoutInflater)
20 | setContentView(binding.root)
21 | initTimeBar()
22 | }
23 |
24 | private fun initTimeBar() {
25 | val calendar = Calendar.getInstance()
26 |
27 | // 00:00:00 000
28 | calendar[Calendar.HOUR_OF_DAY] = 0
29 | calendar[Calendar.MINUTE] = 0
30 | calendar[Calendar.SECOND] = 0
31 | calendar[Calendar.MILLISECOND] = 0
32 | var startTime = calendar.timeInMillis
33 |
34 | // 23:59:59 999
35 | calendar[Calendar.HOUR_OF_DAY] = 23
36 | calendar[Calendar.MINUTE] = 59
37 | calendar[Calendar.SECOND] = 59
38 | calendar[Calendar.MILLISECOND] = 999
39 | var endTime = calendar.timeInMillis
40 |
41 | binding.timeBar.setRange(startTime, endTime)
42 | binding.timeBar.setMode(TimeRulerBar.MODE_UINT_30_MIN)
43 | binding.timeBar.setCursorValue(System.currentTimeMillis())
44 |
45 | binding.timeBar.setOnCursorListener(object : BaseScaleBar.OnCursorListener {
46 | override fun onStartTrackingTouch(cursorValue: Long) {
47 |
48 | }
49 |
50 | override fun onProgressChanged(cursorValue: Long, fromeUser: Boolean) {
51 | //监听时间戳变化
52 | binding.tvData.text = cursorDateFormat.format(Date(cursorValue))
53 | }
54 |
55 | override fun onStopTrackingTouch(cursorValue: Long) {
56 |
57 | }
58 | })
59 |
60 | binding.seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
61 | override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
62 | binding.timeBar.setScale(progress.toFloat())
63 | }
64 |
65 | override fun onStartTrackingTouch(seekBar: SeekBar?) {
66 | }
67 |
68 | override fun onStopTrackingTouch(seekBar: SeekBar?) {
69 | }
70 |
71 | })
72 |
73 | binding.seekAreaOffset.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
74 | override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
75 | binding.timeBar.setVideoAreaOffset(progress)
76 | }
77 |
78 | override fun onStartTrackingTouch(seekBar: SeekBar?) {
79 | }
80 |
81 | override fun onStopTrackingTouch(seekBar: SeekBar?) {
82 | }
83 |
84 | })
85 |
86 | binding.btnDir.setOnClickListener {
87 | binding.btnDir.isSelected = !binding.btnDir.isSelected
88 | binding.timeBar.setTickDirection(binding.btnDir.isSelected)
89 | }
90 |
91 | binding.btnShowCursor.setOnClickListener {
92 | binding.btnShowCursor.isSelected = !binding.btnShowCursor.isSelected
93 | binding.timeBar.setShowCursor(binding.btnShowCursor.isSelected);
94 | }
95 |
96 | binding.btnPlay.setOnClickListener {
97 | nowTime++
98 | binding.timeBar.setCursorValue(System.currentTimeMillis() + 1000 * nowTime * 60)
99 | }
100 |
101 | binding.btnVerticalDemo.setOnClickListener {
102 | startActivity(Intent(this, VerticalTimeRulerActivity::class.java))
103 | }
104 |
105 | setData()
106 |
107 | }
108 |
109 | private fun setData() {
110 | var videos = mutableListOf()
111 | var testTime = System.currentTimeMillis()
112 | for (i in 1..5) {
113 | val video = VideoBean(testTime, testTime + i * 10 * 60 * 1000, i % 2 == 0)
114 | videos.add(video)
115 | testTime += i * 15 * 60 * 1000
116 | }
117 | val timeBean = TimeBean(videos)
118 | binding.timeBar.setColorScale(timeBean)
119 | }
120 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/kubi/timruledemo/VerticalTimeRulerActivity.kt:
--------------------------------------------------------------------------------
1 | package com.kubi.timruledemo
2 |
3 | import android.os.Bundle
4 | import android.widget.SeekBar
5 | import androidx.appcompat.app.AppCompatActivity
6 | import com.kubi.timeruler.BaseScaleBarVertical
7 | import com.kubi.timeruler.TimeRulerBarVertical
8 | import com.kubi.timruledemo.databinding.ActivityVerticalTimeRulerBinding
9 | import java.text.SimpleDateFormat
10 | import java.util.*
11 |
12 | class VerticalTimeRulerActivity : AppCompatActivity() {
13 | lateinit var binding: ActivityVerticalTimeRulerBinding
14 | val cursorDateFormat = SimpleDateFormat("MM月dd日 HH:mm:ss")
15 | var nowTime = 1
16 |
17 | override fun onCreate(savedInstanceState: Bundle?) {
18 | super.onCreate(savedInstanceState)
19 | binding = ActivityVerticalTimeRulerBinding.inflate(layoutInflater)
20 | setContentView(binding.root)
21 | initTimeBar()
22 | }
23 |
24 | private fun initTimeBar() {
25 | val calendar = Calendar.getInstance()
26 |
27 | // 00:00:00 000
28 | calendar[Calendar.HOUR_OF_DAY] = 0
29 | calendar[Calendar.MINUTE] = 0
30 | calendar[Calendar.SECOND] = 0
31 | calendar[Calendar.MILLISECOND] = 0
32 | var startTime = calendar.timeInMillis
33 |
34 | // 23:59:59 999
35 | calendar[Calendar.HOUR_OF_DAY] = 23
36 | calendar[Calendar.MINUTE] = 59
37 | calendar[Calendar.SECOND] = 59
38 | calendar[Calendar.MILLISECOND] = 999
39 | var endTime = calendar.timeInMillis
40 |
41 | binding.timeBarVertical.setRange(startTime, endTime)
42 | binding.timeBarVertical.setMode(TimeRulerBarVertical.MODE_UINT_30_MIN)
43 | binding.timeBarVertical.setCursorValue(System.currentTimeMillis())
44 |
45 | binding.timeBarVertical.setOnCursorListener(object : BaseScaleBarVertical.OnCursorListener {
46 | override fun onStartTrackingTouch(cursorValue: Long) {
47 |
48 | }
49 |
50 | override fun onProgressChanged(cursorValue: Long, fromeUser: Boolean) {
51 | //监听时间戳变化
52 | binding.tvDataVertical.text = cursorDateFormat.format(Date(cursorValue))
53 | }
54 |
55 | override fun onStopTrackingTouch(cursorValue: Long) {
56 |
57 | }
58 | })
59 |
60 | binding.seekBarVertical.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
61 | override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
62 | binding.timeBarVertical.setScale(progress.toFloat())
63 | }
64 |
65 | override fun onStartTrackingTouch(seekBar: SeekBar?) {
66 | }
67 |
68 | override fun onStopTrackingTouch(seekBar: SeekBar?) {
69 | }
70 |
71 | })
72 |
73 | binding.seekAreaOffsetVertical.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
74 | override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
75 | binding.timeBarVertical.setVideoAreaOffset(progress)
76 | }
77 |
78 | override fun onStartTrackingTouch(seekBar: SeekBar?) {
79 | }
80 |
81 | override fun onStopTrackingTouch(seekBar: SeekBar?) {
82 | }
83 |
84 | })
85 |
86 | binding.btnDirVertical.setOnClickListener {
87 | binding.btnDirVertical.isSelected = !binding.btnDirVertical.isSelected
88 | binding.timeBarVertical.setTickDirection(binding.btnDirVertical.isSelected)
89 | }
90 |
91 | binding.btnShowCursorVertical.setOnClickListener {
92 | binding.btnShowCursorVertical.isSelected = !binding.btnShowCursorVertical.isSelected
93 | binding.timeBarVertical.setShowCursor(binding.btnShowCursorVertical.isSelected);
94 | }
95 |
96 | binding.btnPlayVertical.setOnClickListener {
97 | nowTime++
98 | binding.timeBarVertical.setCursorValue(System.currentTimeMillis() + 1000 * nowTime * 60)
99 | }
100 | setData()
101 |
102 | }
103 |
104 | private fun setData() {
105 | var videos = mutableListOf()
106 | var testTime = System.currentTimeMillis()
107 | for (i in 1..5) {
108 | val video = VideoBean(testTime, testTime + i * 10 * 60 * 1000, i % 2 == 0)
109 | videos.add(video)
110 | testTime += i * 15 * 60 * 1000
111 | }
112 | val timeBean = TimeBean(videos)
113 | binding.timeBarVertical.setColorScale(timeBean)
114 | }
115 | }
116 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_vertical_time_ruler.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
28 |
29 |
39 |
40 |
49 |
50 |
59 |
60 |
69 |
70 |
79 |
80 |
89 |
90 |
99 |
100 |
109 |
110 |
111 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
28 |
29 |
39 |
48 |
57 |
58 |
67 |
68 |
77 |
78 |
87 |
88 |
97 |
106 |
107 |
116 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/TimeRuler/src/main/java/com/kubi/timeruler/TimeRulerBar.java:
--------------------------------------------------------------------------------
1 | package com.kubi.timeruler;
2 |
3 | import android.content.Context;
4 | import android.content.res.TypedArray;
5 | import android.graphics.Canvas;
6 | import android.graphics.Color;
7 | import android.graphics.Paint;
8 | import android.graphics.Path;
9 | import android.graphics.Rect;
10 | import android.graphics.RectF;
11 |
12 | import android.util.AttributeSet;
13 |
14 |
15 | import androidx.annotation.ColorInt;
16 | import androidx.annotation.NonNull;
17 | import androidx.annotation.Nullable;
18 | import androidx.annotation.StringDef;
19 |
20 | import com.kubi.timeruler.utils.SizeUtils;
21 |
22 | import java.text.SimpleDateFormat;
23 |
24 | public class TimeRulerBar extends BaseScaleBar implements BaseScaleBar.TickMarkStrategy {
25 |
26 | public static final String MODE_UINT_1_MIN = "unit 1 minute";
27 | public static final String MODE_UINT_5_MIN = "unit 5 minute";
28 | public static final String MODE_UINT_10_MIN = "unit 10 minute";
29 | public static final String MODE_UINT_30_MIN = "unit 30 minute";
30 | public static final String MODE_UINT_1_HOUR = "unit 1 hour";
31 |
32 |
33 |
34 |
35 | @StringDef({MODE_UINT_1_MIN, MODE_UINT_5_MIN, MODE_UINT_10_MIN, MODE_UINT_30_MIN, MODE_UINT_1_HOUR})
36 | public @interface Mode {
37 | }
38 |
39 | private @Mode
40 | String mMode = MODE_UINT_1_MIN;
41 |
42 | public static final long MODE_UINT_1_MIN_VALUE = 30 * 60 * 1000;
43 | public static final long MODE_UINT_5_MIN_VALUE = 1 * 60 * 60 * 1000;
44 | public static final long MODE_UINT_10_MIN_VALUE = 2 * 60 * 60 * 1000;
45 | public static final long MODE_UINT_30_MIN_VALUE = 8 * 60 * 60 * 1000;
46 | public static final long MODE_UINT_1_HOUR_VALUE = 16 * 60 * 60 * 1000;
47 | private Paint mTickPaint;
48 | private Paint mColorCursorPaint;
49 | private float mTriangleHeight = 10;
50 | private int tickValueColor;
51 | private float tickValueSize;
52 | private int cursorBackgroundColor;
53 | private float cursorValueSize;
54 | private final int colorScaleBackground;
55 | private final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
56 | float tickValueBoundOffsetH = 20;
57 | private float videoAreaHeight;
58 | private float videoAreaOffset;
59 | private boolean drawCursorContent;
60 |
61 | public TimeRulerBar(Context context) {
62 | this(context, null);
63 | }
64 |
65 | public TimeRulerBar(Context context, @Nullable AttributeSet attrs) {
66 | super(context, attrs);
67 | TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TimeRulerBar);
68 | videoAreaHeight = typedArray.getDimension(R.styleable.TimeRulerBar_videoAreaHeight, SizeUtils.sp2px(getContext(), 20));
69 | videoAreaOffset = typedArray.getDimension(R.styleable.TimeRulerBar_videoAreaOffset, SizeUtils.sp2px(getContext(), 0));
70 | tickValueColor = typedArray.getColor(R.styleable.TimeRulerBar_tickValueColor, Color.BLACK);
71 | tickValueSize = typedArray.getDimension(R.styleable.TimeRulerBar_tickValueSize, SizeUtils.sp2px(getContext(), 8));
72 | cursorBackgroundColor = typedArray.getColor(R.styleable.TimeRulerBar_cursorBackgroundColor, Color.RED);
73 | cursorValueSize = typedArray.getDimension(R.styleable.TimeRulerBar_cursorValueSize, SizeUtils.sp2px(getContext(), 10));
74 | colorScaleBackground = typedArray.getColor(R.styleable.TimeRulerBar_colorScaleBackground, Color.WHITE);
75 | drawCursorContent = typedArray.getBoolean(R.styleable.TimeRulerBar_drawCursorContent, true);
76 | typedArray.recycle();
77 | init();
78 | }
79 |
80 | void init() {
81 | tickValueBoundOffsetH = SizeUtils.dp2px(getContext(), 6);
82 |
83 | mTickPaint = new Paint();
84 | mTickPaint.setColor(tickValueColor);
85 | mTickPaint.setAntiAlias(true);
86 | mTickPaint.setStyle(Paint.Style.FILL_AND_STROKE);
87 | mTickPaint.setTextAlign(Paint.Align.CENTER);
88 | mTickPaint.setTextSize(tickValueSize);
89 | mTickPaint.setStrokeWidth(1);
90 | mTickPaint.setDither(true);
91 |
92 | mColorCursorPaint = new Paint();
93 | mColorCursorPaint.setStyle(Paint.Style.FILL_AND_STROKE);
94 | mColorCursorPaint.setDither(true);
95 |
96 | setTickMarkStrategy(this);
97 | }
98 |
99 | public void setMode(@Mode String mode) {
100 | setMode(mode, true);
101 | }
102 |
103 | @Override
104 | public void setRange(long start, long end) {
105 | super.setRange(start, end);
106 | if (mMode != MODE_UINT_1_MIN) {
107 | setMode(mMode, true);
108 | }
109 | }
110 |
111 | public void setMode(@Mode String m, boolean setScaleRatio) {
112 | if (mMode == m) {
113 | // Log.e(TAG, " same mode.");
114 | return;
115 | }
116 | long spanValue;
117 | switch (m) {
118 | case MODE_UINT_1_MIN:
119 | this.mMode = m;
120 | updateScaleInfo(5 * 60 * 1000, 1 * 60 * 1000);
121 | spanValue = MODE_UINT_1_MIN_VALUE;
122 | break;
123 | case MODE_UINT_5_MIN:
124 | this.mMode = m;
125 | updateScaleInfo(10 * 60 * 1000, 5 * 60 * 1000);
126 | spanValue = MODE_UINT_5_MIN_VALUE;
127 | break;
128 | case MODE_UINT_10_MIN:
129 | this.mMode = m;
130 | updateScaleInfo(30 * 60 * 1000, 10 * 60 * 1000);
131 | spanValue = MODE_UINT_10_MIN_VALUE;
132 | break;
133 | case MODE_UINT_30_MIN:
134 | this.mMode = m;
135 | updateScaleInfo(1 * 60 * 60 * 1000, 30 * 60 * 1000);
136 | spanValue = MODE_UINT_30_MIN_VALUE;
137 | break;
138 | case MODE_UINT_1_HOUR:
139 | this.mMode = m;
140 | updateScaleInfo(2 * 60 * 60 * 1000, 1 * 60 * 60 * 1000);
141 | spanValue = MODE_UINT_1_HOUR_VALUE;
142 | break;
143 | default:
144 | throw new RuntimeException("not support mode: " + m);
145 | }
146 | // Log.e("TAG", " mode: " + mMode);
147 | if (setScaleRatio) {
148 | setScaleRatio(getMinScreenSpanValue() * 1.0f / spanValue);
149 | }
150 | }
151 |
152 | @Override
153 | public boolean disPlay(long scaleValue, boolean keyScale) {
154 | return keyScale;
155 | }
156 |
157 | @NonNull
158 | @Override
159 | public String getScaleValue(long scaleValue, boolean keyScale) {
160 | return simpleDateFormat.format(scaleValue);
161 | }
162 |
163 | @Override
164 | public int getColor(long scaleValue, boolean keyScale) {
165 | return tickValueColor;
166 | }
167 |
168 | @Override
169 | public float getSize(long scaleValue, boolean keyScale, float maxScaleValueSize) {
170 | return tickValueSize;
171 | }
172 |
173 | @Override
174 | protected void onEndTickDraw(Canvas canvas) {
175 | int startLimit = getScrollX();
176 | int endLimit = getScrollX() + getWidth();
177 | float startY = videoAreaOffset;
178 | float endY = videoAreaHeight + videoAreaOffset;
179 | // ① 绘制背景
180 | mColorCursorPaint.setColor(colorScaleBackground);
181 | canvas.drawRect(startLimit, startY, endLimit, endY, mColorCursorPaint);
182 | // 绘制颜色刻度尺
183 | if (null != mColorScale) {
184 | float cursorPosition = getCursorPosition();
185 | long cursorValue = getCursorValue();
186 | float unitPixel = getUnitPixel();
187 | int size = mColorScale.getSize();
188 | RectF rect = new RectF();
189 | rect.top = startY;
190 | rect.bottom = endY;
191 | long startValue;
192 | long endValue;
193 | float startPiexl;
194 | float endPiexl;
195 | // ② 绘制颜色刻度
196 | for (int i = 0; i < size; i++) {
197 | startValue = mColorScale.getStart(i);
198 | endValue = mColorScale.getEnd(i);
199 | startPiexl = cursorPosition + (startValue - cursorValue) * unitPixel;
200 | endPiexl = cursorPosition + (endValue - cursorValue) * unitPixel;
201 | if (endPiexl < startLimit) {
202 | continue;
203 | }
204 | if (startPiexl > endLimit) {
205 | continue;
206 | }
207 |
208 | rect.left = startPiexl;
209 | rect.right = endPiexl;
210 |
211 | mColorCursorPaint.setColor(mColorScale.getColor(i));
212 | canvas.drawRect(rect, mColorCursorPaint);
213 | }
214 | }
215 | }
216 |
217 | @Override
218 | protected int calcContentHeight(float baselinePositionProportion) {
219 | int contentHeight = super.calcContentHeight(baselinePositionProportion);
220 |
221 | mColorCursorPaint.setTextSize(cursorValueSize);
222 | Paint.FontMetrics fontMetrics = mColorCursorPaint.getFontMetrics();
223 | double ceil = Math.ceil(fontMetrics.bottom - fontMetrics.top);
224 | int cursorValueHeight = (int) (ceil + mTriangleHeight + tickValueBoundOffsetH) + 5;
225 | int cursorContentHeight = (int) ((getKeyTickHeight() + cursorValueHeight) / baselinePositionProportion + 0.5f);
226 | return Math.max(contentHeight, cursorContentHeight);
227 | }
228 |
229 | @Override
230 | protected void onScale(ScaleMode info, float unitPixel) {
231 | int width = getWidth();
232 | // 计算一屏刻度值跨度
233 | float screenSpanValue = width / unitPixel;
234 | updateMode(screenSpanValue);
235 | }
236 |
237 | public void setShowCursor(boolean isShowCursorContent) {
238 | drawCursorContent = isShowCursorContent;
239 | invalidate();
240 | }
241 |
242 | public void setVideoAreaOffset(int progress) {
243 | videoAreaOffset = progress;
244 | invalidate();
245 | }
246 |
247 | protected void updateMode(float screenSpanValue) {
248 | if (screenSpanValue > MODE_UINT_1_HOUR_VALUE) {
249 | setMode(MODE_UINT_1_HOUR, false);
250 | } else if (screenSpanValue > MODE_UINT_30_MIN_VALUE) {
251 | setMode(MODE_UINT_30_MIN, false);
252 | } else if (screenSpanValue > MODE_UINT_10_MIN_VALUE) {
253 | setMode(MODE_UINT_10_MIN, false);
254 | } else if (screenSpanValue > MODE_UINT_5_MIN_VALUE) {
255 | setMode(MODE_UINT_5_MIN, false);
256 | } else {
257 | setMode(MODE_UINT_1_MIN, false);
258 | }
259 | }
260 |
261 | SimpleDateFormat cursorDateFormat = new SimpleDateFormat("HH:mm:ss");
262 |
263 | /*可自行绘制浮标*/
264 | @Override
265 | protected void drawCursor(Canvas canvas, float cursorPosition, long cursorValue) {
266 | super.drawCursor(canvas, cursorPosition, cursorValue);
267 | if (!drawCursorContent) return;
268 | float keyTickHeight = getKeyTickHeight();
269 | float baselinePosition = getBaselinePosition();
270 | // ①绘制倒三角
271 | Path path = new Path();
272 | float startX = cursorPosition;
273 | float statY = baselinePosition - keyTickHeight;
274 | // 倒三角形顶边的 y
275 | float topSidePosition = statY - mTriangleHeight;
276 | path.moveTo(startX, statY);
277 | path.lineTo(startX - 3.5f, topSidePosition);
278 | path.lineTo(startX + 3.5f, topSidePosition);
279 | path.close();
280 | mTickPaint.setColor(cursorBackgroundColor);
281 | canvas.drawPath(path, mTickPaint);
282 |
283 | String content = cursorDateFormat.format(cursorValue);
284 | Rect textBound = new Rect();
285 | mTickPaint.setTextSize(cursorValueSize);
286 | // 测量内容大小
287 | mTickPaint.getTextBounds(content, 0, content.length(), textBound);
288 |
289 | // ②绘制内容背景
290 | // 创建包裹内容的背景大小
291 | RectF rectF = new RectF(0, 0, textBound.width() + 20, textBound.height() + tickValueBoundOffsetH);
292 | // 背景位置
293 | // x方向: 关于游标居中 y方向:在倒三角形上边
294 | rectF.offset(cursorPosition - rectF.width() * 0.5f, topSidePosition + 0.5f - rectF.height());
295 | float rx = rectF.width() * 0.5f;
296 | float ry = rx;
297 | mTickPaint.setColor(cursorBackgroundColor);
298 | canvas.drawRoundRect(rectF, rx, ry, mTickPaint);
299 |
300 | mTickPaint.setColor(tickValueColor);
301 | // ③ 绘制内容
302 | // 使内容绘制在背景内,达到包裹效果
303 | float textY = rectF.centerY() + textBound.height() * 0.5f;
304 | canvas.drawText(content, cursorPosition, textY, mTickPaint);
305 | }
306 |
307 | private ColorScale mColorScale;
308 |
309 | public void setColorScale(ColorScale scale) {
310 | this.mColorScale = scale;
311 | }
312 |
313 | public interface ColorScale {
314 | /*需要绘制的颜色数量*/
315 | int getSize();
316 |
317 | /*开始时间*/
318 | long getStart(int index);
319 |
320 | /*结束时间*/
321 | long getEnd(int index);
322 |
323 | /*绘制的颜色*/
324 | @ColorInt
325 | int getColor(int index);
326 | }
327 |
328 |
329 | }
330 |
--------------------------------------------------------------------------------
/TimeRuler/src/main/java/com/kubi/timeruler/TimeRulerBarVertical.java:
--------------------------------------------------------------------------------
1 | package com.kubi.timeruler;
2 |
3 | import android.content.Context;
4 | import android.content.res.TypedArray;
5 | import android.graphics.Canvas;
6 | import android.graphics.Color;
7 | import android.graphics.Paint;
8 | import android.graphics.Path;
9 | import android.graphics.Rect;
10 | import android.graphics.RectF;
11 |
12 | import android.util.AttributeSet;
13 |
14 |
15 | import androidx.annotation.ColorInt;
16 | import androidx.annotation.NonNull;
17 | import androidx.annotation.Nullable;
18 | import androidx.annotation.StringDef;
19 |
20 | import com.kubi.timeruler.utils.SizeUtils;
21 |
22 | import java.text.SimpleDateFormat;
23 |
24 | public class TimeRulerBarVertical extends BaseScaleBarVertical implements BaseScaleBarVertical.TickMarkStrategy {
25 |
26 | public static final String MODE_UINT_1_MIN = "unit 1 minute";
27 | public static final String MODE_UINT_5_MIN = "unit 5 minute";
28 | public static final String MODE_UINT_10_MIN = "unit 10 minute";
29 | public static final String MODE_UINT_30_MIN = "unit 30 minute";
30 | public static final String MODE_UINT_1_HOUR = "unit 1 hour";
31 |
32 |
33 |
34 |
35 | @StringDef({MODE_UINT_1_MIN, MODE_UINT_5_MIN, MODE_UINT_10_MIN, MODE_UINT_30_MIN, MODE_UINT_1_HOUR})
36 | public @interface Mode {
37 | }
38 |
39 | private @Mode
40 | String mMode = MODE_UINT_1_MIN;
41 |
42 | public static final long MODE_UINT_1_MIN_VALUE = 30 * 60 * 1000;
43 | public static final long MODE_UINT_5_MIN_VALUE = 1 * 60 * 60 * 1000;
44 | public static final long MODE_UINT_10_MIN_VALUE = 2 * 60 * 60 * 1000;
45 | public static final long MODE_UINT_30_MIN_VALUE = 8 * 60 * 60 * 1000;
46 | public static final long MODE_UINT_1_HOUR_VALUE = 16 * 60 * 60 * 1000;
47 | private Paint mTickPaint;
48 | private Paint mColorCursorPaint;
49 | private float mTriangleWidth = 10;
50 | private int tickValueColor;
51 | private float tickValueSize;
52 | private int cursorBackgroundColor;
53 | private float cursorValueSize;
54 | private final int colorScaleBackground;
55 | private final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
56 | float tickValueBoundOffsetV = 20;
57 | private float videoAreaWidth;
58 | private float videoAreaOffset;
59 | private boolean drawCursorContent;
60 |
61 | public TimeRulerBarVertical(Context context) {
62 | this(context, null);
63 | }
64 |
65 | public TimeRulerBarVertical(Context context, @Nullable AttributeSet attrs) {
66 | super(context, attrs);
67 | TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TimeRulerBarVertical);
68 | videoAreaWidth = typedArray.getDimension(R.styleable.TimeRulerBarVertical_videoAreaWidthVertical, SizeUtils.sp2px(getContext(), 20));
69 | videoAreaOffset = typedArray.getDimension(R.styleable.TimeRulerBarVertical_videoAreaOffsetVertical, SizeUtils.sp2px(getContext(), 0));
70 | tickValueColor = typedArray.getColor(R.styleable.TimeRulerBarVertical_tickValueColorVertical, Color.BLACK);
71 | tickValueSize = typedArray.getDimension(R.styleable.TimeRulerBarVertical_tickValueSizeVertical, SizeUtils.sp2px(getContext(), 8));
72 | cursorBackgroundColor = typedArray.getColor(R.styleable.TimeRulerBarVertical_cursorBackgroundColorVertical, Color.RED);
73 | cursorValueSize = typedArray.getDimension(R.styleable.TimeRulerBarVertical_cursorValueSizeVertical, SizeUtils.sp2px(getContext(), 10));
74 | colorScaleBackground = typedArray.getColor(R.styleable.TimeRulerBarVertical_colorScaleBackgroundVertical, Color.WHITE);
75 | drawCursorContent = typedArray.getBoolean(R.styleable.TimeRulerBarVertical_drawCursorContentVertical, true);
76 | typedArray.recycle();
77 | init();
78 | }
79 |
80 | void init() {
81 | tickValueBoundOffsetV = SizeUtils.dp2px(getContext(), 6);
82 |
83 | mTickPaint = new Paint();
84 | mTickPaint.setColor(tickValueColor);
85 | mTickPaint.setAntiAlias(true);
86 | mTickPaint.setStyle(Paint.Style.FILL_AND_STROKE);
87 | mTickPaint.setTextAlign(Paint.Align.CENTER);
88 | mTickPaint.setTextSize(tickValueSize);
89 | mTickPaint.setStrokeWidth(1);
90 | mTickPaint.setDither(true);
91 |
92 | mColorCursorPaint = new Paint();
93 | mColorCursorPaint.setStyle(Paint.Style.FILL_AND_STROKE);
94 | mColorCursorPaint.setDither(true);
95 |
96 | setTickMarkStrategy(this);
97 | }
98 |
99 | /**
100 | * 设置时间模式
101 | * @param mode 时间模式,支持1分钟、5分钟、10分钟、30分钟、1小时
102 | */
103 | public void setMode(@Mode String mode) {
104 | setMode(mode, true);
105 | }
106 |
107 | @Override
108 | public void setRange(long start, long end) {
109 | super.setRange(start, end);
110 | if (mMode != MODE_UINT_1_MIN) {
111 | setMode(mMode, true);
112 | }
113 | }
114 |
115 | /**
116 | * 设置时间模式
117 | * @param m 时间模式
118 | * @param setScaleRatio 是否设置缩放比例
119 | */
120 | public void setMode(@Mode String m, boolean setScaleRatio) {
121 | if (mMode == m) {
122 | // Log.e(TAG, " same mode.");
123 | return;
124 | }
125 | long spanValue;
126 | switch (m) {
127 | case MODE_UINT_1_MIN:
128 | this.mMode = m;
129 | updateScaleInfo(5 * 60 * 1000, 1 * 60 * 1000);
130 | spanValue = MODE_UINT_1_MIN_VALUE;
131 | break;
132 | case MODE_UINT_5_MIN:
133 | this.mMode = m;
134 | updateScaleInfo(10 * 60 * 1000, 5 * 60 * 1000);
135 | spanValue = MODE_UINT_5_MIN_VALUE;
136 | break;
137 | case MODE_UINT_10_MIN:
138 | this.mMode = m;
139 | updateScaleInfo(30 * 60 * 1000, 10 * 60 * 1000);
140 | spanValue = MODE_UINT_10_MIN_VALUE;
141 | break;
142 | case MODE_UINT_30_MIN:
143 | this.mMode = m;
144 | updateScaleInfo(1 * 60 * 60 * 1000, 30 * 60 * 1000);
145 | spanValue = MODE_UINT_30_MIN_VALUE;
146 | break;
147 | case MODE_UINT_1_HOUR:
148 | this.mMode = m;
149 | updateScaleInfo(2 * 60 * 60 * 1000, 1 * 60 * 60 * 1000);
150 | spanValue = MODE_UINT_1_HOUR_VALUE;
151 | break;
152 | default:
153 | throw new RuntimeException("not support mode: " + m);
154 | }
155 | // Log.e("TAG", " mode: " + mMode);
156 | if (setScaleRatio) {
157 | setScaleRatio(getMinScreenSpanValue() * 1.0f / spanValue);
158 | }
159 | }
160 |
161 | @Override
162 | public boolean disPlay(long scaleValue, boolean keyScale) {
163 | return keyScale;
164 | }
165 |
166 | @NonNull
167 | @Override
168 | public String getScaleValue(long scaleValue, boolean keyScale) {
169 | return simpleDateFormat.format(scaleValue);
170 | }
171 |
172 | @Override
173 | public int getColor(long scaleValue, boolean keyScale) {
174 | return tickValueColor;
175 | }
176 |
177 | @Override
178 | public float getSize(long scaleValue, boolean keyScale, float maxScaleValueSize) {
179 | return tickValueSize;
180 | }
181 |
182 | @Override
183 | protected void onEndTickDraw(Canvas canvas) {
184 | // 由于画布已经旋转90度,坐标系已经转换:
185 | // 原来的X轴现在是Y轴,原来的Y轴现在是X轴
186 | // 所以需要交换坐标和尺寸
187 | int startLimit = getScrollY();
188 | int endLimit = getScrollY() + getHeight();
189 | float startY = videoAreaOffset; // 在旋转后的坐标系中,这是Y坐标
190 | float endY = videoAreaWidth + videoAreaOffset; // 在旋转后的坐标系中,这是Y坐标
191 | // ① 绘制背景
192 | mColorCursorPaint.setColor(colorScaleBackground);
193 | canvas.drawRect(startLimit, startY, endLimit, endY, mColorCursorPaint);
194 | // 绘制颜色刻度尺
195 | if (null != mColorScale) {
196 | float cursorPosition = getCursorPosition();
197 | long cursorValue = getCursorValue();
198 | float unitPixel = getUnitPixel();
199 | int size = mColorScale.getSize();
200 | RectF rect = new RectF();
201 | rect.top = startY;
202 | rect.bottom = endY;
203 | long startValue;
204 | long endValue;
205 | float startPiexl;
206 | float endPiexl;
207 | // ② 绘制颜色刻度
208 | for (int i = 0; i < size; i++) {
209 | startValue = mColorScale.getStart(i);
210 | endValue = mColorScale.getEnd(i);
211 | startPiexl = cursorPosition + (startValue - cursorValue) * unitPixel;
212 | endPiexl = cursorPosition + (endValue - cursorValue) * unitPixel;
213 | if (endPiexl < startLimit) {
214 | continue;
215 | }
216 | if (startPiexl > endLimit) {
217 | continue;
218 | }
219 |
220 | rect.left = startPiexl;
221 | rect.right = endPiexl;
222 |
223 | mColorCursorPaint.setColor(mColorScale.getColor(i));
224 | canvas.drawRect(rect, mColorCursorPaint);
225 | }
226 | }
227 | }
228 |
229 | @Override
230 | protected int calcContentWidth(float baselinePositionProportion) {
231 | int contentWidth = super.calcContentWidth(baselinePositionProportion);
232 |
233 | mColorCursorPaint.setTextSize(cursorValueSize);
234 | Paint.FontMetrics fontMetrics = mColorCursorPaint.getFontMetrics();
235 | double ceil = Math.ceil(fontMetrics.bottom - fontMetrics.top);
236 | int cursorValueWidth = (int) (ceil + mTriangleWidth + tickValueBoundOffsetV) + 5;
237 | int cursorContentWidth = (int) ((getKeyTickWidth() + cursorValueWidth) / baselinePositionProportion + 0.5f);
238 | return Math.max(contentWidth, cursorContentWidth);
239 | }
240 |
241 | @Override
242 | protected void onScale(ScaleMode info, float unitPixel) {
243 | int height = getHeight();
244 | // 计算一屏刻度值跨度
245 | float screenSpanValue = height / unitPixel;
246 | updateMode(screenSpanValue);
247 | }
248 |
249 | /**
250 | * 设置是否显示游标内容
251 | * @param isShowCursorContent true显示游标内容,false不显示
252 | */
253 | public void setShowCursor(boolean isShowCursorContent) {
254 | drawCursorContent = isShowCursorContent;
255 | invalidate();
256 | }
257 |
258 | /**
259 | * 设置视频区域偏移量
260 | * @param progress 偏移量值
261 | */
262 | public void setVideoAreaOffset(int progress) {
263 | videoAreaOffset = progress;
264 | invalidate();
265 | }
266 |
267 | protected void updateMode(float screenSpanValue) {
268 | if (screenSpanValue > MODE_UINT_1_HOUR_VALUE) {
269 | setMode(MODE_UINT_1_HOUR, false);
270 | } else if (screenSpanValue > MODE_UINT_30_MIN_VALUE) {
271 | setMode(MODE_UINT_30_MIN, false);
272 | } else if (screenSpanValue > MODE_UINT_10_MIN_VALUE) {
273 | setMode(MODE_UINT_10_MIN, false);
274 | } else if (screenSpanValue > MODE_UINT_5_MIN_VALUE) {
275 | setMode(MODE_UINT_5_MIN, false);
276 | } else {
277 | setMode(MODE_UINT_1_MIN, false);
278 | }
279 | }
280 |
281 | SimpleDateFormat cursorDateFormat = new SimpleDateFormat("HH:mm:ss");
282 |
283 | /**
284 | * 绘制纵向游标
285 | * 由于画布已旋转90度,需要适配纵向布局
286 | */
287 | @Override
288 | protected void drawCursor(Canvas canvas, float cursorPosition, long cursorValue) {
289 | super.drawCursor(canvas, cursorPosition, cursorValue);
290 | if (!drawCursorContent) return;
291 | float keyTickWidth = getKeyTickWidth();
292 | float baselinePosition = getBaselinePosition();
293 | // ①绘制倒三角 - 适配纵向布局
294 | Path path = new Path();
295 | float startX = cursorPosition; // 在旋转后的坐标系中,这是X坐标
296 | float statY = baselinePosition - keyTickWidth; // 在旋转后的坐标系中,这是Y坐标
297 | // 倒三角形顶边的 y
298 | float topSidePosition = statY - mTriangleWidth;
299 | path.moveTo(startX, statY);
300 | path.lineTo(startX - 3.5f, topSidePosition);
301 | path.lineTo(startX + 3.5f, topSidePosition);
302 | path.close();
303 | mTickPaint.setColor(cursorBackgroundColor);
304 | canvas.drawPath(path, mTickPaint);
305 |
306 | String content = cursorDateFormat.format(cursorValue);
307 | Rect textBound = new Rect();
308 | mTickPaint.setTextSize(cursorValueSize);
309 | // 测量内容大小
310 | mTickPaint.getTextBounds(content, 0, content.length(), textBound);
311 |
312 | // ②绘制内容背景
313 | // 创建包裹内容的背景大小
314 | RectF rectF = new RectF(0, 0, textBound.width() + 20, textBound.height() + tickValueBoundOffsetV);
315 | // 背景位置 - 适配纵向布局
316 | // x方向:关于游标居中 y方向: 在倒三角形上边
317 | rectF.offset(cursorPosition - rectF.width() * 0.5f, topSidePosition + 0.5f - rectF.height());
318 | float rx = rectF.width() * 0.5f;
319 | float ry = rx;
320 | mTickPaint.setColor(cursorBackgroundColor);
321 | canvas.drawRoundRect(rectF, rx, ry, mTickPaint);
322 |
323 | mTickPaint.setColor(tickValueColor);
324 | // ③ 绘制内容
325 | // 使内容绘制在背景内,达到包裹效果
326 | float textY = rectF.centerY() + textBound.height() * 0.5f;
327 | canvas.drawText(content, cursorPosition, textY, mTickPaint);
328 | }
329 |
330 | private TimeRulerBar.ColorScale mColorScale;
331 |
332 | /**
333 | * 设置颜色刻度尺数据
334 | * @param scale 颜色刻度尺数据源
335 | */
336 | public void setColorScale(TimeRulerBar.ColorScale scale) {
337 | this.mColorScale = scale;
338 | }
339 |
340 |
341 |
342 |
343 | }
344 |
--------------------------------------------------------------------------------
/TimeRuler/src/main/java/com/kubi/timeruler/BaseScaleBar.java:
--------------------------------------------------------------------------------
1 | package com.kubi.timeruler;
2 |
3 | import android.content.Context;
4 | import android.content.res.Resources;
5 | import android.content.res.TypedArray;
6 | import android.graphics.Canvas;
7 | import android.graphics.Color;
8 | import android.graphics.Paint;
9 |
10 | import android.util.AttributeSet;
11 | import android.util.Log;
12 | import android.view.GestureDetector;
13 | import android.view.MotionEvent;
14 | import android.view.ScaleGestureDetector;
15 | import android.view.View;
16 | import android.widget.Scroller;
17 |
18 |
19 | import androidx.annotation.ColorInt;
20 | import androidx.annotation.Dimension;
21 | import androidx.annotation.FloatRange;
22 | import androidx.annotation.NonNull;
23 | import androidx.annotation.Nullable;
24 | import androidx.core.view.GestureDetectorCompat;
25 |
26 | import com.kubi.timeruler.utils.SizeUtils;
27 |
28 | import java.util.Calendar;
29 |
30 | public class BaseScaleBar extends View implements ScaleGestureDetector.OnScaleGestureListener,
31 | GestureDetector.OnGestureListener {
32 | private static final String TAG = "BaseScaleBar";
33 | /* 缩放比例*/
34 | private float mScaleRatio = 1.0f;
35 | /* 关键刻度高度*/
36 | private float mKeyTickHeight;
37 | /* 刻度高度*/
38 | private float mTickHeight;
39 | /* 普通刻度线与关键刻度线的比*/
40 | private float mNormalTickAndKeyTickRatio;
41 | /* 刻度间距*/
42 | private float mTickSpacing;
43 | private ScaleGestureDetector mScaleGestureDetector;
44 | private Paint mScalePaint;
45 | private ScaleMode mScaleInfo;
46 | private float unitPixel;
47 | //
48 | private long minScreenSpanValue;
49 | private long maxScreenSpanValue;
50 | private long mCursorValue;
51 | private GestureDetectorCompat mGestureDetectorCompat;
52 | private boolean scrollHappened;
53 | private float mCursorPosition;
54 | private float mCursorPositionProportion = 0.5f;
55 | // 刻度尺横线位置
56 | private float mBaselinePosition;
57 | private float mBaselinePositionProportion = 0.67f;
58 | private float maxUnitPixel;
59 | private float minUnitPixel;
60 | private int tickColor;
61 | private boolean tickDirectionUp;//刻度线方向
62 | private final int cursorLineColor;
63 | private boolean showCursorLine;
64 | private boolean showTickValue;
65 | private boolean showTickLine;
66 | private Scroller mScroller;
67 | /**
68 | * 状态
69 | */
70 | private int status = STATUS_NONE;
71 | /**
72 | * 默认状态
73 | */
74 | public static final int STATUS_NONE = 0;
75 |
76 | /**
77 | * 按下
78 | */
79 | public static final int STATUS_DOWN = 1;
80 | /**
81 | * 拖拽滚动
82 | */
83 | public static final int STATUS_SCROLL = STATUS_DOWN + 1;
84 | /**
85 | * 甩动滚动(惯性)
86 | */
87 | public static final int STATUS_SCROLL_FLING = STATUS_SCROLL + 1;
88 | /**
89 | * 缩放
90 | */
91 | public static final int STATUS_ZOOM = STATUS_SCROLL_FLING + 1;
92 | /* 刻度值 最大规格*/
93 | private float maxScaleValueSize;
94 | /*文字偏移量*/
95 | private float tickValueOffset;
96 |
97 | public BaseScaleBar(Context context) {
98 | this(context, null);
99 | }
100 |
101 | public BaseScaleBar(Context context, @Nullable AttributeSet attrs) {
102 | super(context, attrs);
103 | TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.BaseScaleBar);
104 | mKeyTickHeight = typedArray.getDimension(R.styleable.BaseScaleBar_keyTickHeight, SizeUtils.dp2px(getContext(), 10));
105 | tickValueOffset = typedArray.getDimension(R.styleable.BaseScaleBar_tickValueOffset, SizeUtils.dp2px(getContext(), -30));
106 | tickDirectionUp = typedArray.getBoolean(R.styleable.BaseScaleBar_tickDirectionUp, true);
107 | mNormalTickAndKeyTickRatio = typedArray.getFloat(R.styleable.BaseScaleBar_normalTickRatio, 0.67f);
108 |
109 | tickColor = typedArray.getColor(R.styleable.BaseScaleBar_tickColor, Color.BLACK);
110 | cursorLineColor = typedArray.getColor(R.styleable.BaseScaleBar_cursorLineColor, Color.YELLOW);
111 | showCursorLine = typedArray.getBoolean(R.styleable.BaseScaleBar_showCursorLine, true);
112 | showTickValue = typedArray.getBoolean(R.styleable.BaseScaleBar_showCursorLine, true);
113 | showTickLine = typedArray.getBoolean(R.styleable.BaseScaleBar_showTickLine, true);
114 | maxScaleValueSize = typedArray.getDimension(R.styleable.BaseScaleBar_maxScaleValueSize, SizeUtils.sp2px(getContext(), 15));
115 |
116 | float position = typedArray.getFloat(R.styleable.BaseScaleBar_cursorPosition, 0.5f);
117 | if (position != 0) {
118 | mCursorPositionProportion = position;
119 | }
120 | position = typedArray.getFloat(R.styleable.BaseScaleBar_baselinePosition, 0.67f);
121 | if (position != 0) {
122 | mBaselinePositionProportion = position;
123 | }
124 | // 释放
125 | typedArray.recycle();
126 |
127 |
128 | init();
129 | }
130 |
131 | private void init() {
132 | mScalePaint = new Paint();
133 | mScalePaint.setAntiAlias(true);
134 | mScalePaint.setStrokeWidth(1.0f);
135 | mScalePaint.setDither(true);
136 | mScalePaint.setStyle(Paint.Style.FILL_AND_STROKE);
137 |
138 | minScreenSpanValue = 30 * 60 * 1000;
139 | maxScreenSpanValue = 24 * 60 * 60 * 1000;
140 | mScaleInfo = new ScaleMode();
141 | mScaleInfo.unitValue = 60000;
142 | mScaleInfo.keyScaleRange = 5 * 60 * 1000;
143 | Calendar calendar = Calendar.getInstance();
144 | calendar.set(Calendar.HOUR_OF_DAY, 0);
145 | calendar.set(Calendar.MINUTE, 0);
146 | calendar.set(Calendar.SECOND, 0);
147 | mScaleInfo.startValue = calendar.getTimeInMillis();
148 | calendar.set(Calendar.HOUR_OF_DAY, 23);
149 | calendar.set(Calendar.MINUTE, 59);
150 | calendar.set(Calendar.SECOND, 59);
151 | mScaleInfo.endValue = calendar.getTimeInMillis();
152 | mCursorValue = mScaleInfo.startValue;
153 | }
154 |
155 | protected void setScaleRatio(@FloatRange(from = 0.0f, to = 1.0f, fromInclusive = false) float scaleRatio) {
156 | this.mScaleRatio = scaleRatio;
157 | }
158 |
159 | protected long getMaxScreenSpanValue() {
160 | return maxScreenSpanValue;
161 | }
162 |
163 | protected long getMinScreenSpanValue() {
164 | return minScreenSpanValue;
165 | }
166 |
167 | protected float getKeyTickHeight() {
168 | return mKeyTickHeight;
169 | }
170 |
171 | protected float getCursorPosition() {
172 | return mCursorPosition;
173 | }
174 |
175 | public long getCursorValue() {
176 | return mCursorValue;
177 | }
178 |
179 | protected float getUnitPixel() {
180 | return unitPixel;
181 | }
182 |
183 | @Override
184 | protected void onSizeChanged(int w, int h, int oldw, int oldh) {
185 | mTickHeight = mKeyTickHeight * mNormalTickAndKeyTickRatio;
186 | mCursorPosition = w * mCursorPositionProportion;
187 | mBaselinePosition = h * mBaselinePositionProportion;
188 |
189 | maxUnitPixel = w * 1.0f / minScreenSpanValue;
190 | minUnitPixel = w * 1.0f / maxScreenSpanValue;
191 |
192 | unitPixel = maxUnitPixel * mScaleRatio;
193 | mTickSpacing = mScaleInfo.unitValue * unitPixel;
194 | }
195 |
196 | public void setRange(long start, long end) {
197 | if (start >= end) {
198 | return;
199 | }
200 | mScaleInfo.startValue = start;
201 | mScaleInfo.endValue = end;
202 | mCursorValue = start;
203 | invalidate();
204 | }
205 |
206 | public void setCursorValue(long cursorValue) {
207 | if (status != STATUS_NONE) {
208 | return;
209 | }
210 | if (cursorValue < mScaleInfo.startValue || cursorValue > mScaleInfo.endValue) {
211 | return;
212 | }
213 | this.mCursorValue = cursorValue;
214 | if (mOnCursorListener != null) mOnCursorListener.onProgressChanged(mCursorValue, false);
215 | invalidate();
216 | }
217 |
218 | protected void updateScaleInfo(long keyScaleRange, long unitValue) {
219 | mScaleInfo.keyScaleRange = keyScaleRange;
220 | mScaleInfo.unitValue = unitValue;
221 | }
222 |
223 | protected float getBaselinePosition() {
224 | return mBaselinePosition;
225 | }
226 |
227 | @Override
228 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
229 | setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec), getHeightSize(getSuggestedMinimumHeight(), heightMeasureSpec));
230 | }
231 |
232 | private int getHeightSize(int size, int heightMeasureSpec) {
233 | int result = size;
234 | int contentHeight = calcContentHeight(mBaselinePositionProportion);
235 | int specMode = MeasureSpec.getMode(heightMeasureSpec);
236 | int specSize = MeasureSpec.getSize(heightMeasureSpec);
237 | switch (specMode) {
238 | case MeasureSpec.UNSPECIFIED:
239 | result = size > contentHeight ? size : contentHeight;
240 | break;
241 | case MeasureSpec.AT_MOST:
242 | result = contentHeight;
243 | break;
244 | case MeasureSpec.EXACTLY:
245 | result = specSize > contentHeight ? specSize : contentHeight;
246 | break;
247 | }
248 | return result;
249 | }
250 |
251 | protected int calcContentHeight(float baselinePositionProportion) {
252 | int tickValueHeight = 0;
253 | if (showTickValue && null != mTickMarkStrategy) {
254 | mScalePaint.setTextSize(maxScaleValueSize);
255 | Paint.FontMetrics fontMetrics = mScalePaint.getFontMetrics();
256 | double ceil = Math.ceil(fontMetrics.bottom - fontMetrics.top);
257 | tickValueHeight = (int) (ceil + tickValueOffset);
258 | }
259 | return (int) ((mKeyTickHeight + tickValueHeight) / baselinePositionProportion + 0.5f);
260 | }
261 |
262 | @Override
263 | protected void onDraw(Canvas canvas) {
264 |
265 | float baselinePosition = getBaselinePosition();
266 | mScalePaint.setColor(tickColor);
267 | if (showTickLine) {
268 | canvas.drawLine(getScrollX(), baselinePosition, getScrollX() + getWidth(), baselinePosition, mScalePaint);
269 | }
270 | long leftRange = mCursorValue - mScaleInfo.startValue;
271 | long leftNeighborOffest = leftRange % mScaleInfo.unitValue;
272 | long leftNeighborTickValue = mCursorValue - leftNeighborOffest;
273 | float leftNeighborPosition = mCursorPosition - leftNeighborOffest * unitPixel;
274 | int leftCount = (int) (mCursorPosition / mTickSpacing + 0.5f);
275 | float onDrawTickPosition;
276 | long onDrawTickValue;
277 | for (int i = 0; i < leftCount; i++) {
278 | onDrawTickValue = leftNeighborTickValue - mScaleInfo.unitValue * i;
279 | if (onDrawTickValue < mScaleInfo.startValue) {
280 | break;
281 | }
282 | onDrawTickPosition = leftNeighborPosition - mTickSpacing * i;
283 | if (tickDirectionUp) {
284 | if ((onDrawTickValue - mScaleInfo.startValue) % mScaleInfo.keyScaleRange == 0) {
285 | canvas.drawLine(onDrawTickPosition, baselinePosition, onDrawTickPosition, baselinePosition - mKeyTickHeight, mScalePaint);
286 | drawTickValue(canvas, onDrawTickPosition, baselinePosition - mKeyTickHeight, onDrawTickValue, true);
287 | } else {
288 | canvas.drawLine(onDrawTickPosition, baselinePosition - mTickHeight, onDrawTickPosition, baselinePosition, mScalePaint);
289 | drawTickValue(canvas, onDrawTickPosition, baselinePosition - mTickHeight, onDrawTickValue, false);
290 | }
291 | } else {
292 | if ((onDrawTickValue - mScaleInfo.startValue) % mScaleInfo.keyScaleRange == 0) {
293 | canvas.drawLine(onDrawTickPosition, baselinePosition, onDrawTickPosition, baselinePosition + mKeyTickHeight, mScalePaint);
294 | drawTickValue(canvas, onDrawTickPosition, baselinePosition - mKeyTickHeight, onDrawTickValue, true);
295 | } else {
296 | canvas.drawLine(onDrawTickPosition, baselinePosition, onDrawTickPosition, baselinePosition + mTickHeight, mScalePaint);
297 | drawTickValue(canvas, onDrawTickPosition, baselinePosition - mTickHeight, onDrawTickValue, false);
298 | }
299 | }
300 |
301 | }
302 |
303 | long rightNeighborTickValue = leftNeighborTickValue + mScaleInfo.unitValue;
304 | float rightNeighborPosition = leftNeighborPosition + mTickSpacing;
305 | int rightCount = (int) ((getWidth() - mCursorPosition) / mTickSpacing + 0.5f);
306 | for (int i = 0; i < rightCount; i++) {
307 | onDrawTickValue = rightNeighborTickValue + mScaleInfo.unitValue * i;
308 | if (onDrawTickValue > mScaleInfo.endValue) {
309 | break;
310 | }
311 | onDrawTickPosition = rightNeighborPosition + mTickSpacing * i;
312 | if (tickDirectionUp) {
313 | if ((onDrawTickValue - mScaleInfo.startValue) % mScaleInfo.keyScaleRange == 0) {
314 | canvas.drawLine(onDrawTickPosition, baselinePosition, onDrawTickPosition, baselinePosition - mKeyTickHeight, mScalePaint);
315 | drawTickValue(canvas, onDrawTickPosition, baselinePosition - mKeyTickHeight, onDrawTickValue, true);
316 | } else {
317 | canvas.drawLine(onDrawTickPosition, baselinePosition, onDrawTickPosition, baselinePosition - mTickHeight, mScalePaint);
318 | drawTickValue(canvas, onDrawTickPosition, baselinePosition - mTickHeight, onDrawTickValue, false);
319 | }
320 | } else {
321 | if ((onDrawTickValue - mScaleInfo.startValue) % mScaleInfo.keyScaleRange == 0) {
322 | canvas.drawLine(onDrawTickPosition, baselinePosition, onDrawTickPosition, baselinePosition + mKeyTickHeight, mScalePaint);
323 | drawTickValue(canvas, onDrawTickPosition, baselinePosition - mKeyTickHeight, onDrawTickValue, true);
324 | } else {
325 | canvas.drawLine(onDrawTickPosition, baselinePosition, onDrawTickPosition, baselinePosition + mTickHeight, mScalePaint);
326 | drawTickValue(canvas, onDrawTickPosition, baselinePosition - mTickHeight, onDrawTickValue, false);
327 | }
328 | }
329 |
330 | }
331 |
332 | onEndTickDraw(canvas);
333 |
334 | drawCursor(canvas, mCursorPosition, mCursorValue);
335 | }
336 |
337 | protected void onEndTickDraw(Canvas canvas) {
338 | }
339 |
340 | /**
341 | * 绘制游标
342 | *
343 | * @param canvas
344 | * @param cursorPosition
345 | */
346 | protected void drawCursor(Canvas canvas, float cursorPosition, long cursorValue) {
347 | if (showCursorLine) {
348 | mScalePaint.setColor(cursorLineColor);
349 | canvas.drawLine(cursorPosition, 0, cursorPosition, getHeight(), mScalePaint);
350 | }
351 | }
352 |
353 | /**
354 | * 绘制刻度线描述
355 | *
356 | * @param canvas
357 | * @param x
358 | * @param y
359 | * @param scaleValue
360 | * @param keyScale
361 | */
362 | private void drawTickValue(Canvas canvas, float x, float y, long scaleValue, boolean keyScale) {
363 | if (showTickValue) {
364 | if (null != mTickMarkStrategy) {
365 | if (mTickMarkStrategy.disPlay(scaleValue, keyScale)) {
366 | mScalePaint.setColor(mTickMarkStrategy.getColor(scaleValue, keyScale));
367 | mScalePaint.setTextAlign(Paint.Align.CENTER);
368 | float size = mTickMarkStrategy.getSize(scaleValue, keyScale, maxScaleValueSize);
369 | size = Math.min(maxScaleValueSize, size);
370 | mScalePaint.setTextSize(size);
371 | canvas.drawText(mTickMarkStrategy.getScaleValue(scaleValue, keyScale), x, y - tickValueOffset, mScalePaint);
372 | }
373 | }
374 | }
375 | }
376 |
377 | ScaleGestureDetector getScaleGestureDetect() {
378 | if (null == mScaleGestureDetector) {
379 | mScaleGestureDetector = new ScaleGestureDetector(getContext(), this);
380 | }
381 | return mScaleGestureDetector;
382 | }
383 |
384 | GestureDetectorCompat getGestureDetectorCompat() {
385 | if (null == mGestureDetectorCompat) {
386 | mGestureDetectorCompat = new GestureDetectorCompat(getContext(), this);
387 | }
388 | return mGestureDetectorCompat;
389 | }
390 |
391 | Scroller getScroller() {
392 | if (null == mScroller) {
393 | mScroller = new Scroller(getContext());
394 | }
395 | return mScroller;
396 | }
397 |
398 | @Override
399 | public boolean onTouchEvent(MotionEvent event) {
400 | getScaleGestureDetect().onTouchEvent(event);
401 | getGestureDetectorCompat().onTouchEvent(event);
402 | switch (event.getActionMasked()) {
403 | case MotionEvent.ACTION_UP:
404 | if (scrollHappened && status != STATUS_SCROLL_FLING) {
405 | if (null != mOnCursorListener) {
406 | mOnCursorListener.onStopTrackingTouch(mCursorValue);
407 | }
408 | }
409 | scrollHappened = false;
410 | if (status == STATUS_DOWN
411 | || status == STATUS_SCROLL
412 | || status == STATUS_ZOOM) {
413 | status = STATUS_NONE;
414 | }
415 | break;
416 | }
417 | return true;
418 | }
419 |
420 | @Override
421 | public final boolean onScale(ScaleGestureDetector detector) {
422 | float scaleFactor = detector.getScaleFactor();
423 | Log.d(TAG, "onScalescaleFactor: " + scaleFactor);
424 | status = STATUS_ZOOM;
425 | unitPixel *= scaleFactor;
426 | if (unitPixel > maxUnitPixel) {
427 | unitPixel = maxUnitPixel;
428 | scaleFactor = 1.0f;
429 | } else if (unitPixel < minUnitPixel) {
430 | unitPixel = minUnitPixel;
431 | scaleFactor = 1.0f;
432 | }
433 | onScale(mScaleInfo, unitPixel);
434 | mScaleRatio *= scaleFactor;
435 | mTickSpacing = mScaleInfo.unitValue * unitPixel;
436 | Log.d(TAG, mScaleRatio + "onScale:mTickSpacing " + mTickSpacing);
437 | invalidate();
438 | return unitPixel < maxUnitPixel || unitPixel > minUnitPixel;
439 | }
440 |
441 | private float lastScale = 0;
442 |
443 | /*0~1000之间*/
444 | public void setScale(float scale) {
445 | float scaleFactor = 0.9f + scale / 10000;
446 | if (lastScale < scale) {
447 | scaleFactor = 1 + scale / 10000;
448 | }
449 | status = STATUS_ZOOM;
450 | unitPixel *= scaleFactor;
451 | if (unitPixel > maxUnitPixel) {
452 | unitPixel = maxUnitPixel;
453 | scaleFactor = 1.0f;
454 | } else if (unitPixel < minUnitPixel) {
455 | unitPixel = minUnitPixel;
456 | scaleFactor = 1.0f;
457 | }
458 | Log.d(TAG, unitPixel + "onScalescaleFactor: " + scaleFactor);
459 | if (scale == 1000) {
460 | scaleFactor = 1;
461 | unitPixel = (float) (6.0f * Math.pow(10, -4));
462 | }
463 | if (scale == 0) {
464 | scaleFactor = 1;
465 | unitPixel = (float) (1.25f * Math.pow(10, -5));
466 | }
467 | onScale(mScaleInfo, unitPixel);
468 | mScaleRatio *= scaleFactor;
469 | mTickSpacing = mScaleInfo.unitValue * unitPixel;
470 | Log.d(TAG, mScaleRatio + "onScale:mTickSpacing " + mTickSpacing);
471 | invalidate();
472 | lastScale = scale;
473 | status = STATUS_NONE;
474 | }
475 |
476 | protected void onScale(ScaleMode info, float unitPixel) {
477 | }
478 |
479 | @Override
480 | public boolean onScaleBegin(ScaleGestureDetector detector) {
481 | return true;
482 | }
483 |
484 | @Override
485 | public void onScaleEnd(ScaleGestureDetector detector) {
486 | }
487 |
488 | @Override
489 | public final boolean onDown(MotionEvent e) {
490 | if (status == STATUS_SCROLL_FLING) {
491 | getScroller().forceFinished(true);
492 | } else {
493 | scrollHappened = false;
494 | }
495 | status = STATUS_DOWN;
496 | // 返回出 拦截事件
497 | return true;
498 | }
499 |
500 | @Override
501 | public void onShowPress(MotionEvent e) {
502 | // do nothing
503 | }
504 |
505 | @Override
506 | public boolean onSingleTapUp(MotionEvent e) {
507 | // do nothing
508 | return false;
509 | }
510 |
511 | @Override
512 | public final boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
513 | if (e2.getPointerCount() > 1) {
514 | return false;
515 | }
516 | if (getScaleGestureDetect().isInProgress()) {
517 | return false;
518 | }
519 | // TODO: 处理第一次触发滚动产生的距离过大,呈现滚动突兀不友好体验问题 ------ 待优化
520 | if (!scrollHappened) {
521 | scrollHappened = true;
522 | if (null != mOnCursorListener) {
523 | mOnCursorListener.onStartTrackingTouch(mCursorValue);
524 | }
525 | return true;
526 | }
527 | status = STATUS_SCROLL;
528 | // 游标刻度值增量
529 | long courseIncrement = (long) (distanceX / unitPixel);
530 | mCursorValue += courseIncrement;
531 | boolean result = true;
532 | if (mCursorValue < mScaleInfo.startValue) {
533 | mCursorValue = mScaleInfo.startValue;
534 | result = false;
535 | } else if (mCursorValue > mScaleInfo.endValue) {
536 | mCursorValue = mScaleInfo.endValue;
537 | result = false;
538 | }
539 | if (null != mOnCursorListener) {
540 | mOnCursorListener.onProgressChanged(mCursorValue, true);
541 | }
542 | invalidate();
543 | return result;
544 | }
545 |
546 | @Override
547 | public void onLongPress(MotionEvent e) {
548 | // do nothing
549 | }
550 |
551 | @Override
552 | public void computeScroll() {
553 | if (getScroller().computeScrollOffset()) {
554 | int currX = getScroller().getCurrX();
555 | mCursorValue = mScaleInfo.startValue + (long) (currX / unitPixel);
556 | if (mCursorValue < mScaleInfo.startValue) {
557 | mCursorValue = mScaleInfo.startValue;
558 | } else if (mCursorValue > mScaleInfo.endValue) {
559 | mCursorValue = mScaleInfo.endValue;
560 | }
561 | invalidate();
562 | } else {
563 | if (status == STATUS_SCROLL_FLING) {
564 | status = STATUS_NONE;
565 | if (null != mOnCursorListener) {
566 | mOnCursorListener.onStopTrackingTouch(mCursorValue);
567 | }
568 | }
569 | }
570 | }
571 |
572 | @Override
573 | public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
574 | status = STATUS_SCROLL_FLING;
575 | int startX = (int) ((mCursorValue - mScaleInfo.startValue) * unitPixel);
576 | int maX = (int) ((mScaleInfo.endValue - mScaleInfo.startValue) * unitPixel);
577 | getScroller().fling(startX,
578 | 0,
579 | -(int) velocityX,
580 | 0,
581 | 0,
582 | maX,
583 | 0,
584 | 0);
585 | invalidate();
586 | return true;
587 | }
588 |
589 | public void setTickDirection(boolean isUP) {
590 | tickDirectionUp = isUP;
591 | invalidate();
592 | }
593 |
594 | protected class ScaleMode {
595 | public long unitValue;
596 | public long startValue;
597 | public long endValue;
598 | public long keyScaleRange;
599 | }
600 |
601 | private OnCursorListener mOnCursorListener;
602 |
603 | public void setOnCursorListener(OnCursorListener l) {
604 | this.mOnCursorListener = l;
605 | }
606 |
607 | public interface OnCursorListener {
608 | void onStartTrackingTouch(long cursorValue);
609 |
610 | void onProgressChanged(long cursorValue, boolean isFromUser);
611 |
612 | void onStopTrackingTouch(long cursorValue);
613 | }
614 |
615 | private TickMarkStrategy mTickMarkStrategy;
616 |
617 | public void setTickMarkStrategy(TickMarkStrategy tickMarkStrategy) {
618 | this.mTickMarkStrategy = tickMarkStrategy;
619 | }
620 |
621 | public interface TickMarkStrategy {
622 | /**
623 | * 是否显示刻度值
624 | *
625 | * @param scaleValue 刻度值
626 | * @param keyScale 是否是关键刻度
627 | * @return
628 | */
629 | boolean disPlay(long scaleValue, boolean keyScale);
630 |
631 | /**
632 | * 获取显示的刻度值
633 | *
634 | * @param scaleValue
635 | * @param keyScale
636 | * @return
637 | */
638 | @NonNull
639 | String getScaleValue(long scaleValue, boolean keyScale);
640 |
641 | /**
642 | * 获取当前刻度值显示颜色
643 | *
644 | * @param scaleValue
645 | * @param keyScale
646 | * @return
647 | */
648 | @ColorInt
649 | int getColor(long scaleValue, boolean keyScale);
650 |
651 | /**
652 | * 获取当前刻度值显示大小
653 | *
654 | * @param scaleValue
655 | * @param keyScale
656 | * @param maxScaleValueSize
657 | * @return
658 | */
659 | @Dimension
660 | float getSize(long scaleValue, boolean keyScale, float maxScaleValueSize);
661 | }
662 |
663 | public static int dip2px(float dipValue) {
664 | final float scale = Resources.getSystem().getDisplayMetrics().density;
665 | return (int) (dipValue * scale + 0.5f);
666 | }
667 | }
668 |
--------------------------------------------------------------------------------
/TimeRuler/src/main/java/com/kubi/timeruler/BaseScaleBarVertical.java:
--------------------------------------------------------------------------------
1 | package com.kubi.timeruler;
2 |
3 | import android.content.Context;
4 | import android.content.res.Resources;
5 | import android.content.res.TypedArray;
6 | import android.graphics.Canvas;
7 | import android.graphics.Color;
8 | import android.graphics.Paint;
9 |
10 | import android.util.AttributeSet;
11 | import android.util.Log;
12 | import android.view.GestureDetector;
13 | import android.view.MotionEvent;
14 | import android.view.ScaleGestureDetector;
15 | import android.view.View;
16 | import android.widget.Scroller;
17 |
18 |
19 | import androidx.annotation.ColorInt;
20 | import androidx.annotation.Dimension;
21 | import androidx.annotation.FloatRange;
22 | import androidx.annotation.NonNull;
23 | import androidx.annotation.Nullable;
24 | import androidx.core.view.GestureDetectorCompat;
25 |
26 | import com.kubi.timeruler.utils.SizeUtils;
27 |
28 | import java.util.Calendar;
29 |
30 | public class BaseScaleBarVertical extends View implements ScaleGestureDetector.OnScaleGestureListener,
31 | GestureDetector.OnGestureListener {
32 | private static final String TAG = "BaseScaleBarVertical";
33 | /* 缩放比例*/
34 | private float mScaleRatio = 1.0f;
35 | /* 关键刻度宽度*/
36 | private float mKeyTickWidth;
37 | /* 刻度宽度*/
38 | private float mTickWidth;
39 | /* 普通刻度线与关键刻度线的比*/
40 | private float mNormalTickAndKeyTickRatio;
41 | /* 刻度间距*/
42 | private float mTickSpacing;
43 | private ScaleGestureDetector mScaleGestureDetector;
44 | private Paint mScalePaint;
45 | private ScaleMode mScaleInfo;
46 | private float unitPixel;
47 | //
48 | private long minScreenSpanValue;
49 | private long maxScreenSpanValue;
50 | private long mCursorValue;
51 | private GestureDetectorCompat mGestureDetectorCompat;
52 | private boolean scrollHappened;
53 | private float mCursorPosition;
54 | private float mCursorPositionProportion = 0.5f;
55 | // 刻度尺纵线位置
56 | private float mBaselinePosition;
57 | private float mBaselinePositionProportion = 0.67f;
58 | private float maxUnitPixel;
59 | private float minUnitPixel;
60 | private int tickColor;
61 | private boolean tickDirectionLeft;//刻度线方向
62 | private final int cursorLineColor;
63 | private boolean showCursorLine;
64 | private boolean showTickValue;
65 | private boolean showTickLine;
66 | private Scroller mScroller;
67 | /**
68 | * 状态
69 | */
70 | private int status = STATUS_NONE;
71 | /**
72 | * 默认状态
73 | */
74 | public static final int STATUS_NONE = 0;
75 |
76 | /**
77 | * 按下
78 | */
79 | public static final int STATUS_DOWN = 1;
80 | /**
81 | * 拖拽滚动
82 | */
83 | public static final int STATUS_SCROLL = STATUS_DOWN + 1;
84 | /**
85 | * 甩动滚动(惯性)
86 | */
87 | public static final int STATUS_SCROLL_FLING = STATUS_SCROLL + 1;
88 | /**
89 | * 缩放
90 | */
91 | public static final int STATUS_ZOOM = STATUS_SCROLL_FLING + 1;
92 | /* 刻度值 最大规格*/
93 | private float maxScaleValueSize;
94 | /*文字偏移量*/
95 | private float tickValueOffset;
96 |
97 | public BaseScaleBarVertical(Context context) {
98 | this(context, null);
99 | }
100 |
101 | public BaseScaleBarVertical(Context context, @Nullable AttributeSet attrs) {
102 | super(context, attrs);
103 | TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.BaseScaleBarVertical);
104 | mKeyTickWidth = typedArray.getDimension(R.styleable.BaseScaleBarVertical_keyTickWidthVertical, SizeUtils.dp2px(getContext(), 10));
105 | tickValueOffset = typedArray.getDimension(R.styleable.BaseScaleBarVertical_tickValueOffsetVertical, SizeUtils.dp2px(getContext(), -30));
106 | tickDirectionLeft = typedArray.getBoolean(R.styleable.BaseScaleBarVertical_tickDirectionLeftVertical, true);
107 | mNormalTickAndKeyTickRatio = typedArray.getFloat(R.styleable.BaseScaleBarVertical_normalTickRatioVertical, 0.67f);
108 |
109 | tickColor = typedArray.getColor(R.styleable.BaseScaleBarVertical_tickColorVertical, Color.BLACK);
110 | cursorLineColor = typedArray.getColor(R.styleable.BaseScaleBarVertical_cursorLineColorVertical, Color.YELLOW);
111 | showCursorLine = typedArray.getBoolean(R.styleable.BaseScaleBarVertical_showCursorLineVertical, true);
112 | showTickValue = typedArray.getBoolean(R.styleable.BaseScaleBarVertical_showTickValueVertical, true);
113 | showTickLine = typedArray.getBoolean(R.styleable.BaseScaleBarVertical_showTickLineVertical, true);
114 | maxScaleValueSize = typedArray.getDimension(R.styleable.BaseScaleBarVertical_maxScaleValueSizeVertical, SizeUtils.sp2px(getContext(), 15));
115 |
116 | float position = typedArray.getFloat(R.styleable.BaseScaleBarVertical_cursorPositionVertical, 0.5f);
117 | if (position != 0) {
118 | mCursorPositionProportion = position;
119 | }
120 | position = typedArray.getFloat(R.styleable.BaseScaleBarVertical_baselinePositionVertical, 0.67f);
121 | if (position != 0) {
122 | mBaselinePositionProportion = position;
123 | }
124 | // 释放
125 | typedArray.recycle();
126 |
127 | init();
128 | }
129 |
130 | private void init() {
131 | mScalePaint = new Paint();
132 | mScalePaint.setAntiAlias(true);
133 | mScalePaint.setStrokeWidth(1.0f);
134 | mScalePaint.setDither(true);
135 | mScalePaint.setStyle(Paint.Style.FILL_AND_STROKE);
136 |
137 | minScreenSpanValue = 30 * 60 * 1000;
138 | maxScreenSpanValue = 24 * 60 * 60 * 1000;
139 | mScaleInfo = new ScaleMode();
140 | mScaleInfo.unitValue = 60000;
141 | mScaleInfo.keyScaleRange = 5 * 60 * 1000;
142 | Calendar calendar = Calendar.getInstance();
143 | calendar.set(Calendar.HOUR_OF_DAY, 0);
144 | calendar.set(Calendar.MINUTE, 0);
145 | calendar.set(Calendar.SECOND, 0);
146 | mScaleInfo.startValue = calendar.getTimeInMillis();
147 | calendar.set(Calendar.HOUR_OF_DAY, 23);
148 | calendar.set(Calendar.MINUTE, 59);
149 | calendar.set(Calendar.SECOND, 59);
150 | mScaleInfo.endValue = calendar.getTimeInMillis();
151 | mCursorValue = mScaleInfo.startValue;
152 | }
153 |
154 | protected void setScaleRatio(@FloatRange(from = 0.0f, to = 1.0f, fromInclusive = false) float scaleRatio) {
155 | this.mScaleRatio = scaleRatio;
156 | }
157 |
158 | protected long getMaxScreenSpanValue() {
159 | return maxScreenSpanValue;
160 | }
161 |
162 | protected long getMinScreenSpanValue() {
163 | return minScreenSpanValue;
164 | }
165 |
166 | protected float getKeyTickWidth() {
167 | return mKeyTickWidth;
168 | }
169 |
170 | protected float getCursorPosition() {
171 | return mCursorPosition;
172 | }
173 |
174 | /**
175 | * 获取当前游标值
176 | * @return 游标时间值(毫秒时间戳)
177 | */
178 | public long getCursorValue() {
179 | return mCursorValue;
180 | }
181 |
182 | protected float getUnitPixel() {
183 | return unitPixel;
184 | }
185 |
186 | @Override
187 | protected void onSizeChanged(int w, int h, int oldw, int oldh) {
188 | mTickWidth = mKeyTickWidth * mNormalTickAndKeyTickRatio;
189 | mCursorPosition = h * mCursorPositionProportion;
190 | mBaselinePosition = w * mBaselinePositionProportion;
191 |
192 | maxUnitPixel = h * 1.0f / minScreenSpanValue;
193 | minUnitPixel = h * 1.0f / maxScreenSpanValue;
194 |
195 | unitPixel = maxUnitPixel * mScaleRatio;
196 | mTickSpacing = mScaleInfo.unitValue * unitPixel;
197 | }
198 |
199 | /**
200 | * 设置时间范围
201 | * @param start 开始时间(毫秒时间戳)
202 | * @param end 结束时间(毫秒时间戳)
203 | */
204 | public void setRange(long start, long end) {
205 | if (start >= end) {
206 | return;
207 | }
208 | mScaleInfo.startValue = start;
209 | mScaleInfo.endValue = end;
210 | mCursorValue = start;
211 | invalidate();
212 | }
213 |
214 | /**
215 | * 设置游标值
216 | * @param cursorValue 游标时间值(毫秒时间戳)
217 | */
218 | public void setCursorValue(long cursorValue) {
219 | if (status != STATUS_NONE) {
220 | return;
221 | }
222 | if (cursorValue < mScaleInfo.startValue || cursorValue > mScaleInfo.endValue) {
223 | return;
224 | }
225 | this.mCursorValue = cursorValue;
226 | if (mOnCursorListener != null) mOnCursorListener.onProgressChanged(mCursorValue, false);
227 | invalidate();
228 | }
229 |
230 | protected void updateScaleInfo(long keyScaleRange, long unitValue) {
231 | mScaleInfo.keyScaleRange = keyScaleRange;
232 | mScaleInfo.unitValue = unitValue;
233 | }
234 |
235 | protected float getBaselinePosition() {
236 | return mBaselinePosition;
237 | }
238 |
239 | @Override
240 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
241 | setMeasuredDimension(getWidthSize(getSuggestedMinimumWidth(), widthMeasureSpec), getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
242 | }
243 |
244 | private int getWidthSize(int size, int widthMeasureSpec) {
245 | int result = size;
246 | int contentWidth = calcContentWidth(mBaselinePositionProportion);
247 | int specMode = MeasureSpec.getMode(widthMeasureSpec);
248 | int specSize = MeasureSpec.getSize(widthMeasureSpec);
249 | switch (specMode) {
250 | case MeasureSpec.UNSPECIFIED:
251 | result = size > contentWidth ? size : contentWidth;
252 | break;
253 | case MeasureSpec.AT_MOST:
254 | result = contentWidth;
255 | break;
256 | case MeasureSpec.EXACTLY:
257 | result = specSize > contentWidth ? specSize : contentWidth;
258 | break;
259 | }
260 | return result;
261 | }
262 |
263 | protected int calcContentWidth(float baselinePositionProportion) {
264 | int tickValueWidth = 0;
265 | if (showTickValue && null != mTickMarkStrategy) {
266 | mScalePaint.setTextSize(maxScaleValueSize);
267 | Paint.FontMetrics fontMetrics = mScalePaint.getFontMetrics();
268 | double ceil = Math.ceil(fontMetrics.bottom - fontMetrics.top);
269 | tickValueWidth = (int) (ceil + tickValueOffset);
270 | }
271 | return (int) ((mKeyTickWidth + tickValueWidth) / baselinePositionProportion + 0.5f);
272 | }
273 |
274 | @Override
275 | protected void onDraw(Canvas canvas) {
276 | // 保存画布状态
277 | canvas.save();
278 |
279 | // 旋转画布90度,使纵向滚动变为横向绘制
280 | canvas.rotate(90);
281 | canvas.translate(0, -getWidth());
282 |
283 | float baselinePosition = getBaselinePosition();
284 | mScalePaint.setColor(tickColor);
285 | if (showTickLine) {
286 | canvas.drawLine(getScrollY(), baselinePosition, getScrollY() + getHeight(), baselinePosition, mScalePaint);
287 | }
288 | long leftRange = mCursorValue - mScaleInfo.startValue;
289 | long leftNeighborOffest = leftRange % mScaleInfo.unitValue;
290 | long leftNeighborTickValue = mCursorValue - leftNeighborOffest;
291 | float leftNeighborPosition = mCursorPosition - leftNeighborOffest * unitPixel;
292 | int leftCount = (int) (mCursorPosition / mTickSpacing + 0.5f);
293 | float onDrawTickPosition;
294 | long onDrawTickValue;
295 | for (int i = 0; i < leftCount; i++) {
296 | onDrawTickValue = leftNeighborTickValue - mScaleInfo.unitValue * i;
297 | if (onDrawTickValue < mScaleInfo.startValue) {
298 | break;
299 | }
300 | onDrawTickPosition = leftNeighborPosition - mTickSpacing * i;
301 | if (tickDirectionLeft) {
302 | if ((onDrawTickValue - mScaleInfo.startValue) % mScaleInfo.keyScaleRange == 0) {
303 | canvas.drawLine(onDrawTickPosition, baselinePosition, onDrawTickPosition, baselinePosition - mKeyTickWidth, mScalePaint);
304 | drawTickValue(canvas, onDrawTickPosition, baselinePosition - mKeyTickWidth, onDrawTickValue, true);
305 | } else {
306 | canvas.drawLine(onDrawTickPosition, baselinePosition - mTickWidth, onDrawTickPosition, baselinePosition, mScalePaint);
307 | drawTickValue(canvas, onDrawTickPosition, baselinePosition - mTickWidth, onDrawTickValue, false);
308 | }
309 | } else {
310 | if ((onDrawTickValue - mScaleInfo.startValue) % mScaleInfo.keyScaleRange == 0) {
311 | canvas.drawLine(onDrawTickPosition, baselinePosition, onDrawTickPosition, baselinePosition + mKeyTickWidth, mScalePaint);
312 | drawTickValue(canvas, onDrawTickPosition, baselinePosition - mKeyTickWidth, onDrawTickValue, true);
313 | } else {
314 | canvas.drawLine(onDrawTickPosition, baselinePosition, onDrawTickPosition, baselinePosition + mTickWidth, mScalePaint);
315 | drawTickValue(canvas, onDrawTickPosition, baselinePosition - mTickWidth, onDrawTickValue, false);
316 | }
317 | }
318 |
319 | }
320 |
321 | long rightNeighborTickValue = leftNeighborTickValue + mScaleInfo.unitValue;
322 | float rightNeighborPosition = leftNeighborPosition + mTickSpacing;
323 | int rightCount = (int) ((getHeight() - mCursorPosition) / mTickSpacing + 0.5f);
324 | for (int i = 0; i < rightCount; i++) {
325 | onDrawTickValue = rightNeighborTickValue + mScaleInfo.unitValue * i;
326 | if (onDrawTickValue > mScaleInfo.endValue) {
327 | break;
328 | }
329 | onDrawTickPosition = rightNeighborPosition + mTickSpacing * i;
330 | if (tickDirectionLeft) {
331 | if ((onDrawTickValue - mScaleInfo.startValue) % mScaleInfo.keyScaleRange == 0) {
332 | canvas.drawLine(onDrawTickPosition, baselinePosition, onDrawTickPosition, baselinePosition - mKeyTickWidth, mScalePaint);
333 | drawTickValue(canvas, onDrawTickPosition, baselinePosition - mKeyTickWidth, onDrawTickValue, true);
334 | } else {
335 | canvas.drawLine(onDrawTickPosition, baselinePosition, onDrawTickPosition, baselinePosition - mTickWidth, mScalePaint);
336 | drawTickValue(canvas, onDrawTickPosition, baselinePosition - mTickWidth, onDrawTickValue, false);
337 | }
338 | } else {
339 | if ((onDrawTickValue - mScaleInfo.startValue) % mScaleInfo.keyScaleRange == 0) {
340 | canvas.drawLine(onDrawTickPosition, baselinePosition, onDrawTickPosition, baselinePosition + mKeyTickWidth, mScalePaint);
341 | drawTickValue(canvas, onDrawTickPosition, baselinePosition - mKeyTickWidth, onDrawTickValue, true);
342 | } else {
343 | canvas.drawLine(onDrawTickPosition, baselinePosition, onDrawTickPosition, baselinePosition + mTickWidth, mScalePaint);
344 | drawTickValue(canvas, onDrawTickPosition, baselinePosition - mTickWidth, onDrawTickValue, false);
345 | }
346 | }
347 |
348 | }
349 |
350 | onEndTickDraw(canvas);
351 |
352 | drawCursor(canvas, mCursorPosition, mCursorValue);
353 |
354 | // 恢复画布状态
355 | canvas.restore();
356 | }
357 |
358 | protected void onEndTickDraw(Canvas canvas) {
359 | }
360 |
361 | /**
362 | * 绘制游标
363 | *
364 | * @param canvas
365 | * @param cursorPosition
366 | */
367 | protected void drawCursor(Canvas canvas, float cursorPosition, long cursorValue) {
368 | if (showCursorLine) {
369 | mScalePaint.setColor(cursorLineColor);
370 | canvas.drawLine(0, cursorPosition, getWidth(), cursorPosition, mScalePaint);
371 | }
372 | }
373 |
374 | /**
375 | * 绘制刻度线描述
376 | *
377 | * @param canvas
378 | * @param x
379 | * @param y
380 | * @param scaleValue
381 | * @param keyScale
382 | */
383 | private void drawTickValue(Canvas canvas, float x, float y, long scaleValue, boolean keyScale) {
384 | if (showTickValue) {
385 | if (null != mTickMarkStrategy) {
386 | if (mTickMarkStrategy.disPlay(scaleValue, keyScale)) {
387 | mScalePaint.setColor(mTickMarkStrategy.getColor(scaleValue, keyScale));
388 | mScalePaint.setTextAlign(Paint.Align.CENTER);
389 | float size = mTickMarkStrategy.getSize(scaleValue, keyScale, maxScaleValueSize);
390 | size = Math.min(maxScaleValueSize, size);
391 | mScalePaint.setTextSize(size);
392 | canvas.drawText(mTickMarkStrategy.getScaleValue(scaleValue, keyScale), x, y - tickValueOffset, mScalePaint);
393 | }
394 | }
395 | }
396 | }
397 |
398 | ScaleGestureDetector getScaleGestureDetect() {
399 | if (null == mScaleGestureDetector) {
400 | mScaleGestureDetector = new ScaleGestureDetector(getContext(), this);
401 | }
402 | return mScaleGestureDetector;
403 | }
404 |
405 | GestureDetectorCompat getGestureDetectorCompat() {
406 | if (null == mGestureDetectorCompat) {
407 | mGestureDetectorCompat = new GestureDetectorCompat(getContext(), this);
408 | }
409 | return mGestureDetectorCompat;
410 | }
411 |
412 | Scroller getScroller() {
413 | if (null == mScroller) {
414 | mScroller = new Scroller(getContext());
415 | }
416 | return mScroller;
417 | }
418 |
419 | @Override
420 | public boolean onTouchEvent(MotionEvent event) {
421 | getScaleGestureDetect().onTouchEvent(event);
422 | getGestureDetectorCompat().onTouchEvent(event);
423 | switch (event.getActionMasked()) {
424 | case MotionEvent.ACTION_UP:
425 | if (scrollHappened && status != STATUS_SCROLL_FLING) {
426 | if (null != mOnCursorListener) {
427 | mOnCursorListener.onStopTrackingTouch(mCursorValue);
428 | }
429 | }
430 | scrollHappened = false;
431 | if (status == STATUS_DOWN
432 | || status == STATUS_SCROLL
433 | || status == STATUS_ZOOM) {
434 | status = STATUS_NONE;
435 | }
436 | break;
437 | }
438 | return true;
439 | }
440 |
441 | @Override
442 | public final boolean onScale(ScaleGestureDetector detector) {
443 | float scaleFactor = detector.getScaleFactor();
444 | Log.d(TAG, "onScalescaleFactor: " + scaleFactor);
445 | status = STATUS_ZOOM;
446 | unitPixel *= scaleFactor;
447 | if (unitPixel > maxUnitPixel) {
448 | unitPixel = maxUnitPixel;
449 | scaleFactor = 1.0f;
450 | } else if (unitPixel < minUnitPixel) {
451 | unitPixel = minUnitPixel;
452 | scaleFactor = 1.0f;
453 | }
454 | onScale(mScaleInfo, unitPixel);
455 | mScaleRatio *= scaleFactor;
456 | mTickSpacing = mScaleInfo.unitValue * unitPixel;
457 | Log.d(TAG, mScaleRatio + "onScale:mTickSpacing " + mTickSpacing);
458 | invalidate();
459 | return unitPixel < maxUnitPixel || unitPixel > minUnitPixel;
460 | }
461 |
462 | private float lastScale = 0;
463 |
464 | /*0~1000之间*/
465 | /**
466 | * 设置缩放比例
467 | * @param scale 缩放值,范围0-1000
468 | */
469 | public void setScale(float scale) {
470 | float scaleFactor = 0.9f + scale / 10000;
471 | if (lastScale < scale) {
472 | scaleFactor = 1 + scale / 10000;
473 | }
474 | status = STATUS_ZOOM;
475 | unitPixel *= scaleFactor;
476 | if (unitPixel > maxUnitPixel) {
477 | unitPixel = maxUnitPixel;
478 | scaleFactor = 1.0f;
479 | } else if (unitPixel < minUnitPixel) {
480 | unitPixel = minUnitPixel;
481 | scaleFactor = 1.0f;
482 | }
483 | Log.d(TAG, unitPixel + "onScalescaleFactor: " + scaleFactor);
484 | if (scale == 1000) {
485 | scaleFactor = 1;
486 | unitPixel = (float) (6.0f * Math.pow(10, -4));
487 | }
488 | if (scale == 0) {
489 | scaleFactor = 1;
490 | unitPixel = (float) (1.25f * Math.pow(10, -5));
491 | }
492 | onScale(mScaleInfo, unitPixel);
493 | mScaleRatio *= scaleFactor;
494 | mTickSpacing = mScaleInfo.unitValue * unitPixel;
495 | Log.d(TAG, mScaleRatio + "onScale:mTickSpacing " + mTickSpacing);
496 | invalidate();
497 | lastScale = scale;
498 | status = STATUS_NONE;
499 | }
500 |
501 | protected void onScale(ScaleMode info, float unitPixel) {
502 | }
503 |
504 | @Override
505 | public boolean onScaleBegin(ScaleGestureDetector detector) {
506 | return true;
507 | }
508 |
509 | @Override
510 | public void onScaleEnd(ScaleGestureDetector detector) {
511 | }
512 |
513 | @Override
514 | public final boolean onDown(MotionEvent e) {
515 | if (status == STATUS_SCROLL_FLING) {
516 | getScroller().forceFinished(true);
517 | } else {
518 | scrollHappened = false;
519 | }
520 | status = STATUS_DOWN;
521 | // 返回出 拦截事件
522 | return true;
523 | }
524 |
525 | @Override
526 | public void onShowPress(MotionEvent e) {
527 | // do nothing
528 | }
529 |
530 | @Override
531 | public boolean onSingleTapUp(MotionEvent e) {
532 | // do nothing
533 | return false;
534 | }
535 |
536 | @Override
537 | public final boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
538 | if (e2.getPointerCount() > 1) {
539 | return false;
540 | }
541 | if (getScaleGestureDetect().isInProgress()) {
542 | return false;
543 | }
544 | // TODO: 处理第一次触发滚动产生的距离过大,呈现滚动突兀不友好体验问题 ------ 待优化
545 | if (!scrollHappened) {
546 | scrollHappened = true;
547 | if (null != mOnCursorListener) {
548 | mOnCursorListener.onStartTrackingTouch(mCursorValue);
549 | }
550 | return true;
551 | }
552 | status = STATUS_SCROLL;
553 | // 游标刻度值增量 - 纵向滚动使用distanceY
554 | long courseIncrement = (long) (distanceY / unitPixel);
555 | mCursorValue += courseIncrement;
556 | boolean result = true;
557 | if (mCursorValue < mScaleInfo.startValue) {
558 | mCursorValue = mScaleInfo.startValue;
559 | result = false;
560 | } else if (mCursorValue > mScaleInfo.endValue) {
561 | mCursorValue = mScaleInfo.endValue;
562 | result = false;
563 | }
564 | if (null != mOnCursorListener) {
565 | mOnCursorListener.onProgressChanged(mCursorValue, true);
566 | }
567 | invalidate();
568 | return result;
569 | }
570 |
571 | @Override
572 | public void onLongPress(MotionEvent e) {
573 | // do nothing
574 | }
575 |
576 | @Override
577 | public void computeScroll() {
578 | if (getScroller().computeScrollOffset()) {
579 | int currY = getScroller().getCurrY();
580 | mCursorValue = mScaleInfo.startValue + (long) (currY / unitPixel);
581 | if (mCursorValue < mScaleInfo.startValue) {
582 | mCursorValue = mScaleInfo.startValue;
583 | } else if (mCursorValue > mScaleInfo.endValue) {
584 | mCursorValue = mScaleInfo.endValue;
585 | }
586 | invalidate();
587 | } else {
588 | if (status == STATUS_SCROLL_FLING) {
589 | status = STATUS_NONE;
590 | if (null != mOnCursorListener) {
591 | mOnCursorListener.onStopTrackingTouch(mCursorValue);
592 | }
593 | }
594 | }
595 | }
596 |
597 | @Override
598 | public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
599 | status = STATUS_SCROLL_FLING;
600 | int startY = (int) ((mCursorValue - mScaleInfo.startValue) * unitPixel);
601 | int maY = (int) ((mScaleInfo.endValue - mScaleInfo.startValue) * unitPixel);
602 | getScroller().fling(0,
603 | startY,
604 | 0,
605 | -(int) velocityY,
606 | 0,
607 | 0,
608 | 0,
609 | maY);
610 | invalidate();
611 | return true;
612 | }
613 |
614 | /**
615 | * 设置刻度线方向
616 | * @param isLeft true向左延伸,false向右延伸
617 | */
618 | public void setTickDirection(boolean isLeft) {
619 | tickDirectionLeft = isLeft;
620 | invalidate();
621 | }
622 |
623 | protected class ScaleMode {
624 | public long unitValue;
625 | public long startValue;
626 | public long endValue;
627 | public long keyScaleRange;
628 | }
629 |
630 | private OnCursorListener mOnCursorListener;
631 |
632 | /**
633 | * 设置游标监听器
634 | * @param l 游标监听器
635 | */
636 | public void setOnCursorListener(OnCursorListener l) {
637 | this.mOnCursorListener = l;
638 | }
639 |
640 | public interface OnCursorListener {
641 | void onStartTrackingTouch(long cursorValue);
642 |
643 | void onProgressChanged(long cursorValue, boolean isFromUser);
644 |
645 | void onStopTrackingTouch(long cursorValue);
646 | }
647 |
648 | private TickMarkStrategy mTickMarkStrategy;
649 |
650 | /**
651 | * 设置刻度标记策略
652 | * @param tickMarkStrategy 刻度标记策略
653 | */
654 | public void setTickMarkStrategy(TickMarkStrategy tickMarkStrategy) {
655 | this.mTickMarkStrategy = tickMarkStrategy;
656 | }
657 |
658 | public interface TickMarkStrategy {
659 | /**
660 | * 是否显示刻度值
661 | *
662 | * @param scaleValue 刻度值
663 | * @param keyScale 是否是关键刻度
664 | * @return
665 | */
666 | boolean disPlay(long scaleValue, boolean keyScale);
667 |
668 | /**
669 | * 获取显示的刻度值
670 | *
671 | * @param scaleValue
672 | * @param keyScale
673 | * @return
674 | */
675 | @NonNull
676 | String getScaleValue(long scaleValue, boolean keyScale);
677 |
678 | /**
679 | * 获取当前刻度值显示颜色
680 | *
681 | * @param scaleValue
682 | * @param keyScale
683 | * @return
684 | */
685 | @ColorInt
686 | int getColor(long scaleValue, boolean keyScale);
687 |
688 | /**
689 | * 获取当前刻度值显示大小
690 | *
691 | * @param scaleValue
692 | * @param keyScale
693 | * @param maxScaleValueSize
694 | * @return
695 | */
696 | @Dimension
697 | float getSize(long scaleValue, boolean keyScale, float maxScaleValueSize);
698 | }
699 |
700 | public static int dip2px(float dipValue) {
701 | final float scale = Resources.getSystem().getDisplayMetrics().density;
702 | return (int) (dipValue * scale + 0.5f);
703 | }
704 | }
705 |
--------------------------------------------------------------------------------