├── 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
│ │ │ ├── drawable
│ │ │ │ ├── shape_circle_indicator.xml
│ │ │ │ └── ic_launcher_background.xml
│ │ │ ├── mipmap-anydpi-v26
│ │ │ │ ├── ic_launcher.xml
│ │ │ │ └── ic_launcher_round.xml
│ │ │ ├── layout
│ │ │ │ └── activity_main.xml
│ │ │ └── drawable-v24
│ │ │ │ └── ic_launcher_foreground.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── victory
│ │ │ └── chartview
│ │ │ └── MainActivity.java
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── victory
│ │ │ └── chartview
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── victory
│ │ └── chartview
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── chartview
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ └── values
│ │ │ │ ├── strings.xml
│ │ │ │ └── colors.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── victory
│ │ │ └── chartview
│ │ │ ├── UIUtils.java
│ │ │ ├── CircleIndicatorView.java
│ │ │ └── ScrollChartView.java
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── victory
│ │ │ └── chartview
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── victory
│ │ └── chartview
│ │ └── ExampleInstrumentedTest.java
├── build.gradle
└── proguard-rules.pro
├── settings.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── gradle.properties
├── gradlew.bat
├── gradlew
└── README.md
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/chartview/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':chartview'
2 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | 滚动图表
3 |
4 |
--------------------------------------------------------------------------------
/chartview/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | chartview
3 |
4 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Victory-Over/SuperChartView/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Victory-Over/SuperChartView/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Victory-Over/SuperChartView/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Victory-Over/SuperChartView/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Victory-Over/SuperChartView/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Victory-Over/SuperChartView/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/chartview/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Victory-Over/SuperChartView/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/Victory-Over/SuperChartView/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/Victory-Over/SuperChartView/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/Victory-Over/SuperChartView/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/Victory-Over/SuperChartView/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #1E2024
4 | #1E2024
5 | #1E2024
6 |
7 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Tue Nov 27 14:02:10 CST 2018
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-4.1-all.zip
7 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/shape_circle_indicator.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
8 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/test/java/com/victory/chartview/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.victory.chartview;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/chartview/src/test/java/com/victory/chartview/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.victory.chartview;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/chartview/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 27
5 |
6 |
7 |
8 | defaultConfig {
9 | minSdkVersion 14
10 | targetSdkVersion 27
11 | versionCode 1
12 | versionName "1.0"
13 |
14 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
15 |
16 | }
17 |
18 | buildTypes {
19 | release {
20 | minifyEnabled false
21 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
22 | }
23 | }
24 |
25 | }
26 |
27 | dependencies {
28 | implementation 'com.android.support:appcompat-v7:27.1.1'
29 | }
30 |
--------------------------------------------------------------------------------
/chartview/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #1E2024
4 | #1261FF
5 | #139DFF
6 | #1C47FF
7 | #737373
8 | #331261FF
9 | #33101113
10 | #803D3D3D
11 | #139DFF
12 | #101113
13 | #3E4B7B
14 | #0780ED
15 |
16 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Built application files
2 | *.apk
3 | *.ap_
4 |
5 | # Files for the ART/Dalvik VM
6 | *.dex
7 |
8 | # Java class files
9 | *.class
10 |
11 | # Generated files
12 | bin/
13 | gen/
14 | out/
15 |
16 | # Local configuration file (sdk path, etc)
17 | local.properties
18 |
19 |
20 | # Gradle files needs.
21 | .gradle
22 | build/
23 |
24 | # Android Studio
25 | .idea
26 | *.iml
27 |
28 | # Android Studio Navigation editor temp files
29 | .navigation/
30 |
31 | # Android Studio captures folder
32 | captures/
33 |
34 | # Proguard folder generated by Eclipse
35 | proguard/
36 | .classpath
37 | .project
38 |
39 | # Log Files
40 | *.log
41 |
42 | # Signing files
43 | .signing/
44 |
45 | # Windows thumbnail db
46 | Thumbs.db
47 |
48 | # OSX files
49 | .DS_Store
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/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
22 |
--------------------------------------------------------------------------------
/chartview/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
22 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 27
5 | defaultConfig {
6 | applicationId "com.victory.chartview"
7 | minSdkVersion 14
8 | targetSdkVersion 27
9 | versionCode 1
10 | versionName "1.0"
11 | multiDexEnabled true
12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | implementation fileTree(dir: 'libs', include: ['*.jar'])
24 | implementation 'com.android.support:appcompat-v7:27.1.1'
25 | implementation 'com.android.support.constraint:constraint-layout:1.1.3'
26 | implementation project(':chartview')
27 | }
28 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/victory/chartview/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.victory.chartview;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumented test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.victory.chartview", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/chartview/src/androidTest/java/com/victory/chartview/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.victory.chartview;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumented test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.victory.chartview.test", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/chartview/src/main/java/com/victory/chartview/UIUtils.java:
--------------------------------------------------------------------------------
1 | package com.victory.chartview;
2 |
3 | import android.content.Context;
4 | import android.util.DisplayMetrics;
5 |
6 | /**
7 | * describe:
8 | *
9 | * @author :鲁宇峰 on 2018/11/14 14:34
10 | * email:466708987@qq.com
11 | * github:https://github.com/Victory-Over
12 | */
13 | public class UIUtils {
14 |
15 | /**
16 | * 获取屏内容
17 | */
18 | public static DisplayMetrics getScreen(Context context) {
19 | DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
20 | return displayMetrics;
21 | }
22 |
23 | /**
24 | * dip转换px
25 | */
26 | public static int dp2px(Context context, int dip) {
27 | final float scale = context.getResources().getDisplayMetrics().density;
28 | return (int) (dip * scale + 0.5f);
29 | }
30 |
31 | /**
32 | * pxz转换dip
33 | */
34 | public static int px2dp(Context context, int px) {
35 | final float scale = context.getResources().getDisplayMetrics().density;
36 | return (int) (px / scale + 0.5f);
37 | }
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
15 |
16 |
19 |
20 |
24 |
25 |
33 |
34 |
35 |
42 |
43 |
50 |
51 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/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 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
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 Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/app/src/main/java/com/victory/chartview/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.victory.chartview;
2 |
3 | import android.annotation.SuppressLint;
4 | import android.os.Bundle;
5 | import android.os.Handler;
6 | import android.os.Message;
7 | import android.support.v7.app.AppCompatActivity;
8 | import android.view.View;
9 | import android.widget.Button;
10 | import android.widget.TextView;
11 |
12 | import java.util.ArrayList;
13 | import java.util.List;
14 | import java.util.Random;
15 |
16 | public class MainActivity extends AppCompatActivity {
17 |
18 | private ScrollChartView scrollChartView;
19 | private CircleIndicatorView circleIndicatorView;
20 | private TextView tvTime;
21 | private TextView tvData;
22 |
23 | private ScrollChartView.LineType lineType = ScrollChartView.LineType.ARC;
24 |
25 | @Override
26 | protected void onCreate(Bundle savedInstanceState) {
27 | super.onCreate(savedInstanceState);
28 | setContentView(R.layout.activity_main);
29 | initView();
30 | //加个延时设置数据,防止view未绘制完成的情况下就设置数据,正常业务不会出现这种情况,因为会有网络加载数据的过程
31 | handler.sendEmptyMessageDelayed(0, 1000);
32 | }
33 |
34 |
35 | private void initView() {
36 | scrollChartView = findViewById(R.id.scroll_chart_main);
37 | circleIndicatorView = findViewById(R.id.civ_main);
38 | tvTime = findViewById(R.id.tv_time);
39 | tvData = findViewById(R.id.tv_data);
40 |
41 | final Button btnLine = findViewById(R.id.btn_line);
42 | btnLine.setOnClickListener(new View.OnClickListener() {
43 | @Override
44 | public void onClick(View v) {
45 | if (scrollChartView.getLineType() == ScrollChartView.LineType.LINE) {
46 | scrollChartView.setLineType(ScrollChartView.LineType.ARC);
47 | scrollChartView.invalidateView();
48 | btnLine.setText("折线");
49 | } else {
50 | scrollChartView.setLineType(ScrollChartView.LineType.LINE);
51 | scrollChartView.invalidateView();
52 | btnLine.setText("曲线");
53 | }
54 | }
55 | });
56 | }
57 |
58 |
59 | @SuppressLint("HandlerLeak")
60 | private Handler handler = new Handler() {
61 | @Override
62 | public void handleMessage(Message msg) {
63 | initData();
64 | }
65 | };
66 |
67 | private void initData() {
68 | final List timeList = new ArrayList<>();
69 | for (int i = 0; i < 12; i++) {
70 | timeList.add(i + ":00");
71 | }
72 |
73 | final List dataList = new ArrayList<>();
74 | for (int i = 0; i < 12; i++) {
75 | dataList.add((double) new Random().nextInt(100));
76 | }
77 | scrollChartView.setData(timeList, dataList);
78 | scrollChartView.setOnScaleListener(new ScrollChartView.OnScaleListener() {
79 | @Override
80 | public void onScaleChanged(int position) {
81 | tvTime.setText(timeList.get(position));
82 | tvData.setText(dataList.get(position) + "");
83 | ScrollChartView.Point point = scrollChartView.getList().get(position);
84 | circleIndicatorView.setCircleY(point.y);
85 | }
86 | });
87 |
88 | //滚动到目标position
89 | scrollChartView.smoothScrollTo(dataList.size() - 1);
90 | }
91 |
92 | @Override
93 | protected void onDestroy() {
94 | super.onDestroy();
95 | handler.removeCallbacksAndMessages(null);
96 | }
97 | }
98 |
--------------------------------------------------------------------------------
/chartview/src/main/java/com/victory/chartview/CircleIndicatorView.java:
--------------------------------------------------------------------------------
1 | package com.victory.chartview;
2 |
3 | import android.content.Context;
4 | import android.graphics.Canvas;
5 | import android.graphics.LinearGradient;
6 | import android.graphics.Paint;
7 | import android.graphics.Path;
8 | import android.graphics.Shader;
9 | import android.support.annotation.Nullable;
10 | import android.support.v4.content.ContextCompat;
11 | import android.util.AttributeSet;
12 | import android.view.View;
13 |
14 |
15 | /**
16 | * describe:圆形指示器
17 | *
18 | * @author :鲁宇峰 on 2018/11/26 16:47
19 | * email:466708987@qq.com
20 | * github:https://github.com/Victory-Over
21 | */
22 | public class CircleIndicatorView extends View {
23 |
24 | /**
25 | * 画笔 线
26 | */
27 | private Paint mLinePaint;
28 |
29 | /**
30 | * 线 起始颜色
31 | */
32 | private int mLineStartColor;
33 |
34 | /**
35 | * 线 结束颜色
36 | */
37 | private int mLineEndColor;
38 |
39 | /**
40 | * 线 宽度
41 | */
42 | private int mLineWidth;
43 |
44 | /**
45 | * 画笔 圆形指示器
46 | */
47 | private Paint mCirclePaint;
48 |
49 | /**
50 | * 圆 半径
51 | */
52 | private int mCircleRadius;
53 |
54 | /**
55 | * 圆 颜色
56 | */
57 | private int mCircleColor;
58 |
59 | /**
60 | * 圆 背景颜色
61 | */
62 | private int mCircleBackColor;
63 |
64 | /**
65 | * 圆 当前坐标
66 | */
67 | private float mCircleY;
68 |
69 | public CircleIndicatorView(Context context) {
70 | this(context, null);
71 | }
72 |
73 | public CircleIndicatorView(Context context, @Nullable AttributeSet attrs) {
74 | this(context, attrs, 0);
75 | }
76 |
77 | public CircleIndicatorView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
78 | super(context, attrs, defStyleAttr);
79 |
80 | //初始化一些参数
81 | mLineStartColor = ContextCompat.getColor(context, R.color.colorIndicatorStart);
82 | mLineEndColor = ContextCompat.getColor(context, R.color.colorIndicatorEnd);
83 | mLineWidth = UIUtils.dp2px(context, 2);
84 |
85 | mCircleColor = ContextCompat.getColor(context, R.color.colorCircle);
86 | mCircleBackColor = ContextCompat.getColor(context, R.color.colorBaseBlack);
87 | mCircleRadius = UIUtils.dp2px(context, 5);
88 |
89 | mLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
90 | mLinePaint.setStyle(Paint.Style.STROKE);
91 | mLinePaint.setAntiAlias(true);
92 | mLinePaint.setStrokeWidth(mLineWidth);
93 | Shader shader = new LinearGradient(0, getHeight(), getWidth(), 0, mLineEndColor, mLineStartColor, Shader.TileMode.CLAMP);
94 | mLinePaint.setShader(shader);
95 |
96 | mCirclePaint = new Paint();
97 | mCirclePaint.setStyle(Paint.Style.FILL);
98 | mCirclePaint.setAntiAlias(true);
99 | }
100 |
101 |
102 | @Override
103 | protected void onDraw(Canvas canvas) {
104 | super.onDraw(canvas);
105 | if (mCircleY == 0) {
106 | return;
107 | }
108 | //画线
109 | drawLine(canvas);
110 | //画圆形指示器
111 | drawCircle(canvas);
112 | }
113 |
114 | private void drawCircle(Canvas canvas) {
115 | mCirclePaint.setColor(mCircleColor);
116 | canvas.drawCircle(getWidth() / 2, mCircleY, mCircleRadius, mCirclePaint);
117 | mCirclePaint.setColor(mCircleBackColor);
118 | canvas.drawCircle(getWidth() / 2, mCircleY, mCircleRadius / 2, mCirclePaint);
119 | }
120 |
121 | private void drawLine(Canvas canvas) {
122 | int left = getWidth() / 2;
123 | int bottom = getHeight();
124 | int top = 0;
125 | Path path = new Path();
126 | path.moveTo(left, top);
127 | path.lineTo(left, bottom);
128 | canvas.drawPath(path, mLinePaint);
129 | }
130 |
131 | public void setCircleY(float circleY) {
132 | mCircleY = circleY;
133 | invalidate();
134 | }
135 |
136 |
137 | public int getmLineStartColor() {
138 | return mLineStartColor;
139 | }
140 |
141 | public void setmLineStartColor(int mLineStartColor) {
142 | this.mLineStartColor = mLineStartColor;
143 | }
144 |
145 | public int getmLineEndColor() {
146 | return mLineEndColor;
147 | }
148 |
149 | public void setmLineEndColor(int mLineEndColor) {
150 | this.mLineEndColor = mLineEndColor;
151 | }
152 |
153 | public int getmLineWidth() {
154 | return mLineWidth;
155 | }
156 |
157 | public void setmLineWidth(int mLineWidth) {
158 | this.mLineWidth = mLineWidth;
159 | }
160 |
161 | public int getmCircleRadius() {
162 | return mCircleRadius;
163 | }
164 |
165 | public void setmCircleRadius(int mCircleRadius) {
166 | this.mCircleRadius = mCircleRadius;
167 | }
168 |
169 | public int getmCircleColor() {
170 | return mCircleColor;
171 | }
172 |
173 | public void setmCircleColor(int mCircleColor) {
174 | this.mCircleColor = mCircleColor;
175 | }
176 |
177 | public int getmCircleBackColor() {
178 | return mCircleBackColor;
179 | }
180 |
181 | public void setmCircleBackColor(int mCircleBackColor) {
182 | this.mCircleBackColor = mCircleBackColor;
183 | }
184 | }
185 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # SuperChartView
2 |
3 | #### 1、添加依赖和配置
4 |
5 | * 根目录build.gradle文件添加如下配置:
6 |
7 | ```Java
8 | allprojects {
9 | repositories {
10 | maven { url 'https://jitpack.io' }
11 | }
12 | }
13 | ```
14 |
15 | * APP目录build.gradle文件添加如下配置:
16 |
17 | ```Java
18 | dependencies {
19 | 'com.github.Victory-Over:SuperChartView:v1.0.0'
20 | }
21 | ```
22 |
23 | #### 2、效果展示
24 | 
25 |
26 | #### 3、核心代码
27 |
28 | * ScrollChartView 可滚动的自定义图表
29 |
30 | 每次滚动完成 计算滚动的位置,使indicate居中并回调当前位置的position 供外部使用
31 |
32 | ```Java
33 | /**
34 | * 调整indicate,使其居中。
35 | */
36 | private void adjustIndicate() {
37 | if (!mOverScroller.isFinished()) {
38 | mOverScroller.abortAnimation();
39 | }
40 |
41 | int position = computeSelectedPosition();
42 | int scrollX = getScrollByPosition(position);
43 | scrollX -= getScrollX();
44 | this.position = position;
45 |
46 | if (scrollX != 0) {
47 | mOverScroller.startScroll(getScrollX(), getScrollY(), scrollX, 0);
48 | invalidateView();
49 | }
50 |
51 | //滚动完毕回调
52 | onScaleChanged(position);
53 | }
54 | ```
55 |
56 | 根据传入的position 计算出每个indicate的位置,用于画图
57 |
58 | 例如position = 5 * indicate总宽度(indicate宽度+indicatePadding间隔*2) = 80
59 |
60 | 则该下标的位置为 left = 400 , right = left+indicate。
61 |
62 | ```Java
63 | /**
64 | * 计算indicate的位置
65 | */
66 | private void computeIndicateLoc(Rect outRect, int position) {
67 | if (outRect == null) {
68 | return;
69 | }
70 |
71 | int height = getHeight();
72 | int indicate = getIndicateWidth();
73 |
74 | int left = (indicate * position);
75 | int right = left + indicate;
76 | int top = getPaddingTop();
77 | int bottom = height - getPaddingBottom();
78 |
79 | if (isAlignTop()) {
80 | bottom -= mIndicateBottomPadding;
81 | } else {
82 | top += mIndicateBottomPadding;
83 | }
84 |
85 | outRect.set(left, top, right, bottom);
86 | }
87 | ```
88 |
89 | 上面两个方法是调整indicate 并计算出他的位置,得到这些参数后,就可以开始画图了
90 |
91 | ```Java
92 | /**
93 | * 绘制网格线
94 | */
95 | private void drawGridLine(Canvas canvas) {
96 | for (int i = 0; i < mList.size(); i++) {
97 | computeIndicateLoc(mIndicateLoc, i);
98 | int left = mIndicateLoc.left + mIndicatePadding;
99 | int right = mIndicateLoc.right - mIndicatePadding;
100 | int bottom = getHeight() - mShadowMarginHeight;
101 | canvas.drawRect(left, 0, right, bottom, mGridPaint);
102 | }
103 | }
104 | ```
105 |
106 | this.position == position 判断当前的position与将要绘制的position是否一致
107 |
108 | 是则改变其颜色并判断SDK版本是否大于21(支持画圆角的矩形)
109 |
110 | ```Java
111 | /**
112 | * 绘制指示标
113 | */
114 | private void drawIndicate(Canvas canvas, int position) {
115 | computeIndicateLoc(mIndicateLoc, position);
116 | int left = mIndicateLoc.left + mIndicatePadding;
117 | int right = mIndicateLoc.right - mIndicatePadding;
118 | int bottom = mIndicateLoc.bottom;
119 | int top = bottom - mIndicateHeight;
120 | if (this.position == position) {
121 | mIndicatePaint.setColor(mSelectedColor);
122 | } else {
123 | mIndicatePaint.setColor(mIndicateColor);
124 | }
125 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
126 | canvas.drawRoundRect(left, top, right, bottom, 5, 5, mIndicatePaint);
127 | } else {
128 | canvas.drawRect(left, top, right, bottom, mIndicatePaint);
129 | }
130 | }
131 | ```
132 |
133 | 同上,如果position一致则改变其大小和颜色
134 |
135 | ```Java
136 | /**
137 | * 绘制文字
138 | */
139 | private void drawText(Canvas canvas, int position, String text) {
140 | computeIndicateLoc(mIndicateLoc, position);
141 |
142 | if (this.position == position) {
143 | mTextPaint.setTextSize(mTextSelectedSize);
144 | mTextPaint.setColor(mSelectedColor);
145 | } else {
146 | mTextPaint.setTextSize(mTextSize);
147 | mTextPaint.setColor(mTextColor);
148 | }
149 |
150 | int x = (mIndicateLoc.left + mIndicateLoc.right) / 2;
151 | int y = mIndicateLoc.bottom + mIndicateBottomPadding - mTextBottomPadding;
152 |
153 | if (!isAlignTop()) {
154 | y = mIndicateLoc.top;
155 | mTextPaint.getTextBounds(text, 0, text.length(), mIndicateLoc);
156 | //增加一些偏移
157 | y += mIndicateLoc.top / 2;
158 | }
159 |
160 | canvas.drawText(text, x, y, mTextPaint);
161 | }
162 | ```
163 |
164 | 绘制Line,支持折线和曲线,后续还会支持柱状图,曲线的绘制方式可以去了解下贝塞尔曲线。
165 |
166 | ```Java
167 | /**
168 | * 绘制折线图
169 | */
170 | private void drawLine(Canvas canvas) {
171 | Path path = new Path();
172 | path.moveTo(mList.get(0).x, mList.get(0).y);
173 | for (int i = 1; i < mList.size(); i++) {
174 | path.lineTo(mList.get(i).x, mList.get(i).y);
175 | }
176 | canvas.drawPath(path, mLinePaint);
177 | }
178 |
179 |
180 | /**
181 | * 绘制曲线图
182 | */
183 | private void drawScrollLine(Canvas canvas) {
184 | Point pStart;
185 | Point pEnd;
186 | Path path = new Path();
187 | for (int i = 0; i < mList.size() - 1; i++) {
188 | pStart = mList.get(i);
189 | pEnd = mList.get(i + 1);
190 | Point point3 = new Point();
191 | Point point4 = new Point();
192 | float wd = (pStart.x + pEnd.x) / 2;
193 | point3.x = wd;
194 | point3.y = pStart.y;
195 | point4.x = wd;
196 | point4.y = pEnd.y;
197 | path.moveTo(pStart.x, pStart.y);
198 | path.cubicTo(point3.x, point3.y, point4.x, point4.y, pEnd.x, pEnd.y);
199 | canvas.drawPath(path, mLinePaint);
200 | }
201 | }
202 | ```
203 |
204 | 最后是绘制阴影,判断是折线还是曲线, 绘制阴影的方式跟绘制Line的方式差不多
205 |
206 | 重点是path.close() 如果连接Path起点和终点能形成一个闭合图形
207 |
208 | 则会将起点和终点连接起来形成一个闭合图形
209 |
210 | ```Java
211 | /**
212 | * 绘制阴影
213 | */
214 | private void drawShadow(Canvas canvas) {
215 | if (mLineType == LineType.ARC) {
216 | Point pStart;
217 | Point pEnd;
218 | Path path = new Path();
219 | for (int i = 0; i < mList.size() - 1; i++) {
220 | pStart = mList.get(i);
221 | pEnd = mList.get(i + 1);
222 | Point point3 = new Point();
223 | Point point4 = new Point();
224 | float wd = (pStart.x + pEnd.x) / 2;
225 | point3.x = wd;
226 | point3.y = pStart.y;
227 | point4.x = wd;
228 | point4.y = pEnd.y;
229 | path.moveTo(pStart.x, pStart.y);
230 | path.cubicTo(point3.x, point3.y, point4.x, point4.y, pEnd.x, pEnd.y);
231 | //减去文字和指示标的高度
232 | path.lineTo(pEnd.x, getHeight() - mShadowMarginHeight);
233 | path.lineTo(pStart.x, getHeight() - mShadowMarginHeight);
234 | }
235 | path.close();
236 | canvas.drawPath(path, mShadowPaint);
237 | } else {
238 | Path path = new Path();
239 | path.moveTo(mList.get(0).x, mList.get(0).y);
240 | for (int i = 1; i < mList.size(); i++) {
241 | path.lineTo(mList.get(i).x, mList.get(i).y);
242 | }
243 | //链接最后两个点
244 | int index = mList.size() - 1;
245 | path.lineTo(mList.get(index).x, getHeight() - mShadowMarginHeight);
246 | path.lineTo(mList.get(0).x, getHeight() - mShadowMarginHeight);
247 | path.close();
248 | canvas.drawPath(path, mShadowPaint);
249 | }
250 | }
251 | ```
252 |
253 | * CircleIndicatorView 圆形的指示器
254 |
255 | 这个自定义控件就比较简单,总共就三个重要的方法
256 |
257 | 1、画圆 2、画线 3、设置圆的Y坐标
258 |
259 | ```Java
260 | private void drawCircle(Canvas canvas) {
261 | mCirclePaint.setColor(mCircleColor);
262 | canvas.drawCircle(getWidth() / 2, mCircleY, mCircleRadius, mCirclePaint);
263 | mCirclePaint.setColor(mCircleBackColor);
264 | canvas.drawCircle(getWidth() / 2, mCircleY, mCircleRadius / 2, mCirclePaint);
265 | }
266 |
267 | private void drawLine(Canvas canvas) {
268 | int left = getWidth() / 2;
269 | int bottom = getHeight();
270 | int top = 0;
271 | Path path = new Path();
272 | path.moveTo(left, top);
273 | path.lineTo(left, bottom);
274 | canvas.drawPath(path, mLinePaint);
275 | }
276 |
277 | public void setCircleY(float circleY) {
278 | mCircleY = circleY;
279 | invalidate();
280 | }
281 | ```
282 |
283 | 结合之前的图表控件回调,获取到position 然后根据position获取到当前下标的坐标,赋值Y轴值给圆形控件
284 |
285 | ```Java
286 | scrollChartView.setOnScaleListener(new ScrollChartView.OnScaleListener() {
287 | @Override
288 | public void onScaleChanged(int position) {
289 | ScrollChartView.Point point = scrollChartView.getList().get(position);
290 | circleIndicatorView.setCircleY(point.y);
291 | }
292 | });
293 | ```
294 |
295 |
296 |
297 | #### License
298 |
299 | ```
300 | Copyright [2018] [Victory-Over]
301 |
302 | Licensed under the Apache License, Version 2.0 (the "License");
303 | you may not use this file except in compliance with the License.
304 | You may obtain a copy of the License at
305 |
306 | http://www.apache.org/licenses/LICENSE-2.0
307 |
308 | Unless required by applicable law or agreed to in writing, software
309 | distributed under the License is distributed on an "AS IS" BASIS,
310 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
311 | See the License for the specific language governing permissions and
312 | limitations under the License.
313 | ```
314 |
--------------------------------------------------------------------------------
/chartview/src/main/java/com/victory/chartview/ScrollChartView.java:
--------------------------------------------------------------------------------
1 | package com.victory.chartview;
2 |
3 | import android.content.Context;
4 | import android.graphics.Canvas;
5 | import android.graphics.Color;
6 | import android.graphics.LinearGradient;
7 | import android.graphics.Paint;
8 | import android.graphics.Path;
9 | import android.graphics.Rect;
10 | import android.graphics.Shader;
11 | import android.os.Build;
12 | import android.support.v4.content.ContextCompat;
13 | import android.util.AttributeSet;
14 | import android.view.Gravity;
15 | import android.view.MotionEvent;
16 | import android.view.VelocityTracker;
17 | import android.view.View;
18 | import android.view.ViewConfiguration;
19 | import android.widget.OverScroller;
20 |
21 | import java.util.ArrayList;
22 | import java.util.Collections;
23 | import java.util.List;
24 |
25 | /**
26 | * describe:
27 | *
28 | * @author :鲁宇峰 on 2018/11/14 14:34
29 | * email:466708987@qq.com
30 | * github:https://github.com/Victory-Over
31 | */
32 | public class ScrollChartView extends View {
33 |
34 | private int mBeginRange = 0;
35 |
36 | private int mEndRange;
37 |
38 | private int mInnerWidth;
39 |
40 | /**
41 | * 画笔 指示标
42 | */
43 | private Paint mIndicatePaint;
44 |
45 | /**
46 | * 指示标 颜色
47 | */
48 | private int mIndicateColor;
49 |
50 | /**
51 | * 指示标 宽度
52 | */
53 | private int mIndicateWidth;
54 |
55 |
56 | /**
57 | * 指示标 高度
58 | */
59 | private int mIndicateHeight;
60 |
61 | /**
62 | * 指示标 左右间隔
63 | */
64 | private int mIndicatePadding;
65 |
66 | /**
67 | * 指示器 间隔底部的距离
68 | */
69 | private int mIndicateBottomPadding;
70 |
71 | /**
72 | * 画笔 文字
73 | */
74 | private Paint mTextPaint;
75 |
76 | /**
77 | * 文字 默认颜色
78 | */
79 | private int mTextColor;
80 |
81 | /**
82 | * 文字 默认大小
83 | */
84 | private float mTextSize;
85 |
86 | /**
87 | * 文字 选中大小
88 | */
89 | private float mTextSelectedSize;
90 |
91 | /**
92 | * 文字 间隔底部的距离
93 | */
94 | private int mTextBottomPadding;
95 |
96 | /**
97 | * 画笔 线
98 | */
99 | private Paint mLinePaint;
100 |
101 | /**
102 | * 线 起始颜色
103 | */
104 | private int mLineStartColor;
105 |
106 | /**
107 | * 线 结束颜色
108 | */
109 | private int mLineEndColor;
110 |
111 |
112 | /**
113 | * 线 宽度
114 | */
115 | private int mLineWidth;
116 |
117 | /**
118 | * 线 类型
119 | */
120 | private LineType mLineType = LineType.ARC;
121 |
122 | /**
123 | * 阴影 画笔
124 | */
125 | private Paint mShadowPaint;
126 |
127 | /**
128 | * 阴影 渐变开始颜色
129 | */
130 | private int mShadowStartColor;
131 |
132 | /**
133 | * 阴影 渐变结束颜色
134 | */
135 | private int mShadowEndColor;
136 |
137 | /**
138 | * 网格线 画笔
139 | */
140 | private Paint mGridPaint;
141 |
142 | /**
143 | * 网格线 颜色
144 | */
145 | private int mGirdColor;
146 |
147 | /**
148 | * 网格线 宽度
149 | */
150 | private int mGridWith;
151 |
152 |
153 | /**
154 | * 底部文字和指示标的高度
155 | */
156 | private int mShadowMarginHeight;
157 |
158 | /**
159 | * 选中颜色
160 | */
161 | private int mSelectedColor;
162 |
163 | private int mLastMotionX;
164 |
165 | private boolean mIsDragged;
166 |
167 | private boolean mIsAutoAlign = true;
168 |
169 |
170 | /**
171 | * 滚动后选择监听
172 | */
173 | private OnScaleListener mListener;
174 |
175 | private int mGravity = Gravity.TOP;
176 |
177 | private Rect mIndicateLoc;
178 |
179 | /**
180 | * 滚动相关参数
181 | */
182 | private OverScroller mOverScroller;
183 | private VelocityTracker mVelocityTracker;
184 | private int mTouchSlop;
185 | private int mMinimumVelocity;
186 | private int mMaximumVelocity;
187 |
188 | /**
189 | * X轴坐标值
190 | */
191 | private List timeList;
192 | /**
193 | * Y轴坐标值
194 | */
195 | private List dataList;
196 | /**
197 | * 坐标集合
198 | */
199 | private List mList = new ArrayList<>();
200 | /**
201 | * Y轴坐标最大的数据
202 | */
203 | private double maxData;
204 |
205 | /**
206 | * 当前选中的下标
207 | */
208 | private int position;
209 |
210 | public ScrollChartView(Context context) {
211 | this(context, null);
212 | }
213 |
214 | public ScrollChartView(Context context, AttributeSet attrs) {
215 | this(context, attrs, 0);
216 | }
217 |
218 | public ScrollChartView(Context context, AttributeSet attrs, int defStyleAttr) {
219 | super(context, attrs, defStyleAttr);
220 |
221 | //初始化一些参数
222 | mSelectedColor = ContextCompat.getColor(context, R.color.colorSelected);
223 |
224 | mLineStartColor = ContextCompat.getColor(context, R.color.colorLineStart);
225 | mLineEndColor = ContextCompat.getColor(context, R.color.colorLineEnd);
226 | mLineWidth = UIUtils.dp2px(context, 2);
227 |
228 | mTextColor = ContextCompat.getColor(context, R.color.colorText);
229 | mTextSize = UIUtils.dp2px(context, 9);
230 | mTextSelectedSize = UIUtils.dp2px(context, 16);
231 | mTextBottomPadding = UIUtils.dp2px(getContext(), 1);
232 |
233 | mIndicateHeight = UIUtils.dp2px(context, 5);
234 | mIndicateWidth = UIUtils.dp2px(context, 2);
235 | mIndicateColor = Color.WHITE;
236 | mIndicatePadding = UIUtils.dp2px(getContext(), 25);
237 | mIndicateBottomPadding = UIUtils.dp2px(getContext(), 15);
238 |
239 | mShadowStartColor = ContextCompat.getColor(getContext(), R.color.colorShadowStart);
240 | mShadowEndColor = ContextCompat.getColor(getContext(), R.color.colorShadowEnd);
241 | mShadowMarginHeight = UIUtils.dp2px(getContext(), 30);
242 |
243 | mGirdColor = ContextCompat.getColor(context, R.color.colorGrid);
244 | mGridWith = UIUtils.dp2px(context, 1);
245 |
246 | initValue();
247 | }
248 |
249 | public void setData(List times, List dataList) {
250 | Double min = Collections.min(dataList);
251 | //如果数据里面有负数 则需要将每个数据都减去最大的负数 以防止负数出现
252 | if (min < 0) {
253 | this.dataList.clear();
254 | for (int i = 0; i < dataList.size(); i++) {
255 | this.dataList.add(dataList.get(i) - min);
256 | }
257 | } else {
258 | this.dataList = dataList;
259 | }
260 | this.timeList = times;
261 | maxData = Collections.max(this.dataList);
262 |
263 | mEndRange = times.size() - 1;
264 | initValue();
265 | getPointList();
266 | invalidate();
267 | }
268 |
269 | private void initValue() {
270 |
271 | mOverScroller = new OverScroller(getContext());
272 | setOverScrollMode(OVER_SCROLL_ALWAYS);
273 |
274 | final ViewConfiguration configuration = ViewConfiguration.get(getContext());
275 | mTouchSlop = configuration.getScaledTouchSlop();
276 | mMinimumVelocity = configuration.getScaledMinimumFlingVelocity();
277 | mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
278 |
279 |
280 | mIndicatePaint = new Paint();
281 | mIndicatePaint.setStyle(Paint.Style.FILL);
282 | mIndicatePaint.setAntiAlias(true);
283 |
284 | mLinePaint = new Paint();
285 | mLinePaint.setStyle(Paint.Style.STROKE);
286 | mLinePaint.setAntiAlias(true);
287 | mLinePaint.setStrokeWidth(mLineWidth);
288 | Shader lineShader = new LinearGradient(0, 0, getWidth(), getHeight(), mLineEndColor, mLineStartColor, Shader.TileMode.CLAMP);
289 | mLinePaint.setShader(lineShader);
290 |
291 |
292 | mTextPaint = new Paint();
293 | mTextPaint.setStyle(Paint.Style.FILL);
294 | mTextPaint.setTextAlign(Paint.Align.CENTER);
295 | mTextPaint.setAntiAlias(true);
296 | mInnerWidth = (mEndRange - mBeginRange) * getIndicateWidth();
297 |
298 | mShadowPaint = new Paint();
299 | mShadowPaint.setStyle(Paint.Style.FILL);
300 | mShadowPaint.setAntiAlias(true);
301 | Shader shader = new LinearGradient(getWidth() / 2, getHeight(), getWidth() / 2, 0, mShadowEndColor, mShadowStartColor, Shader.TileMode.MIRROR);
302 | mShadowPaint.setShader(shader);
303 |
304 | mGridPaint = new Paint();
305 | mGridPaint.setStyle(Paint.Style.FILL);
306 | mGridPaint.setAntiAlias(true);
307 | mGridPaint.setColor(mGirdColor);
308 | mGridPaint.setStrokeWidth(mGridWith);
309 |
310 | mIndicateLoc = new Rect();
311 | }
312 |
313 | @Override
314 | protected void onDraw(Canvas canvas) {
315 | if (timeList == null) {
316 | return;
317 | }
318 | int count = canvas.save();
319 |
320 | //画网格
321 | drawGridLine(canvas);
322 |
323 | //画线
324 | if (LineType.ARC == mLineType) {
325 | //曲线
326 | drawScrollLine(canvas);
327 | } else {
328 | //折线
329 | drawLine(canvas);
330 | }
331 | //阴影
332 | drawShadow(canvas);
333 | for (int value = mBeginRange, position = 0; value <= mEndRange; value++, position++) {
334 | drawIndicate(canvas, position);
335 | drawText(canvas, position, timeList.get(value));
336 | }
337 |
338 | canvas.restoreToCount(count);
339 | }
340 |
341 | /**
342 | * 绘制网格线
343 | *
344 | * @param canvas
345 | */
346 | private void drawGridLine(Canvas canvas) {
347 | for (int i = 0; i < mList.size(); i++) {
348 | computeIndicateLoc(mIndicateLoc, i);
349 | int left = mIndicateLoc.left + mIndicatePadding;
350 | int right = mIndicateLoc.right - mIndicatePadding;
351 | int bottom = getHeight() - mShadowMarginHeight;
352 | mGridPaint.setColor(mGirdColor);
353 | canvas.drawRect(left, 0, right, bottom, mGridPaint);
354 | }
355 | }
356 |
357 | /**
358 | * 绘制指示标
359 | */
360 | private void drawIndicate(Canvas canvas, int position) {
361 | computeIndicateLoc(mIndicateLoc, position);
362 | int left = mIndicateLoc.left + mIndicatePadding;
363 | int right = mIndicateLoc.right - mIndicatePadding;
364 | int bottom = mIndicateLoc.bottom;
365 | int top = bottom - mIndicateHeight;
366 | if (this.position == position) {
367 | mIndicatePaint.setColor(mSelectedColor);
368 | } else {
369 | mIndicatePaint.setColor(mIndicateColor);
370 | }
371 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
372 | canvas.drawRoundRect(left, top, right, bottom, 5, 5, mIndicatePaint);
373 | } else {
374 | canvas.drawRect(left, top, right, bottom, mIndicatePaint);
375 | }
376 | }
377 |
378 | /**
379 | * 绘制文字
380 | */
381 | private void drawText(Canvas canvas, int position, String text) {
382 | computeIndicateLoc(mIndicateLoc, position);
383 |
384 | if (this.position == position) {
385 | mTextPaint.setTextSize(mTextSelectedSize);
386 | mTextPaint.setColor(mSelectedColor);
387 | } else {
388 | mTextPaint.setTextSize(mTextSize);
389 | mTextPaint.setColor(mTextColor);
390 | }
391 |
392 | int x = (mIndicateLoc.left + mIndicateLoc.right) / 2;
393 | int y = mIndicateLoc.bottom + mIndicateBottomPadding - mTextBottomPadding;
394 |
395 | if (!isAlignTop()) {
396 | y = mIndicateLoc.top;
397 | mTextPaint.getTextBounds(text, 0, text.length(), mIndicateLoc);
398 | //增加一些偏移
399 | y += mIndicateLoc.top / 2;
400 | }
401 |
402 | canvas.drawText(text, x, y, mTextPaint);
403 | }
404 |
405 | /**
406 | * 绘制折线图
407 | */
408 | private void drawLine(Canvas canvas) {
409 | Path path = new Path();
410 | path.moveTo(mList.get(0).x, mList.get(0).y);
411 | for (int i = 1; i < mList.size(); i++) {
412 | path.lineTo(mList.get(i).x, mList.get(i).y);
413 | }
414 | canvas.drawPath(path, mLinePaint);
415 | }
416 |
417 | /**
418 | * 绘制曲线图
419 | */
420 | private void drawScrollLine(Canvas canvas) {
421 | Point pStart;
422 | Point pEnd;
423 | Path path = new Path();
424 | for (int i = 0; i < mList.size() - 1; i++) {
425 | pStart = mList.get(i);
426 | pEnd = mList.get(i + 1);
427 | Point point3 = new Point();
428 | Point point4 = new Point();
429 | float wd = (pStart.x + pEnd.x) / 2;
430 | point3.x = wd;
431 | point3.y = pStart.y;
432 | point4.x = wd;
433 | point4.y = pEnd.y;
434 | path.moveTo(pStart.x, pStart.y);
435 | path.cubicTo(point3.x, point3.y, point4.x, point4.y, pEnd.x, pEnd.y);
436 | canvas.drawPath(path, mLinePaint);
437 | }
438 | }
439 |
440 | /**
441 | * 绘制阴影
442 | */
443 | private void drawShadow(Canvas canvas) {
444 | if (mLineType == LineType.ARC) {
445 | Point pStart;
446 | Point pEnd;
447 | Path path = new Path();
448 | for (int i = 0; i < mList.size() - 1; i++) {
449 | pStart = mList.get(i);
450 | pEnd = mList.get(i + 1);
451 | Point point3 = new Point();
452 | Point point4 = new Point();
453 | float wd = (pStart.x + pEnd.x) / 2;
454 | point3.x = wd;
455 | point3.y = pStart.y;
456 | point4.x = wd;
457 | point4.y = pEnd.y;
458 | path.moveTo(pStart.x, pStart.y);
459 | path.cubicTo(point3.x, point3.y, point4.x, point4.y, pEnd.x, pEnd.y);
460 | //减去文字和指示标的高度
461 | path.lineTo(pEnd.x, getHeight() - mShadowMarginHeight);
462 | path.lineTo(pStart.x, getHeight() - mShadowMarginHeight);
463 | }
464 | path.close();
465 | canvas.drawPath(path, mShadowPaint);
466 | } else {
467 | Path path = new Path();
468 | path.moveTo(mList.get(0).x, mList.get(0).y);
469 | for (int i = 1; i < mList.size(); i++) {
470 | path.lineTo(mList.get(i).x, mList.get(i).y);
471 | }
472 | //链接最后两个点
473 | int index = mList.size() - 1;
474 | path.lineTo(mList.get(index).x, getHeight() - mShadowMarginHeight);
475 | path.lineTo(mList.get(0).x, getHeight() - mShadowMarginHeight);
476 | path.close();
477 | canvas.drawPath(path, mShadowPaint);
478 | }
479 | }
480 |
481 | /**
482 | * 获取每个数据源的坐标
483 | */
484 | private void getPointList() {
485 | mList.clear();
486 | for (int i = 0; i < dataList.size(); i++) {
487 | computeIndicateLoc(mIndicateLoc, i);
488 | int left = mIndicateLoc.left + mIndicatePadding + (mIndicateWidth / 2);
489 | // 获取view的高度 减去所以控件的高度 得到 图表的高度 8代表预留出来的边距
490 | int height = getHeight() - mShadowMarginHeight - UIUtils.dp2px(getContext(), 8) - mLineWidth;
491 | // 通过每个数据除以最大的数据 得到所占比
492 | double scale = dataList.get(i) / maxData;
493 | // 图表高度减数据高度 得到每个数据的坐标点
494 | int top = height - (int) (height * scale) + UIUtils.dp2px(getContext(), 8);
495 | Point point = new Point();
496 | point.x = left;
497 | point.y = top;
498 | mList.add(point);
499 | }
500 | }
501 |
502 | /**
503 | * 计算indicate的位置
504 | *
505 | * @param outRect
506 | * @param position
507 | */
508 | private void computeIndicateLoc(Rect outRect, int position) {
509 | if (outRect == null) {
510 | return;
511 | }
512 |
513 | int height = getHeight();
514 | int indicate = getIndicateWidth();
515 |
516 | int left = (indicate * position);
517 | int right = left + indicate;
518 | int top = getPaddingTop();
519 | int bottom = height - getPaddingBottom();
520 |
521 | if (isAlignTop()) {
522 | bottom -= mIndicateBottomPadding;
523 | } else {
524 | top += mIndicateBottomPadding;
525 | }
526 |
527 | outRect.set(left, top, right, bottom);
528 | }
529 |
530 |
531 | @Override
532 | public boolean onTouchEvent(MotionEvent event) {
533 | initVelocityTrackerIfNotExists();
534 |
535 | mVelocityTracker.addMovement(event);
536 | switch (event.getAction()) {
537 | case MotionEvent.ACTION_DOWN:
538 | //
539 | if (mIsDragged = !mOverScroller.isFinished()) {
540 | if (getParent() != null) {
541 | getParent().requestDisallowInterceptTouchEvent(true);
542 | }
543 | }
544 |
545 | if (!mOverScroller.isFinished()) {
546 | mOverScroller.abortAnimation();
547 | }
548 |
549 | mLastMotionX = (int) event.getX();
550 |
551 | return true;
552 |
553 | case MotionEvent.ACTION_MOVE:
554 |
555 | int curX = (int) event.getX();
556 | int deltaX = mLastMotionX - curX;
557 |
558 | if (!mIsDragged && Math.abs(deltaX) > mTouchSlop) {
559 | if (getParent() != null) {
560 | getParent().requestDisallowInterceptTouchEvent(true);
561 | }
562 |
563 | mIsDragged = true;
564 |
565 | if (deltaX > 0) {
566 | deltaX -= mTouchSlop;
567 | } else {
568 | deltaX += mTouchSlop;
569 | }
570 | }
571 |
572 | if (mIsDragged) {
573 | mLastMotionX = curX;
574 |
575 | if (getScrollX() <= 0 || getScrollX() >= getMaximumScroll()) {
576 | deltaX *= 0.7;
577 | }
578 |
579 |
580 | if (overScrollBy(deltaX, 0, getScrollX(), getScrollY(), getMaximumScroll(), 0, getWidth(), 0, true)) {
581 | mVelocityTracker.clear();
582 | }
583 |
584 | }
585 |
586 | break;
587 | case MotionEvent.ACTION_UP: {
588 | if (mIsDragged) {
589 | mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
590 | int initialVelocity = (int) mVelocityTracker.getXVelocity();
591 |
592 | if ((Math.abs(initialVelocity) > mMinimumVelocity)) {
593 | fling(-initialVelocity);
594 | } else {
595 | //alignCenter();
596 | sprintBack();
597 | }
598 | }
599 |
600 | mIsDragged = false;
601 | recycleVelocityTracker();
602 | break;
603 | }
604 | case MotionEvent.ACTION_CANCEL: {
605 |
606 | if (mIsDragged && mOverScroller.isFinished()) {
607 | sprintBack();
608 | }
609 |
610 | mIsDragged = false;
611 |
612 | recycleVelocityTracker();
613 | break;
614 | }
615 | default:
616 | break;
617 | }
618 |
619 | return true;
620 | }
621 |
622 |
623 | private void refreshValues() {
624 | mInnerWidth = (mEndRange - mBeginRange) * getIndicateWidth();
625 | invalidateView();
626 |
627 | }
628 |
629 | private int getIndicateWidth() {
630 | return mIndicateWidth + mIndicatePadding + mIndicatePadding;
631 | }
632 |
633 | /**
634 | * 获取最小滚动值。
635 | *
636 | * @return
637 | */
638 | private int getMinimumScroll() {
639 | return -(getWidth() - getIndicateWidth()) / 2;
640 | }
641 |
642 | /**
643 | * 获取最大滚动值。
644 | *
645 | * @return
646 | */
647 | private int getMaximumScroll() {
648 | return mInnerWidth + getMinimumScroll();
649 | }
650 |
651 | /**
652 | * 调整indicate,使其居中。
653 | */
654 | private void adjustIndicate() {
655 | if (!mOverScroller.isFinished()) {
656 | mOverScroller.abortAnimation();
657 | }
658 |
659 | int position = computeSelectedPosition();
660 | int scrollX = getScrollByPosition(position);
661 | scrollX -= getScrollX();
662 | this.position = position;
663 |
664 | if (scrollX != 0) {
665 | mOverScroller.startScroll(getScrollX(), getScrollY(), scrollX, 0);
666 | invalidateView();
667 | }
668 |
669 | //滚动完毕回调
670 | onScaleChanged(position);
671 | }
672 |
673 | public void fling(int velocityX) {
674 | mOverScroller.fling(getScrollX(), getScrollY(), velocityX, 0, getMinimumScroll(), getMaximumScroll(), 0, 0, getWidth() / 2, 0);
675 | invalidateView();
676 | }
677 |
678 | public void sprintBack() {
679 | mOverScroller.springBack(getScrollX(), getScrollY(), getMinimumScroll(), getMaximumScroll(), 0, 0);
680 | invalidateView();
681 | }
682 |
683 |
684 | public void setOnScaleListener(OnScaleListener listener) {
685 | if (listener != null) {
686 | mListener = listener;
687 | }
688 | }
689 |
690 | /**
691 | * 获取position的绝对滚动位置。
692 | *
693 | * @param position
694 | * @return
695 | */
696 | private int getScrollByPosition(int position) {
697 | computeIndicateLoc(mIndicateLoc, position);
698 | int scrollX = mIndicateLoc.left + getMinimumScroll();
699 | return scrollX;
700 | }
701 |
702 | /**
703 | * 计算当前已选择的位置
704 | *
705 | * @return
706 | */
707 | public int computeSelectedPosition() {
708 | int centerX = getScrollX() - getMinimumScroll() + getIndicateWidth() / 2;
709 | centerX = Math.max(0, Math.min(mInnerWidth, centerX));
710 | int position = centerX / getIndicateWidth();
711 | return position;
712 | }
713 |
714 | public void smoothScrollTo(int position) {
715 | if (position < 0 || mBeginRange + position > mEndRange) {
716 | return;
717 | }
718 |
719 | if (!mOverScroller.isFinished()) {
720 | mOverScroller.abortAnimation();
721 | }
722 |
723 | int scrollX = getScrollByPosition(position);
724 | mOverScroller.startScroll(getScrollX(), getScrollY(), scrollX - getScrollX(), 0);
725 | invalidateView();
726 | }
727 |
728 | public void smoothScrollToValue(int value) {
729 | int position = value - mBeginRange;
730 | smoothScrollTo(position);
731 | }
732 |
733 |
734 | /**
735 | * 滚动回调
736 | */
737 | private void onScaleChanged(int position) {
738 | if (mListener != null) {
739 | mListener.onScaleChanged(position);
740 | }
741 | }
742 |
743 |
744 | @Override
745 | protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
746 | if (!mOverScroller.isFinished()) {
747 | final int oldX = getScrollX();
748 | final int oldY = getScrollY();
749 | setScrollX(scrollX);
750 | onScrollChanged(scrollX, scrollY, oldX, oldY);
751 | if (clampedX) {
752 | //sprintBack();
753 | }
754 | } else {
755 | super.scrollTo(scrollX, scrollY);
756 | }
757 | }
758 |
759 | @Override
760 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
761 | super.onMeasure(widthMeasureSpec, heightMeasureSpec);
762 |
763 |
764 | }
765 |
766 | private boolean isAlignTop() {
767 | return (mGravity & Gravity.TOP) == Gravity.TOP;
768 | }
769 |
770 |
771 | public void setGravity(int gravity) {
772 | this.mGravity = gravity;
773 | invalidateView();
774 | }
775 |
776 | @Override
777 | public void computeScroll() {
778 | if (this.mOverScroller.computeScrollOffset()) {
779 | int oldX = getScrollX();
780 | int oldY = getScrollY();
781 | int x = this.mOverScroller.getCurrX();
782 | int y = this.mOverScroller.getCurrY();
783 | overScrollBy(x - oldX, y - oldY, oldX, oldY, getMaximumScroll(), 0, getWidth(), 0, false);
784 | invalidateView();
785 | } else if (!mIsDragged && mIsAutoAlign) {
786 | adjustIndicate();
787 | }
788 |
789 | }
790 |
791 | @Override
792 | protected int computeHorizontalScrollRange() {
793 | return getMaximumScroll();
794 | }
795 |
796 |
797 | public void invalidateView() {
798 | if (Build.VERSION.SDK_INT >= 16) {
799 | postInvalidateOnAnimation();
800 | } else {
801 | invalidate();
802 | }
803 | }
804 |
805 |
806 | private void initVelocityTrackerIfNotExists() {
807 | if (mVelocityTracker == null) {
808 | mVelocityTracker = VelocityTracker.obtain();
809 | }
810 | }
811 |
812 | private void recycleVelocityTracker() {
813 | if (mVelocityTracker != null) {
814 | mVelocityTracker.recycle();
815 | mVelocityTracker = null;
816 | }
817 | }
818 |
819 | public interface OnScaleListener {
820 | void onScaleChanged(int position);
821 |
822 | }
823 |
824 | public void setIndicateWidth(int indicateWidth) {
825 | this.mIndicateWidth = indicateWidth;
826 | refreshValues();
827 | }
828 |
829 | public void setIndicatePadding(int indicatePadding) {
830 | this.mIndicatePadding = indicatePadding;
831 | refreshValues();
832 | }
833 |
834 | public int getIndicateHeight() {
835 | return mIndicateHeight;
836 | }
837 |
838 | public void setIndicateHeight(int mIndicateHeight) {
839 | this.mIndicateHeight = mIndicateHeight;
840 | }
841 |
842 |
843 | public void setAutoAlign(boolean autoAlign) {
844 | this.mIsAutoAlign = autoAlign;
845 | refreshValues();
846 | }
847 |
848 |
849 | public boolean isAutoAlign() {
850 | return mIsAutoAlign;
851 | }
852 |
853 |
854 | /**
855 | * 枚举类型直线或者是弧线
856 | */
857 | public enum LineType {
858 | LINE, ARC
859 | }
860 |
861 | public void setLineType(LineType mLineType) {
862 | this.mLineType = mLineType;
863 | }
864 |
865 | public LineType getLineType() {
866 | return mLineType;
867 | }
868 |
869 | public class Point {
870 |
871 | public float x;
872 | public float y;
873 |
874 | public Point() {
875 |
876 | }
877 |
878 | public Point(float x, float y) {
879 | this.x = x;
880 | this.y = y;
881 | }
882 | }
883 |
884 | public List getList() {
885 | return mList;
886 | }
887 |
888 |
889 | public int getmIndicateColor() {
890 | return mIndicateColor;
891 | }
892 |
893 | public void setmIndicateColor(int mIndicateColor) {
894 | this.mIndicateColor = mIndicateColor;
895 | }
896 |
897 | public int getmIndicateWidth() {
898 | return mIndicateWidth;
899 | }
900 |
901 | public void setmIndicateWidth(int mIndicateWidth) {
902 | this.mIndicateWidth = mIndicateWidth;
903 | }
904 |
905 | public int getmIndicateHeight() {
906 | return mIndicateHeight;
907 | }
908 |
909 | public void setmIndicateHeight(int mIndicateHeight) {
910 | this.mIndicateHeight = mIndicateHeight;
911 | }
912 |
913 | public int getmIndicatePadding() {
914 | return mIndicatePadding;
915 | }
916 |
917 | public void setmIndicatePadding(int mIndicatePadding) {
918 | this.mIndicatePadding = mIndicatePadding;
919 | }
920 |
921 | public int getmIndicateBottomPadding() {
922 | return mIndicateBottomPadding;
923 | }
924 |
925 | public void setmIndicateBottomPadding(int mIndicateBottomPadding) {
926 | this.mIndicateBottomPadding = mIndicateBottomPadding;
927 | }
928 |
929 | public int getmTextColor() {
930 | return mTextColor;
931 | }
932 |
933 | public void setmTextColor(int mTextColor) {
934 | this.mTextColor = mTextColor;
935 | }
936 |
937 | public float getmTextSize() {
938 | return mTextSize;
939 | }
940 |
941 | public void setmTextSize(float mTextSize) {
942 | this.mTextSize = mTextSize;
943 | }
944 |
945 | public float getmTextSelectedSize() {
946 | return mTextSelectedSize;
947 | }
948 |
949 | public void setmTextSelectedSize(float mTextSelectedSize) {
950 | this.mTextSelectedSize = mTextSelectedSize;
951 | }
952 |
953 | public int getmTextBottomPadding() {
954 | return mTextBottomPadding;
955 | }
956 |
957 | public void setmTextBottomPadding(int mTextBottomPadding) {
958 | this.mTextBottomPadding = mTextBottomPadding;
959 | }
960 |
961 | public int getmLineStartColor() {
962 | return mLineStartColor;
963 | }
964 |
965 | public void setmLineStartColor(int mLineStartColor) {
966 | this.mLineStartColor = mLineStartColor;
967 | }
968 |
969 | public int getmLineEndColor() {
970 | return mLineEndColor;
971 | }
972 |
973 | public void setmLineEndColor(int mLineEndColor) {
974 | this.mLineEndColor = mLineEndColor;
975 | }
976 |
977 | public int getmLineWidth() {
978 | return mLineWidth;
979 | }
980 |
981 | public void setmLineWidth(int mLineWidth) {
982 | this.mLineWidth = mLineWidth;
983 | }
984 |
985 | public int getmShadowStartColor() {
986 | return mShadowStartColor;
987 | }
988 |
989 | public void setmShadowStartColor(int mShadowStartColor) {
990 | this.mShadowStartColor = mShadowStartColor;
991 | }
992 |
993 | public int getmShadowEndColor() {
994 | return mShadowEndColor;
995 | }
996 |
997 | public void setmShadowEndColor(int mShadowEndColor) {
998 | this.mShadowEndColor = mShadowEndColor;
999 | }
1000 |
1001 | public int getmGirdColor() {
1002 | return mGirdColor;
1003 | }
1004 |
1005 | public void setmGirdColor(int mGirdColor) {
1006 | this.mGirdColor = mGirdColor;
1007 | }
1008 |
1009 | public int getmGridWith() {
1010 | return mGridWith;
1011 | }
1012 |
1013 | public void setmGridWith(int mGridWith) {
1014 | this.mGridWith = mGridWith;
1015 | }
1016 |
1017 | public int getmShadowMarginHeight() {
1018 | return mShadowMarginHeight;
1019 | }
1020 |
1021 | public void setmShadowMarginHeight(int mShadowMarginHeight) {
1022 | this.mShadowMarginHeight = mShadowMarginHeight;
1023 | }
1024 |
1025 | public int getmSelectedColor() {
1026 | return mSelectedColor;
1027 | }
1028 |
1029 | public void setmSelectedColor(int mSelectedColor) {
1030 | this.mSelectedColor = mSelectedColor;
1031 | }
1032 |
1033 | public boolean ismIsAutoAlign() {
1034 | return mIsAutoAlign;
1035 | }
1036 |
1037 | public void setmIsAutoAlign(boolean mIsAutoAlign) {
1038 | this.mIsAutoAlign = mIsAutoAlign;
1039 | }
1040 |
1041 | public int getmGravity() {
1042 | return mGravity;
1043 | }
1044 |
1045 | public void setmGravity(int mGravity) {
1046 | this.mGravity = mGravity;
1047 | }
1048 | }
1049 |
--------------------------------------------------------------------------------