diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..aa724b7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +*.iml +.gradle +/local.properties +/.idea/caches +/.idea/libraries +/.idea/modules.xml +/.idea/workspace.xml +/.idea/navEditor.xml +/.idea/assetWizardSettings.xml +.DS_Store +/build +/captures +.externalNativeBuild +.cxx +local.properties diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..fb7f4a8 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..d372f73 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,21 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..5015e8c --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..42afabf --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle new file mode 100644 index 0000000..fcc756d --- /dev/null +++ b/app/build.gradle @@ -0,0 +1,44 @@ +plugins { + id 'com.android.application' + id 'org.jetbrains.kotlin.android' + id 'kotlin-android-extensions' +} + +android { + compileSdk 32 + + defaultConfig { + applicationId "com.github.smaxlyb.stepviewdemo" + minSdk 23 + targetSdk 32 + versionCode 1 + versionName "1.0" + + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + } + + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } + kotlinOptions { + jvmTarget = '1.8' + } +} + +dependencies { + implementation project(path: ':stepView') + implementation 'androidx.core:core-ktx:1.7.0' + implementation 'androidx.appcompat:appcompat:1.3.0' + implementation 'com.google.android.material:material:1.4.0' + implementation 'androidx.constraintlayout:constraintlayout:2.0.4' + testImplementation 'junit:junit:4.13.2' + androidTestImplementation 'androidx.test.ext:junit:1.1.3' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' +} \ No newline at end of file diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..481bb43 --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile \ No newline at end of file diff --git a/app/src/androidTest/java/com/github/smaxlyb/stepviewdemo/ExampleInstrumentedTest.kt b/app/src/androidTest/java/com/github/smaxlyb/stepviewdemo/ExampleInstrumentedTest.kt new file mode 100644 index 0000000..36e9bf5 --- /dev/null +++ b/app/src/androidTest/java/com/github/smaxlyb/stepviewdemo/ExampleInstrumentedTest.kt @@ -0,0 +1,24 @@ +package com.github.smaxlyb.stepviewdemo + +import androidx.test.platform.app.InstrumentationRegistry +import androidx.test.ext.junit.runners.AndroidJUnit4 + +import org.junit.Test +import org.junit.runner.RunWith + +import org.junit.Assert.* + +/** + * Instrumented test, which will execute on an Android device. + * + * See [testing documentation](http://d.android.com/tools/testing). + */ +@RunWith(AndroidJUnit4::class) +class ExampleInstrumentedTest { + @Test + fun useAppContext() { + // Context of the app under test. + val appContext = InstrumentationRegistry.getInstrumentation().targetContext + assertEquals("com.github.smaxlyb.stepviewdemo", appContext.packageName) + } +} \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..cfb2434 --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/github/smaxlyb/stepviewdemo/MainActivity.kt b/app/src/main/java/com/github/smaxlyb/stepviewdemo/MainActivity.kt new file mode 100644 index 0000000..3abbba0 --- /dev/null +++ b/app/src/main/java/com/github/smaxlyb/stepviewdemo/MainActivity.kt @@ -0,0 +1,97 @@ +package com.github.smaxlyb.stepviewdemo + +import android.os.Build +import androidx.appcompat.app.AppCompatActivity +import android.os.Bundle +import android.widget.RadioButton +import android.widget.Toast +import androidx.annotation.RequiresApi +import com.github.smaxlyb.stepview.StepModel +import com.github.smaxlyb.stepview.StepView +import kotlinx.android.synthetic.main.activity_main.* + +class MainActivity : AppCompatActivity() { + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_main) + + step.setNodes( + mutableListOf( + StepModel("步骤一", "这是步骤一这是步骤一这是步骤一这是步骤一这是步骤一这是步骤一这是步骤一"), + StepModel("步骤二"), + StepModel("步骤三步骤三步骤三步骤三步骤三步骤三步骤三步骤三步骤三步骤三步骤三", "这是步骤三这是步骤三这是步骤三这是步骤三这是步骤三这是步骤三这是步骤三"), + StepModel("步骤四") + ) + ) + .setOnNodeClickListener { i, ndStepModel -> + Toast.makeText(this, ndStepModel.title, Toast.LENGTH_SHORT).show() + } + initListener() + } + + private fun initListener() { + + rp4_rd6.setOnClickListener { + rp4_rd2.isEnabled = false + rp4_rd3.isEnabled = false + rp4_rd4.isEnabled = false + rp4_rd5.isEnabled = false + rp4_rd6.isEnabled = false + step.removeNodeAt(3) + } + + rp5_rd1.setOnClickListener { + rp6_rd1.isEnabled = true + rp6_rd2.isEnabled = true + rp6_rd3.isEnabled = false + rp6_rd4.isEnabled = false + + step.setOrientation(StepView.Orientation.Horizontal) + } + rp5_rd2.setOnClickListener { + rp6_rd1.isEnabled = false + rp6_rd2.isEnabled = false + rp6_rd3.isEnabled = true + rp6_rd4.isEnabled = true + + step.setOrientation(StepView.Orientation.Vertical) + } + + listOf(rp1_rd2, rp2_rd2, rp3_rd2, rp4_rd2).forEachIndexed { index, radioButton -> + radioButton.setOnClickListener { + step.setUndoAt(index) + } + } + + listOf(rp1_rd3, rp2_rd3, rp3_rd3, rp4_rd3).forEachIndexed { index, radioButton -> + radioButton.setOnClickListener { + step.setDoingAt(index) + } + } + + listOf(rp1_rd4, rp2_rd4, rp3_rd4, rp4_rd4).forEachIndexed { index, radioButton -> + radioButton.setOnClickListener { + step.setErrorAt(index) + } + } + + listOf(rp1_rd5, rp2_rd5, rp3_rd5, rp4_rd5).forEachIndexed { index, radioButton -> + radioButton.setOnClickListener { + step.setDoneAt(index) + } + } + + rp6_rd1.setOnClickListener { + step.setDirection(StepView.Direction.FromLeft) + } + rp6_rd2.setOnClickListener { + step.setDirection(StepView.Direction.FromRight) + } + rp6_rd3.setOnClickListener { + step.setDirection(StepView.Direction.FromTop) + } + rp6_rd4.setOnClickListener { + step.setDirection(StepView.Direction.FromBottom) + } + } +} \ No newline at end of file diff --git a/app/src/main/res/drawable-v24/ic_launcher_foreground.xml b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 0000000..2b068d1 --- /dev/null +++ b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_launcher_background.xml b/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..07d5da9 --- /dev/null +++ b/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..be54dde --- /dev/null +++ b/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,276 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml b/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml new file mode 100644 index 0000000..eca70cf --- /dev/null +++ b/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml b/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml new file mode 100644 index 0000000..eca70cf --- /dev/null +++ b/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/mipmap-hdpi/ic_launcher.webp b/app/src/main/res/mipmap-hdpi/ic_launcher.webp new file mode 100644 index 0000000..c209e78 Binary files /dev/null and b/app/src/main/res/mipmap-hdpi/ic_launcher.webp differ diff --git a/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp b/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp new file mode 100644 index 0000000..b2dfe3d Binary files /dev/null and b/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp differ diff --git a/app/src/main/res/mipmap-mdpi/ic_launcher.webp b/app/src/main/res/mipmap-mdpi/ic_launcher.webp new file mode 100644 index 0000000..4f0f1d6 Binary files /dev/null and b/app/src/main/res/mipmap-mdpi/ic_launcher.webp differ diff --git a/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp b/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp new file mode 100644 index 0000000..62b611d Binary files /dev/null and b/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp differ diff --git a/app/src/main/res/mipmap-xhdpi/ic_launcher.webp b/app/src/main/res/mipmap-xhdpi/ic_launcher.webp new file mode 100644 index 0000000..948a307 Binary files /dev/null and b/app/src/main/res/mipmap-xhdpi/ic_launcher.webp differ diff --git a/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp b/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp new file mode 100644 index 0000000..1b9a695 Binary files /dev/null and b/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp differ diff --git a/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp b/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp new file mode 100644 index 0000000..28d4b77 Binary files /dev/null and b/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp differ diff --git a/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp b/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp new file mode 100644 index 0000000..9287f50 Binary files /dev/null and b/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp differ diff --git a/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp b/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp new file mode 100644 index 0000000..aa7d642 Binary files /dev/null and b/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp differ diff --git a/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp b/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp new file mode 100644 index 0000000..9126ae3 Binary files /dev/null and b/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp differ diff --git a/app/src/main/res/values-night/themes.xml b/app/src/main/res/values-night/themes.xml new file mode 100644 index 0000000..d84fedb --- /dev/null +++ b/app/src/main/res/values-night/themes.xml @@ -0,0 +1,16 @@ + + + + \ No newline at end of file diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml new file mode 100644 index 0000000..f8c6127 --- /dev/null +++ b/app/src/main/res/values/colors.xml @@ -0,0 +1,10 @@ + + + #FFBB86FC + #FF6200EE + #FF3700B3 + #FF03DAC5 + #FF018786 + #FF000000 + #FFFFFFFF + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml new file mode 100644 index 0000000..8e7ca31 --- /dev/null +++ b/app/src/main/res/values/strings.xml @@ -0,0 +1,3 @@ + + StepViewDemo + \ No newline at end of file diff --git a/app/src/main/res/values/themes.xml b/app/src/main/res/values/themes.xml new file mode 100644 index 0000000..47778ac --- /dev/null +++ b/app/src/main/res/values/themes.xml @@ -0,0 +1,16 @@ + + + + \ No newline at end of file diff --git a/app/src/main/res/xml/backup_rules.xml b/app/src/main/res/xml/backup_rules.xml new file mode 100644 index 0000000..fa0f996 --- /dev/null +++ b/app/src/main/res/xml/backup_rules.xml @@ -0,0 +1,13 @@ + + + + \ No newline at end of file diff --git a/app/src/main/res/xml/data_extraction_rules.xml b/app/src/main/res/xml/data_extraction_rules.xml new file mode 100644 index 0000000..9ee9997 --- /dev/null +++ b/app/src/main/res/xml/data_extraction_rules.xml @@ -0,0 +1,19 @@ + + + + + + + \ No newline at end of file diff --git a/app/src/test/java/com/github/smaxlyb/stepviewdemo/ExampleUnitTest.kt b/app/src/test/java/com/github/smaxlyb/stepviewdemo/ExampleUnitTest.kt new file mode 100644 index 0000000..9f2c838 --- /dev/null +++ b/app/src/test/java/com/github/smaxlyb/stepviewdemo/ExampleUnitTest.kt @@ -0,0 +1,17 @@ +package com.github.smaxlyb.stepviewdemo + +import org.junit.Test + +import org.junit.Assert.* + +/** + * Example local unit test, which will execute on the development machine (host). + * + * See [testing documentation](http://d.android.com/tools/testing). + */ +class ExampleUnitTest { + @Test + fun addition_isCorrect() { + assertEquals(4, 2 + 2) + } +} \ No newline at end of file diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..6fc57f6 --- /dev/null +++ b/build.gradle @@ -0,0 +1,10 @@ +// Top-level build file where you can add configuration options common to all sub-projects/modules. +plugins { + id 'com.android.application' version '7.2.0-beta04' apply false + id 'com.android.library' version '7.2.0-beta04' apply false + id 'org.jetbrains.kotlin.android' version '1.7.0' apply false +} + +task clean(type: Delete) { + delete rootProject.buildDir +} \ No newline at end of file diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 0000000..cd0519b --- /dev/null +++ b/gradle.properties @@ -0,0 +1,23 @@ +# Project-wide Gradle settings. +# IDE (e.g. Android Studio) users: +# Gradle settings configured through the IDE *will override* +# any settings specified in this file. +# For more details on how to configure your build environment visit +# http://www.gradle.org/docs/current/userguide/build_environment.html +# Specifies the JVM arguments used for the daemon process. +# The setting is particularly useful for tweaking memory settings. +org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8 +# When configured, Gradle will run in incubating parallel mode. +# This option should only be used with decoupled projects. More details, visit +# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects +# org.gradle.parallel=true +# AndroidX package structure to make it clearer which packages are bundled with the +# Android operating system, and which are packaged with your app"s APK +# https://developer.android.com/topic/libraries/support-library/androidx-rn +android.useAndroidX=true +# Kotlin code style for this project: "official" or "obsolete": +kotlin.code.style=official +# Enables namespacing of each library's R class so that its R class includes only the +# resources declared in the library itself and none from the library's dependencies, +# thereby reducing the size of the R class for that library +android.nonTransitiveRClass=true \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..e708b1c Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..b5fa812 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Sat Apr 15 11:23:39 CST 2023 +distributionBase=GRADLE_USER_HOME +distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip +distributionPath=wrapper/dists +zipStorePath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME diff --git a/gradlew b/gradlew new file mode 100644 index 0000000..4f906e0 --- /dev/null +++ b/gradlew @@ -0,0 +1,185 @@ +#!/usr/bin/env sh + +# +# Copyright 2015 the original author or authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin or MSYS, switch paths to Windows format before running java +if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=`expr $i + 1` + done + case $i in + 0) set -- ;; + 1) set -- "$args0" ;; + 2) set -- "$args0" "$args1" ;; + 3) set -- "$args0" "$args1" "$args2" ;; + 4) set -- "$args0" "$args1" "$args2" "$args3" ;; + 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=`save "$@"` + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..107acd3 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,89 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/jitpack.yml b/jitpack.yml new file mode 100644 index 0000000..adb3fe1 --- /dev/null +++ b/jitpack.yml @@ -0,0 +1,2 @@ +jdk: + - openjdk11 diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..862bbe3 --- /dev/null +++ b/settings.gradle @@ -0,0 +1,17 @@ +pluginManagement { + repositories { + gradlePluginPortal() + google() + mavenCentral() + } +} +dependencyResolutionManagement { + repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) + repositories { + google() + mavenCentral() + } +} +rootProject.name = "StepViewDemo" +include ':app' +include ':stepView' diff --git a/stepView/.gitignore b/stepView/.gitignore new file mode 100644 index 0000000..42afabf --- /dev/null +++ b/stepView/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/stepView/build.gradle b/stepView/build.gradle new file mode 100644 index 0000000..8f46870 --- /dev/null +++ b/stepView/build.gradle @@ -0,0 +1,51 @@ +plugins { + id 'com.android.library' + id 'org.jetbrains.kotlin.android' + id 'maven-publish' +} + +android { + compileSdk 32 + + defaultConfig { + minSdk 23 + targetSdk 32 + + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + consumerProguardFiles "consumer-rules.pro" + } + + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } + kotlinOptions { + jvmTarget = '1.8' + } +} + +dependencies { + + implementation 'androidx.core:core-ktx:1.7.0' + implementation 'androidx.appcompat:appcompat:1.3.0' + implementation 'com.google.android.material:material:1.4.0' +} + +afterEvaluate { + publishing { + publications { + release(MavenPublication) { + from components.release + groupId = 'com.github.smaxlyb' //groupId 随便取 + artifactId = 'stepview' //artifactId 随便取 + version = '1.0.0' + } + } + } +} \ No newline at end of file diff --git a/stepView/consumer-rules.pro b/stepView/consumer-rules.pro new file mode 100644 index 0000000..e69de29 diff --git a/stepView/proguard-rules.pro b/stepView/proguard-rules.pro new file mode 100644 index 0000000..481bb43 --- /dev/null +++ b/stepView/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile \ No newline at end of file diff --git a/stepView/src/main/AndroidManifest.xml b/stepView/src/main/AndroidManifest.xml new file mode 100644 index 0000000..79f25aa --- /dev/null +++ b/stepView/src/main/AndroidManifest.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/stepView/src/main/java/com/github/smaxlyb/stepview/Extention.kt b/stepView/src/main/java/com/github/smaxlyb/stepview/Extention.kt new file mode 100644 index 0000000..4ea5f2e --- /dev/null +++ b/stepView/src/main/java/com/github/smaxlyb/stepview/Extention.kt @@ -0,0 +1,43 @@ +package com.github.smaxlyb.stepview + +import android.content.res.Resources +import android.graphics.Canvas +import android.graphics.Paint +import android.util.TypedValue +import kotlin.math.ceil + +val Int.dp + get() = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, this.toFloat(), Resources.getSystem().displayMetrics) + + +val Int.sp + get() = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, this.toFloat(), Resources.getSystem().displayMetrics) + + +val Paint.fontLineHeight: Float + get() = fontMetrics.run { bottom - top + leading } + +/** + * 绘制多行文本, 超过自动换行截断 + * @param x 起始x坐标, + * @param y 起始y坐标, 从文字基线开始 + * @param maxLine 最大行数, 超过会被截断 + * @param maxWidth 最大宽度, 超过会换行 + */ +fun Canvas.drawMultilineText(x: Float, y: Float, paint: Paint, text: String, maxLine: Int, maxWidth: Float) { + val textW = paint.measureText(text) + val textLine = textW / maxWidth + val line = if (textLine > maxLine) { + maxLine + } else { + ceil(textLine).toInt() + } + var start = 0 + var end = 0 + repeat(line) { + start = end + end += paint.breakText(text, start, text.length, true, maxWidth, floatArrayOf(0F)) + val s = text.substring(start, end) + drawText(s, x, y + paint.fontLineHeight * it, paint) + } +} \ No newline at end of file diff --git a/stepView/src/main/java/com/github/smaxlyb/stepview/HorizontalStepViewHelper.kt b/stepView/src/main/java/com/github/smaxlyb/stepview/HorizontalStepViewHelper.kt new file mode 100644 index 0000000..8ec7b6a --- /dev/null +++ b/stepView/src/main/java/com/github/smaxlyb/stepview/HorizontalStepViewHelper.kt @@ -0,0 +1,175 @@ +package com.github.smaxlyb.stepview + +import android.graphics.Canvas +import android.graphics.Paint +import android.graphics.Rect +import android.graphics.drawable.Drawable +import android.view.MotionEvent +import android.view.View +import androidx.appcompat.content.res.AppCompatResources +import androidx.core.content.ContextCompat +import androidx.core.graphics.drawable.DrawableCompat +import com.github.smaxlyb.stepview.* +import kotlin.math.ceil + +/** + * @description: 水平绘制 + */ +class HorizontalStepViewHelper( + private val stepView: StepView +) : IStepViewHelper { + + companion object { + // 不包含副标题的固定高度, 减少测量 + private val DEFAULT_HEIGHT_H = 80.dp + + // 含有副标题时固定高度, 减少测量 + private val DEFAULT_HEIGHT_WITH_ASSIST_TEXT_H = 120.dp + + private const val MAIN_TEXT_MAX_SIZE_H = 4 + private const val ASSIST_TEXT_MAX_LINE_H = 2 + } + + private var mUndoIcon: Drawable? = NumberCircleDrawable() + private var mDoingIcon: Drawable? = NumberCircleDrawable() + private var mDoneIcon: Drawable? = AppCompatResources.getDrawable(stepView.context, R.drawable.ic_done) + private var mWarnIcon: Drawable? = AppCompatResources.getDrawable(stepView.context, R.drawable.ic_warn) + + override fun init() { + tintColor() + } + + override fun getMeasureSpec(widthMeasureSpec: Int, heightMeasureSpec: Int): Pair { + val h = View.resolveSize( + // 如果没有副标题文字 + if (stepView.mNodes.firstOrNull { !it.detail.isNullOrBlank() } == null) { + DEFAULT_HEIGHT_H.toInt() + } else { + DEFAULT_HEIGHT_WITH_ASSIST_TEXT_H.toInt() + }, heightMeasureSpec + ) + return widthMeasureSpec to h + } + + override fun drawContent(canvas: Canvas) { + stepView.run { + val halfOfItemW = mRemainW / mSize / 2 + val iconY = paddingTop + mRemainH / 4 + val mainTextY = paddingTop + mRemainH / 4 * 2 + val assistTextY = mainTextY + mTitlePaint.fontLineHeight + + mNodes.forEachIndexed { index, node -> + // 每个item的起始位置x + val iconX = + if (mDirection == StepView.Direction.FromLeft) { + paddingLeft + (index * 2 + 1) * halfOfItemW + } else { + paddingLeft + ((mSize - index) * 2 - 1) * halfOfItemW + } + // 画横线 + kotlin.runCatching { + val next = mNodes[index + 1] + val lineStartX: Float + val lineEndX = + if (mDirection == StepView.Direction.FromLeft) { + lineStartX = iconX + mLine2IconPadding + iconX + 2 * halfOfItemW - mLine2IconPadding + } else { + lineStartX = iconX - mLine2IconPadding + iconX - 2 * halfOfItemW + mLine2IconPadding + } + canvas.drawLine( + lineStartX, iconY, lineEndX, iconY, + if (node.state == StepView.State.Done && next.state != StepView.State.Undo) { + mLinePaint.apply { color = mDoneColor } + } else { + mLinePaint.apply { color = mUndoColor } + }) + } + // 画icon + val bound = Rect( + (iconX - mIconRadius).toInt(), (iconY - mIconRadius).toInt(), + (iconX + mIconRadius).toInt(), (iconY + mIconRadius).toInt() + ) + drawIcon(canvas, index, node, bound) + + // 画主标题 + canvas.drawText( + node.title, 0, + if (node.title.length > MAIN_TEXT_MAX_SIZE_H) { + MAIN_TEXT_MAX_SIZE_H + } else { + node.title.length + }, + iconX, mainTextY, mTitlePaint.apply { + color = if (node.state == StepView.State.Undo) { + mTitleColor + } else { + ContextCompat.getColor(context, R.color.black) + } + textAlign = Paint.Align.CENTER + } + ) + + // 画副标题 + node.detail?.let { + canvas.drawMultilineText( + iconX, assistTextY, mDetailPaint.apply { textAlign = Paint.Align.CENTER }, it, + ASSIST_TEXT_MAX_LINE_H, mRemainW / mSize - mNodePadding * 2 + ) + } + } + } + } + + override fun handleEvent(event: MotionEvent, listener: StepClickListener): Boolean { + stepView.run { + val x = event.x - paddingLeft + // 平均分, 不分左右方向 + val n = ceil(x / (mRemainW / mSize)).toInt() - 1 + when (mDirection) { + StepView.Direction.FromLeft -> { + listener.invoke(n, mNodes[n]) + } + else -> { + listener.invoke(mSize - 1 - n, mNodes[mSize - 1 - n]) + } + } + } + return true + } + + private fun tintColor() { + mUndoIcon?.let { DrawableCompat.setTint(it, stepView.mUndoColor) } + mDoneIcon?.let { DrawableCompat.setTint(it, stepView.mDoneColor) } + mDoingIcon?.let { DrawableCompat.setTint(it, stepView.mDoneColor) } + mWarnIcon?.let { DrawableCompat.setTint(it, stepView.mDoneColor) } + } + + private fun drawIcon(canvas: Canvas, index: Int, node: StepModel, bound: Rect) { + val drawable = when (node.state) { + StepView.State.Undo -> { + (mUndoIcon as NumberCircleDrawable) + .setNum(index + 1) + .setTint(stepView.mUndoColor) + mUndoIcon + } + StepView.State.Doing -> { + (mDoingIcon as NumberCircleDrawable) + .setNum(index + 1) + .setTint(stepView.mDoneColor) + mDoingIcon + } + StepView.State.Done -> { + mDoneIcon + } + StepView.State.Warn -> { + mWarnIcon + } + } + drawable?.run { + bounds = bound + draw(canvas) + } + } +} \ No newline at end of file diff --git a/stepView/src/main/java/com/github/smaxlyb/stepview/IStepViewHelper.kt b/stepView/src/main/java/com/github/smaxlyb/stepview/IStepViewHelper.kt new file mode 100644 index 0000000..1da9875 --- /dev/null +++ b/stepView/src/main/java/com/github/smaxlyb/stepview/IStepViewHelper.kt @@ -0,0 +1,27 @@ +package com.github.smaxlyb.stepview + +import android.graphics.Canvas +import android.view.MotionEvent + +interface IStepViewHelper { + + fun init() + + /** + * 测量 + * 根据传入的父view测量规格和当前实体数据, 计算出当前view要显示的宽高 + * @return 返回pair对象, 第一个int代表计算出的宽规格, 第二个int代表计算出的高规格 + */ + fun getMeasureSpec(widthMeasureSpec: Int, heightMeasureSpec: Int): Pair + + /** + * 绘制内容 + */ + fun drawContent(canvas: Canvas) + + /** + * 处理点击事件 + * @return 是否终止事件传递 + */ + fun handleEvent(event: MotionEvent, listener: StepClickListener): Boolean +} \ No newline at end of file diff --git a/stepView/src/main/java/com/github/smaxlyb/stepview/NumberCircleDrawable.kt b/stepView/src/main/java/com/github/smaxlyb/stepview/NumberCircleDrawable.kt new file mode 100644 index 0000000..87510e4 --- /dev/null +++ b/stepView/src/main/java/com/github/smaxlyb/stepview/NumberCircleDrawable.kt @@ -0,0 +1,60 @@ +package com.github.smaxlyb.stepview + +import android.annotation.SuppressLint +import android.graphics.* +import android.graphics.drawable.Drawable +import androidx.core.graphics.drawable.TintAwareDrawable +import kotlin.math.min + +/** + * @description: 数字圆① + */ +@SuppressLint("RestrictedApi") +class NumberCircleDrawable : Drawable(), TintAwareDrawable { + + private val mPaint = Paint().apply { + isAntiAlias = true + isDither = true + } + private var mBgColor = 0 + private var mNum = 0 + + override fun draw(canvas: Canvas) { + val r = min(bounds.width(), bounds.height()) / 2f - 1.dp + val x = bounds.left + bounds.width() / 2f + val y = bounds.top + bounds.height() / 2f + mPaint.color = mBgColor + canvas.drawCircle(x, y, r, mPaint) + mPaint.apply { + color = Color.WHITE + textSize = 12.sp + textAlign = Paint.Align.CENTER + } + val rect = Rect() + mPaint.getTextBounds("$mNum", 0, 1, rect) + // 因为baseline, -1稍微上移一点点, 减少计算 + canvas.drawText("$mNum", 0, 1, x, y + rect.height() / 2 - 1, mPaint) + } + + override fun setTint(tintColor: Int) { + mBgColor = tintColor + } + + override fun setAlpha(alpha: Int) { + mPaint.alpha = alpha + invalidateSelf() + } + + override fun setColorFilter(colorFilter: ColorFilter?) { + mPaint.colorFilter = colorFilter + invalidateSelf() + } + + fun setNum(num: Int) = run { + mNum = num + this + } + + @Deprecated("Deprecated in Java", ReplaceWith("PixelFormat.TRANSLUCENT", "android.graphics.PixelFormat")) + override fun getOpacity() = PixelFormat.TRANSLUCENT +} \ No newline at end of file diff --git a/stepView/src/main/java/com/github/smaxlyb/stepview/StepModel.kt b/stepView/src/main/java/com/github/smaxlyb/stepview/StepModel.kt new file mode 100644 index 0000000..30c5f5d --- /dev/null +++ b/stepView/src/main/java/com/github/smaxlyb/stepview/StepModel.kt @@ -0,0 +1,27 @@ +package com.github.smaxlyb.stepview + +/** + * @description: stepView数据模型 + */ +data class StepModel( + /** + * 主标题文字, 不可以为null, 也不可以为空字符串 + */ + var title: String, + + /** + * 副标题文字, 可以为null + * 当这个节点不需要副标题文字时, 设置为null, 而不是空字符串 + */ + var detail: String? = null, + + /** + * 节点状态, 控制图标和颜色 + */ + var state: StepView.State = StepView.State.Undo, + + /** + * 是否完成 保留字段, 后续如果要将图标和颜色分离控制就使用这个字段 + */ + var isFinished: Boolean = false +) \ No newline at end of file diff --git a/stepView/src/main/java/com/github/smaxlyb/stepview/StepView.kt b/stepView/src/main/java/com/github/smaxlyb/stepview/StepView.kt new file mode 100644 index 0000000..b97c672 --- /dev/null +++ b/stepView/src/main/java/com/github/smaxlyb/stepview/StepView.kt @@ -0,0 +1,382 @@ +package com.github.smaxlyb.stepview + +import android.annotation.SuppressLint +import android.content.Context +import android.graphics.Canvas +import android.graphics.Paint +import android.graphics.RectF +import android.util.AttributeSet +import android.view.MotionEvent +import android.view.View +import androidx.core.content.ContextCompat +import androidx.core.content.res.use +/** + * @description: 步骤条/流程条, 在xml中使用时, 推荐使用wrap_content设置高度, 如果不合适, 再设置具体值 + */ +typealias StepClickListener = (Int, StepModel) -> Unit + +class StepView @JvmOverloads constructor( + context: Context, + attrs: AttributeSet? = null, + defStyleAttr: Int = 0 +) : View(context, attrs, defStyleAttr) { + + /*================ xml可控变量 =====================*/ + private lateinit var mOrientation: Orientation + internal lateinit var mDirection: Direction + internal var mLine2IconPadding: Float = 0f + internal var mNodePadding: Float = 0f + private var mLineWidth: Float = DEFAULT_LINE_WIDTH + internal var mIconRadius: Float = DEFAULT_ICON_RADIUS + internal var mIcon2TextPadding: Float = DEFAULT_ICON_TEXT_PADDING_V + + /*================ 绘图相关变量 =====================*/ + private lateinit var viewHelper: IStepViewHelper + internal lateinit var mTitlePaint: Paint + internal lateinit var mDetailPaint: Paint + internal lateinit var mLinePaint: Paint + internal var mTitleColor = ContextCompat.getColor(context, R.color.gray) + private var mDetailColor = ContextCompat.getColor(context, R.color.gray) + internal var mDoneColor = ContextCompat.getColor(context, R.color.blue) + internal var mUndoColor = ContextCompat.getColor(context, R.color.light_white) + internal var mTotalW = 0f + internal var mTotalH = 0f + internal var mRemainH = 0f + internal var mRemainW = 0f + + /*==================== 数据变量 =====================*/ + internal val mNodes = mutableListOf() + internal val mSize: Int get() = mNodes.size + private var mOnStepClickListener: StepClickListener? = null + + /*====================== 常量 ========================*/ + companion object { + // 竖直时文字和icon的默认间距 + private val DEFAULT_ICON_TEXT_PADDING_V = 10.dp + + // icon默认半径 + private val DEFAULT_ICON_RADIUS = 9.dp + + // 线条默认厚度 + private val DEFAULT_LINE_WIDTH = 2.dp + } + + init { + context.obtainStyledAttributes(attrs, R.styleable.StepView, defStyleAttr, R.style.StepView).use { + mOrientation = Orientation.getOrientation(it.getInt(R.styleable.StepView_android_orientation, 0)) + mDirection = Direction.getDirection(it.getInt(R.styleable.StepView_step_view_direction, 0)) + adjustDirection() + mLine2IconPadding = it.getDimension(R.styleable.StepView_step_view_line_2_icon_padding, 0f) + mNodePadding = it.getDimension(R.styleable.StepView_step_view_node_padding, 0f) + mLineWidth = it.getDimension(R.styleable.StepView_step_view_line_width, DEFAULT_LINE_WIDTH) + mIconRadius = it.getDimension(R.styleable.StepView_step_view_radius, DEFAULT_ICON_RADIUS) + } + initPaint() + initHelper() + } + + private fun initHelper() { + viewHelper = if (mOrientation == Orientation.Horizontal) { + HorizontalStepViewHelper(this) + } else { + VerticalStepViewHelper(this) + } + + viewHelper.init() + } + + private fun initPaint() { + setLayerType(LAYER_TYPE_SOFTWARE, null) + mTitlePaint = Paint().also { + it.isDither = true + it.isAntiAlias = true + it.textSize = 14.sp + it.color = mTitleColor + it.textAlign = Paint.Align.CENTER + } + mDetailPaint = Paint().also { + it.isDither = true + it.isAntiAlias = true + it.textSize = 12.sp + it.color = mDetailColor + it.textAlign = Paint.Align.CENTER + } + mLinePaint = Paint().also { + it.isDither = true + it.isAntiAlias = true + it.strokeWidth = mLineWidth + it.strokeCap = Paint.Cap.ROUND + it.color = mUndoColor + } + } + + override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { + val pair = viewHelper.getMeasureSpec(widthMeasureSpec, heightMeasureSpec) + setMeasuredDimension(pair.first, pair.second) + } + + override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) { + mTotalH = h.toFloat() + mTotalW = w.toFloat() + mRemainH = mTotalH - paddingTop - paddingBottom + mRemainW = mTotalW - paddingLeft - paddingRight + super.onSizeChanged(w, h, oldw, oldh) + } + + override fun onDraw(canvas: Canvas) { + if (mSize == 0) { + canvas.drawText("您还没有设置数据", 0, 8, mTotalW / 2, mTotalH / 2, mTitlePaint) + return + } + viewHelper.drawContent(canvas) + } + + @SuppressLint("ClickableViewAccessibility") + override fun onTouchEvent(event: MotionEvent): Boolean { + val rect = RectF(paddingLeft.toFloat(), paddingTop.toFloat(), mTotalW - paddingRight, mTotalH - paddingBottom) + if (event.action != 0 || + mSize == 0 || + mOnStepClickListener == null || + !rect.contains(event.x, event.y) + ) { + return super.onTouchEvent(event) + } + + return viewHelper.handleEvent(event, mOnStepClickListener!!) + } + + /** + * 设置纵向还是横向布局 + */ + fun setOrientation(orientation: Orientation) = run { + // 如果相等, 不改变 + if (mOrientation == orientation) { + return@run this + } + mOrientation = orientation + initHelper() + adjustDirection() + requestLayout() + invalidate() + this + } + + /** + * 设置进度起始方向, 会影响节点绘制的方向, 但不会影响原有的数据下标, + * 也就是下标是跟随传入的时候的数据下标, 它在原数据下标是多少就是多少, + * 永远不会改变的, 即使绘制的方向发生了改变, 一切以原数据下标为主 + * + * 例如初始传入数据 a->b->c->d->e + * + * 如果使用FromLeft, 在屏幕上会显示: a b c d e, + * 点击b会提示点击了第1个数据(下标从0开始) + * + * 如果使用FromRight, 在屏幕上会显示: e d c b a, + * 点击b也会提示点击了第1个数据(下标从0开始), 即使b现在从左往右数是第3个, + * 不会随方向而改变自身下标 + * + * 例如要在index=1处新增f, 数据变为: a->f->b->c->d->e + * + * 如果使用FromLeft, 在屏幕上会显示: a f b c d e + * + * 如果使用FromRight, 在屏幕上会显示: e d c b f a + * + * 删除操作也是一样, FromTop和FromBottom同理 + */ + fun setDirection(direction: Direction) = run { + if (mDirection == direction) { + return@run this + } + mDirection = direction + adjustDirection() + requestLayout() + invalidate() + this + } + + /** + * 设置数据, 会清空之前的数据 + */ + + fun setNodes(nodes: List) = run { + clearAllNodes() + addNodes(nodes) + requestLayout() + invalidate() + this + } + + /** + * 添加节点, 位置从0开始, 默认添加到尾部 + * @param index 参考 [setDirection] + */ + @JvmOverloads + fun addNode(node: StepModel, index: Int = mSize) = run { + mNodes.add(index, node) + requestLayout() + invalidate() + this + } + + /** + * 批量添加, 位置从0开始, 默认添加到尾部 + * @param index 参考 [setDirection] + */ + @JvmOverloads + fun addNodes(nodes: List, index: Int = mSize) = run { + mNodes.addAll(index, nodes) + this + } + + /** + * 清除所有节点 + */ + fun clearAllNodes() = run { + mNodes.clear() + requestLayout() + invalidate() + this + } + + /** + * 删除节点, 范围从0开始, 不超过size-1 + * @param index 参考 [setDirection] + */ + fun removeNodeAt(index: Int) = run { + mNodes.removeAt(index) + requestLayout() + invalidate() + this + } + + /** + * 设置已经完成的位置 从0开始 + * 可以设置多个 + * @param indexes 参考 [setDirection] + */ + fun setDoneAt(vararg indexes: Int) = run { + indexes.forEach { + mNodes[it].state = State.Done + } + invalidate() + this + } + + /** + * 设置已完成的位置 从0开始 + * @param from 起始位置(包含) + * @param to 结束位置(包含) + */ + fun setDoneRange(from: Int = 0, to: Int) = run { + for (i in from..to) { + mNodes[i].state = State.Done + } + invalidate() + this + } + + /** + * 设置进行中的位置 从0开始 + * 可以设置多个, 一般是最后一个位置 + * @param indexes 参考 [setDirection] + */ + fun setDoingAt(vararg indexes: Int) = run { + indexes.forEach { + mNodes[it].state = State.Doing + } + invalidate() + this + } + + /** + * 设置未完成的位置 从0开始 + * 可以设置多个 + * @param indexes 参考 [setDirection] + */ + fun setUndoAt(vararg indexes: Int) = run { + indexes.forEach { + mNodes[it].state = State.Undo + } + invalidate() + this + } + + /** + * 设置异常的位置, 从0开始 + * 可以设置多个,异常位置逻辑上一般不可以超过最后一个已完成位置 + * @param indexes 参考 [setDirection] + */ + fun setErrorAt(vararg indexes: Int) = run { + indexes.forEach { + mNodes[it].state = State.Warn + } + invalidate() + this + } + + /** + * 设置点击监听, 第一个参数是点击的位置, 从0开始 + * 绘制方向会影响点击位置的数值, 具体参考[setDirection] + */ + fun setOnNodeClickListener(listener: ((Int, StepModel) -> Unit)) = run { + mOnStepClickListener = listener + this + } + + private fun adjustDirection() { + mDirection = when (mOrientation) { + Orientation.Horizontal -> { + // 在 + if (mDirection in setOf(Direction.FromLeft, Direction.FromRight)) { + return + } + Direction.FromLeft + } + Orientation.Vertical -> { + if (mDirection in setOf(Direction.FromTop, Direction.FromBottom)) { + return + } + Direction.FromTop + } + } + } + + sealed class State { + object Undo : State() + object Doing : State() + object Done : State() + object Warn : State() + } + + sealed class Direction(val value: Int) { + object FromLeft : Direction(0) + object FromRight : Direction(1) + object FromTop : Direction(2) + object FromBottom : Direction(3) + companion object { + fun getDirection(value: Int): Direction { + return when (value) { + 0 -> FromLeft + 1 -> FromRight + 2 -> FromTop + else -> FromBottom + } + } + } + } + + sealed class Orientation(val value: Int) { + object Horizontal : Orientation(0) + object Vertical : Orientation(1) + + companion object { + fun getOrientation(value: Int): Orientation { + return if (value == 0) { + Horizontal + } else { + Vertical + } + } + } + } +} \ No newline at end of file diff --git a/stepView/src/main/java/com/github/smaxlyb/stepview/VerticalStepViewHelper.kt b/stepView/src/main/java/com/github/smaxlyb/stepview/VerticalStepViewHelper.kt new file mode 100644 index 0000000..723a22c --- /dev/null +++ b/stepView/src/main/java/com/github/smaxlyb/stepview/VerticalStepViewHelper.kt @@ -0,0 +1,241 @@ +package com.github.smaxlyb.stepview + +import android.graphics.Canvas +import android.graphics.Paint +import android.graphics.Rect +import android.graphics.drawable.Drawable +import android.util.SparseArray +import android.util.SparseIntArray +import android.view.MotionEvent +import android.view.View +import androidx.appcompat.content.res.AppCompatResources +import androidx.core.content.ContextCompat +import androidx.core.graphics.drawable.DrawableCompat +import androidx.core.util.valueIterator +import kotlin.math.ceil + +/** + * @description: 垂直绘制 + */ +class VerticalStepViewHelper( + private val stepView: StepView +) : IStepViewHelper { + + companion object { + // 节点的高度 + private val DEFAULT_NODE_HEIGHT_V = 66.dp + private const val MAIN_TEXT_MAX_LINE_V = 2 + private const val ASSIST_TEXT_MAX_LINE_V = 2 + } + + private var mUndoIcon: Drawable? = AppCompatResources.getDrawable(stepView.context, R.drawable.ic_undo) + private var mDoingIcon: Drawable? = AppCompatResources.getDrawable(stepView.context, R.drawable.ic_doing) + private var mDoneIcon: Drawable? = AppCompatResources.getDrawable(stepView.context, R.drawable.ic_done) + private var mWarnIcon: Drawable? = AppCompatResources.getDrawable(stepView.context, R.drawable.ic_warn) + + // 记录每个节点额外添加的主+副标题行高, 减少计算 + private val mTextHeightAdded = SparseArray() + + // 记录每个节点的主标题行数, 减少计算 + private val mTitleLine = SparseIntArray() + + override fun init() { + tintColor() + } + + override fun getMeasureSpec(widthMeasureSpec: Int, heightMeasureSpec: Int): Pair { + // 重置数据 + mTextHeightAdded.clear() + // 如果为空数据, 返回一个节点的高度 + val h = if (stepView.mNodes.isEmpty()) { + View.resolveSize(DEFAULT_NODE_HEIGHT_V.toInt(), heightMeasureSpec) + } else { + // 计算剩余宽度 + var totalH = DEFAULT_NODE_HEIGHT_V * stepView.mNodes.size + val remainWForText = View.MeasureSpec.getSize(widthMeasureSpec) - + stepView.paddingLeft - stepView.paddingRight - + stepView.mIconRadius * 2 - stepView.mIcon2TextPadding + + // 记录增加的行高 + stepView.mNodes.forEachIndexed { index, node -> + // 主标题行数 + val mainTextNeedW = stepView.mTitlePaint.measureText(node.title) + var mainTextNeedLine = ceil(mainTextNeedW / remainWForText).toInt() + if (mainTextNeedLine > MAIN_TEXT_MAX_LINE_V) { + mainTextNeedLine = MAIN_TEXT_MAX_LINE_V + } + mTitleLine.put(index, mainTextNeedLine) + var heightAdded = (mainTextNeedLine - 1) * stepView.mTitlePaint.fontLineHeight + // 副标题行数 + node.detail.takeIf { !it.isNullOrBlank() }?.run { + val assistTextNeedW = stepView.mDetailPaint.measureText(this) + var assistTextNeedLine = ceil(assistTextNeedW / remainWForText).toInt() + if (assistTextNeedLine > ASSIST_TEXT_MAX_LINE_V) { + assistTextNeedLine = ASSIST_TEXT_MAX_LINE_V + } + heightAdded += (assistTextNeedLine - 1) * stepView.mDetailPaint.fontLineHeight + } + mTextHeightAdded.put(index, heightAdded) + } + totalH += mTextHeightAdded.valueIterator().asSequence().sum().toInt() + View.resolveSize(totalH.toInt(), heightMeasureSpec) + } + return widthMeasureSpec to h + } + + override fun drawContent(canvas: Canvas) { + stepView.run { + // 真正剩余的高度 + val remainH = mTotalH - paddingTop - paddingBottom - mTextHeightAdded.valueIterator().asSequence().sum() + // 每个节点理应均分高度 + val itemH = remainH / mSize + // icon的x坐标是固定死的 + val iconX = paddingLeft + mIconRadius + // 文字的x坐标是固定死的 + val textX = iconX + mIconRadius + mIcon2TextPadding + // 每个节点绘制区域的上边界 + var startY = + if (mDirection == StepView.Direction.FromTop) { + paddingTop.toFloat() + } else { + // 反向的时候第一个上边界 + mTotalH - paddingBottom - itemH - mTextHeightAdded.get(0) + } + mNodes.forEachIndexed { index, node -> + // 5dp是图标距上边界的距离 + val iconY = startY + mIconRadius * 2 + 5.dp + // 画横线 + kotlin.runCatching { + val next = mNodes[index + 1] + val lineStartY: Float + val lineEndY = + if (mDirection == StepView.Direction.FromTop) { + lineStartY = iconY + mLine2IconPadding + iconY + (itemH + mTextHeightAdded.get(index)) - mLine2IconPadding + } else { + lineStartY = iconY - mLine2IconPadding + iconY - (itemH + mTextHeightAdded.get(index + 1)) + mLine2IconPadding + } + + canvas.drawLine( + iconX, lineStartY, iconX, lineEndY, + if (node.state == StepView.State.Done && next.state != StepView.State.Undo) { + mLinePaint.apply { color = mDoneColor } + } else { + mLinePaint.apply { color = mUndoColor } + }) + } + + // 画icon + val bound = Rect( + (iconX - mIconRadius).toInt(), (iconY - mIconRadius).toInt(), + (iconX + mIconRadius).toInt(), (iconY + mIconRadius).toInt() + ) + drawIcon(canvas, node, bound) + + // 画主标题 + canvas.drawMultilineText( + textX, iconY + mTitlePaint.fontLineHeight / 4, mTitlePaint.apply { + textAlign = Paint + .Align.LEFT + color = if (node.state == StepView.State.Undo) { + mTitleColor + } else { + ContextCompat.getColor(context, R.color.black) + } + }, + node.title, + MAIN_TEXT_MAX_LINE_V, + mTotalW - paddingLeft - paddingRight - mIconRadius * 2 - mIcon2TextPadding + ) + + // 画副标题 + node.detail?.let { + canvas.drawMultilineText( + textX, + iconY + mTitlePaint.fontLineHeight * (mTitleLine.get(index)) + mTitlePaint + .fontLineHeight / 4 + 6.dp, + mDetailPaint.apply { textAlign = Paint.Align.LEFT }, + it, + ASSIST_TEXT_MAX_LINE_V, + mTotalW - paddingLeft - paddingRight - mIconRadius * 2 - mIcon2TextPadding + ) + } + // 刷新下一个的起始点 + kotlin.runCatching { + if (mDirection == StepView.Direction.FromTop) { + startY += itemH + mTextHeightAdded.get(index) + } else { + startY -= itemH + mTextHeightAdded.get(index + 1) + } + } + } + } + } + + override fun handleEvent(event: MotionEvent, listener: StepClickListener): Boolean { + stepView.run { + val y = event.y - paddingTop + // 真正剩余的高度 + val remainH = mTotalH - paddingTop - paddingBottom - mTextHeightAdded.valueIterator().asSequence().sum() + // 每个节点理应均分高度 + val itemH = remainH / mSize + run out@{ + when (mDirection) { + StepView.Direction.FromTop -> { + var startY = paddingTop.toFloat() + repeat(mSize) { + val h = itemH + mTextHeightAdded.get(it) + if (startY < y && y < h + startY) { + listener.invoke(it, mNodes[it]) + return@out + } + startY += h + } + } + else -> { + // 反向的时候第一个下边界 + var startY = mTotalH - paddingBottom + repeat(mSize) { + val h = itemH + mTextHeightAdded.get(it) + if (startY > y && y > startY - h) { + listener.invoke(it, mNodes[it]) + return@out + } + startY -= h + } + } + } + } + } + return true + } + + private fun tintColor() { + mUndoIcon?.let { DrawableCompat.setTint(it, stepView.mUndoColor) } + mDoneIcon?.let { DrawableCompat.setTint(it, stepView.mDoneColor) } + mDoingIcon?.let { DrawableCompat.setTint(it, stepView.mDoneColor) } + mWarnIcon?.let { DrawableCompat.setTint(it, stepView.mDoneColor) } + } + + private fun drawIcon(canvas: Canvas, node: StepModel, bound: Rect) { + val drawable = when (node.state) { + StepView.State.Undo -> { + mUndoIcon + } + StepView.State.Doing -> { + mDoingIcon + } + StepView.State.Done -> { + mDoneIcon + } + StepView.State.Warn -> { + mWarnIcon + } + } + drawable?.run { + bounds = bound + draw(canvas) + } + } +} \ No newline at end of file diff --git a/stepView/src/main/res/drawable/ic_doing.xml b/stepView/src/main/res/drawable/ic_doing.xml new file mode 100644 index 0000000..5e314bf --- /dev/null +++ b/stepView/src/main/res/drawable/ic_doing.xml @@ -0,0 +1,13 @@ + + + + diff --git a/stepView/src/main/res/drawable/ic_done.xml b/stepView/src/main/res/drawable/ic_done.xml new file mode 100644 index 0000000..af1be56 --- /dev/null +++ b/stepView/src/main/res/drawable/ic_done.xml @@ -0,0 +1,9 @@ + + + diff --git a/stepView/src/main/res/drawable/ic_undo.xml b/stepView/src/main/res/drawable/ic_undo.xml new file mode 100644 index 0000000..ecb6e39 --- /dev/null +++ b/stepView/src/main/res/drawable/ic_undo.xml @@ -0,0 +1,9 @@ + + + diff --git a/stepView/src/main/res/drawable/ic_warn.xml b/stepView/src/main/res/drawable/ic_warn.xml new file mode 100644 index 0000000..7068ad0 --- /dev/null +++ b/stepView/src/main/res/drawable/ic_warn.xml @@ -0,0 +1,9 @@ + + + diff --git a/stepView/src/main/res/values/attrs.xml b/stepView/src/main/res/values/attrs.xml new file mode 100644 index 0000000..ef2b14a --- /dev/null +++ b/stepView/src/main/res/values/attrs.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/stepView/src/main/res/values/colors.xml b/stepView/src/main/res/values/colors.xml new file mode 100644 index 0000000..ac0a72b --- /dev/null +++ b/stepView/src/main/res/values/colors.xml @@ -0,0 +1,7 @@ + + + #888888 + #476DF7 + #000000 + #E5E5E5 + \ No newline at end of file diff --git a/stepView/src/main/res/values/styles.xml b/stepView/src/main/res/values/styles.xml new file mode 100644 index 0000000..b37c60e --- /dev/null +++ b/stepView/src/main/res/values/styles.xml @@ -0,0 +1,7 @@ + + + + + \ No newline at end of file