├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── colors.xml
│ │ │ │ └── styles.xml
│ │ │ ├── mipmap-hdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-anydpi-v26
│ │ │ │ ├── ic_launcher.xml
│ │ │ │ └── ic_launcher_round.xml
│ │ │ ├── drawable-v24
│ │ │ │ └── ic_launcher_foreground.xml
│ │ │ ├── layout
│ │ │ │ └── activity_main.xml
│ │ │ └── drawable
│ │ │ │ └── ic_launcher_background.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── wzh
│ │ │ └── demo
│ │ │ └── MainActivity.java
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── wzh
│ │ │ └── demo
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── wzh
│ │ └── demo
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
├── build.gradle
└── app.iml
├── indicator
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ └── values
│ │ │ │ ├── strings.xml
│ │ │ │ └── attrs_universal_indicator.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── wzh
│ │ │ └── viewpager
│ │ │ └── indicator
│ │ │ └── UIndicator.java
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── wzh
│ │ │ └── viewpager
│ │ │ └── indicator
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── wzh
│ │ └── viewpager
│ │ └── indicator
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
├── build.gradle
└── indicator.iml
├── settings.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── local.properties
├── gradle.properties
├── Android-Universal-ViewPager-Indicator.iml
├── gradlew.bat
├── README.md
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/indicator/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':indicator'
2 |
--------------------------------------------------------------------------------
/indicator/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Indicator
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Android-Universal-ViewPager-Indicator
3 |
4 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wenzhihao123/Android-Universal-ViewPager-Indicator/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/indicator/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wenzhihao123/Android-Universal-ViewPager-Indicator/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wenzhihao123/Android-Universal-ViewPager-Indicator/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wenzhihao123/Android-Universal-ViewPager-Indicator/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wenzhihao123/Android-Universal-ViewPager-Indicator/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wenzhihao123/Android-Universal-ViewPager-Indicator/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wenzhihao123/Android-Universal-ViewPager-Indicator/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/wenzhihao123/Android-Universal-ViewPager-Indicator/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/wenzhihao123/Android-Universal-ViewPager-Indicator/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/wenzhihao123/Android-Universal-ViewPager-Indicator/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/wenzhihao123/Android-Universal-ViewPager-Indicator/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #008577
4 | #00574B
5 | #D81B60
6 |
7 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Sun May 19 11:20:34 CST 2019
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.10.1-all.zip
7 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/local.properties:
--------------------------------------------------------------------------------
1 | ## This file must *NOT* be checked into Version Control Systems,
2 | # as it contains information specific to your local configuration.
3 | #
4 | # Location of the SDK. This is only used by Gradle.
5 | # For customization when using a Version Control System, please read the
6 | # header note.
7 | #Sun May 19 11:20:35 CST 2019
8 | ndk.dir=/Users/wenzhihao/Library/Android/sdk/ndk-bundle
9 | sdk.dir=/Users/wenzhihao/Library/Android/sdk
10 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/test/java/com/wzh/demo/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.wzh.demo;
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() {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/indicator/src/test/java/com/wzh/viewpager/indicator/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.wzh.viewpager.indicator;
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() {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx1536m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 |
15 |
16 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/indicator/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/src/androidTest/java/com/wzh/demo/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.wzh.demo;
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() {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.wzh.demo", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/indicator/src/androidTest/java/com/wzh/viewpager/indicator/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.wzh.viewpager.indicator;
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() {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.wzh.viewpager.indicator.test", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/Android-Universal-ViewPager-Indicator.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 28
5 | defaultConfig {
6 | applicationId "com.wzh.demo"
7 | minSdkVersion 16
8 | targetSdkVersion 28
9 | versionCode 1
10 | versionName "1.0"
11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 | }
20 |
21 | dependencies {
22 | implementation fileTree(include: ['*.jar'], dir: 'libs')
23 | implementation 'com.android.support:appcompat-v7:28.0.0'
24 | implementation 'com.android.support.constraint:constraint-layout:1.1.3'
25 | testImplementation 'junit:junit:4.12'
26 | androidTestImplementation 'com.android.support.test:runner:1.0.2'
27 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
28 | implementation project(':indicator')
29 | }
30 |
--------------------------------------------------------------------------------
/indicator/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 28
5 |
6 |
7 |
8 | defaultConfig {
9 | minSdkVersion 16
10 | targetSdkVersion 28
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-optimize.txt'), 'proguard-rules.pro'
22 | }
23 | }
24 |
25 | }
26 |
27 | dependencies {
28 | implementation fileTree(dir: 'libs', include: ['*.jar'])
29 |
30 | implementation 'com.android.support:appcompat-v7:28.0.0'
31 | api 'com.android.support:support-v4:28.0.0'
32 | testImplementation 'junit:junit:4.12'
33 | androidTestImplementation 'com.android.support.test:runner:1.0.2'
34 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
35 |
36 | //gradle
37 | api ('com.alibaba.android:ultraviewpager:1.0.7.7@aar') {
38 | transitive = true
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/indicator/src/main/res/values/attrs_universal_indicator.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/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 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Android-Universal-ViewPager-Indicator
2 | Android 通用的ViewPager的指示器
3 |
4 | ### 效果图
5 | 
6 |
7 | ### 自定义属性
8 |
9 | | 属性 | 说明 |
10 | | ------ | ------ |
11 | | `normal_color` | 未选中的指示器颜色 |
12 | | `selected_color `| 选中的指示器颜色 |
13 | | `spacing` | 指示器每个item之间的间距 |
14 | |` orientation` | 设置指示器排列方向,枚举类型,有`horizontal`和`vertical` |
15 | |` style` | 枚举类型,有如下几种类型 |
16 |
17 | 样式说明
18 |
19 | | style | 说明 |
20 | | ------ | ------ |
21 | | `circle_circle` | 圆点指示器,对应图中第一种样式 |
22 | | `rect_rect` | 长条指示器,对应图中第二种样式 |
23 | | `circle_rect` | 指示器选中是长条,未选中是圆点,对应图中第三种样式 |
24 |
25 | 如果`style`设置为 `circle_circle`可设置以下属性:
26 |
27 | | 属性 | 说明 |
28 | | ------ | ------ |
29 | | `circle_circle_radius` | 都是圆点指示器半径大小 |
30 |
31 | 如果`style`设置为 `rect_rect`可设置以下属性:
32 |
33 | | 属性 | 说明 |
34 | | ------ | ------ |
35 | | `rect_rect_itemWidth` | 条形长度 |
36 | | `rect_rect_itemHeight` | 条形高度 |
37 | | `rect_rect_corner` | 条形圆角 |
38 |
39 | 如果`style`设置为 `circle_rect`可设置以下属性:
40 |
41 | | 属性 | 说明 |
42 | |--|--|
43 | | `circle_rect_radius` | 未选中圆点半径 |
44 | | `circle_rect_itemWidth` | 选中条形长度 |
45 | | `circle_rect_itemHeight` | 选中条形高度 |
46 | | `circle_rect_corner` | 选中条形设置圆角|
47 |
48 | ### 使用方法
49 | ```
50 |
54 |
55 |
60 |
61 |
76 |
77 |
78 | ```
79 | 在代码里需要关联上我们的ViewPager:
80 |
81 | ```
82 | //普通ViewPager使用
83 | ...
84 | ViewPager mViewPager1 = findViewById(R.id.viewPager1);
85 | UIndicator uIndicator1 = findViewById(R.id.indicator1);
86 | uIndicator1.attachToViewPager(mViewPager1);
87 |
88 | // UltraViewPager使用
89 | ...
90 | UltraViewPager mViewPager4 = findViewById(R.id.viewPager4);
91 | UIndicator uIndicator4 = findViewById(R.id.indicator4);
92 | uIndicator4.attachToViewPager(mViewPager4.getViewPager());
93 | ```
94 |
95 | * 简书: https://www.jianshu.com/p/42811a7c708c
96 |
--------------------------------------------------------------------------------
/app/src/main/java/com/wzh/demo/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.wzh.demo;
2 |
3 | import android.os.Bundle;
4 | import android.support.v4.view.PagerAdapter;
5 | import android.support.v4.view.ViewPager;
6 | import android.support.v7.app.AppCompatActivity;
7 | import android.view.View;
8 | import android.view.ViewGroup;
9 |
10 | import com.tmall.ultraviewpager.UltraViewPager;
11 | import com.wzh.viewpager.indicator.UIndicator;
12 |
13 | import java.util.ArrayList;
14 | import java.util.List;
15 | import java.util.Random;
16 |
17 | public class MainActivity extends AppCompatActivity {
18 | private ViewPager mViewPager1, mViewPager2, mViewPager3;
19 | private DemoPagerAdapter mAdapter1, mAdapter2, mAdapter3,mAdapter4;
20 |
21 | private UltraViewPager mViewPager4;
22 | private UIndicator uIndicator1, uIndicator2, uIndicator3,uIndicator4;
23 |
24 | @Override
25 | protected void onCreate(Bundle savedInstanceState) {
26 | super.onCreate(savedInstanceState);
27 | setContentView(R.layout.activity_main);
28 | mViewPager1 = findViewById(R.id.viewPager1);
29 | mViewPager2 = findViewById(R.id.viewPager2);
30 | mViewPager3 = findViewById(R.id.viewPager3);
31 | mViewPager4 = findViewById(R.id.ultra_viewpager);
32 |
33 | uIndicator1 = findViewById(R.id.indicator1);
34 | uIndicator2 = findViewById(R.id.indicator2);
35 | uIndicator3 = findViewById(R.id.indicator3);
36 | uIndicator4 = findViewById(R.id.indicator4);
37 |
38 |
39 | mAdapter1 = new DemoPagerAdapter(getList());
40 | mViewPager1.setAdapter(mAdapter1);
41 | uIndicator1.attachToViewPager(mViewPager1);
42 |
43 | mAdapter2 = new DemoPagerAdapter(getList());
44 | mViewPager2.setAdapter(mAdapter2);
45 | uIndicator2.attachToViewPager(mViewPager2);
46 |
47 | mAdapter3 = new DemoPagerAdapter(getList());
48 | mViewPager3.setAdapter(mAdapter3);
49 | uIndicator3.attachToViewPager(mViewPager3);
50 |
51 | mAdapter4 = new DemoPagerAdapter(getList());
52 | mViewPager4.setAdapter(mAdapter4);
53 | uIndicator4.attachToViewPager(mViewPager4.getViewPager());
54 | }
55 |
56 | public List getList() {
57 | List list = new ArrayList<>();
58 | Random rm = new Random();
59 | for (int i = 0; i < 5; i++) {
60 | int ranColor = 0xff000000 | rm.nextInt(0x00ffffff);
61 | list.add(generateView(ranColor));
62 | }
63 | return list;
64 | }
65 |
66 | public View generateView(int color) {
67 | View tv = new View(this);
68 | ViewPager.LayoutParams lp = new ViewPager.LayoutParams();
69 | lp.width = ViewPager.LayoutParams.MATCH_PARENT;
70 | lp.height = ViewPager.LayoutParams.MATCH_PARENT;
71 | tv.setBackgroundColor(color);
72 | tv.setLayoutParams(lp);
73 |
74 | return tv;
75 | }
76 |
77 | public class DemoPagerAdapter extends PagerAdapter {
78 | private List views;
79 |
80 | public DemoPagerAdapter(List views) {
81 | this.views = views;
82 | }
83 |
84 | @Override
85 | public int getCount() {
86 | return views.size();
87 | }
88 |
89 | @Override
90 | public boolean isViewFromObject(View view, Object object) {
91 | return view == object;
92 | }
93 |
94 | @Override
95 | public Object instantiateItem(ViewGroup container, int position) {
96 | View view = views.get(position);
97 | container.addView(view);
98 | return view;
99 | }
100 |
101 | @Override
102 | public void destroyItem(ViewGroup container, int position, Object object) {
103 | container.removeView(views.get(position));
104 | }
105 | }
106 | }
107 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
9 |
14 |
15 |
18 |
19 |
24 |
25 |
40 |
41 |
42 |
46 |
47 |
52 |
53 |
67 |
68 |
69 |
73 |
74 |
79 |
80 |
91 |
92 |
93 |
98 |
99 |
103 |
104 |
112 |
113 |
127 |
128 |
129 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/indicator/src/main/java/com/wzh/viewpager/indicator/UIndicator.java:
--------------------------------------------------------------------------------
1 | package com.wzh.viewpager.indicator;
2 |
3 | import android.content.Context;
4 | import android.content.res.ColorStateList;
5 | import android.content.res.TypedArray;
6 | import android.graphics.Canvas;
7 | import android.graphics.Color;
8 | import android.graphics.Paint;
9 | import android.graphics.RectF;
10 | import android.support.annotation.Nullable;
11 | import android.support.v4.view.PagerAdapter;
12 | import android.support.v4.view.ViewPager;
13 | import android.util.AttributeSet;
14 | import android.view.View;
15 |
16 | import com.tmall.ultraviewpager.UltraViewPagerAdapter;
17 |
18 | /**
19 | * @Author: wenzhihao
20 | * @Time: 2019/5/19
21 | * @Description: 通用ViewPager指示器
22 | */
23 | public class UIndicator extends View implements ViewPager.OnPageChangeListener {
24 |
25 | private static final String TAG = "UIndicator";
26 |
27 | //指示器样式一 选中未选中都是圆点
28 | public static final int STYLE_CIRCLR_CIRCLE = 0;
29 | //指示器样式二 选中未选中都是方形
30 | public static final int STYLE_RECT_RECT = 1;
31 | //指示器样式三 选中方形,未选中圆点
32 | public static final int STYLE_CIRCLR_RECT = 2;
33 |
34 | //横向排列
35 | public static final int HORIZONTAL = 0;
36 | //纵向排列
37 | public static final int VERTICAL = 1;
38 |
39 | private Context mContext;
40 |
41 | //指示器之间的间距
42 | private int spacing;
43 | //指示器排列方向
44 | private int orientation = HORIZONTAL;
45 | //选中与为选中的颜色
46 | private ColorStateList selectedColor, normalColor;
47 |
48 | //指示器样式,默认都是圆点
49 | private int mStyle = STYLE_CIRCLR_CIRCLE;
50 |
51 | //样式一 圆点半径大小
52 | private int circleCircleRadius = 0;
53 |
54 | //样式二 方形大小及圆角
55 | private int rectRectItemWidth = 0, rectRectItemHeight = 0, rectRectCorner = 0;
56 |
57 | //样式三 选中的方形大小及圆角
58 | private int circleRectItemWidth = 0, circleRectItemHeight = 0, circleRectCorner = 0;
59 | //样式三 未选中的圆点半径
60 | private int circleRectRadius = 0;
61 |
62 | //画笔
63 | private Paint normalPaint, selectedPaint;
64 |
65 | //指示器item的区域
66 | private RectF mRectF;
67 | //指示器大小
68 | private int width, height;
69 | //指示器item个数
70 | private int itemCount = 0;
71 | //当前选中的位置
72 | private int selection = 0;
73 |
74 | private ViewPager viewPager;
75 |
76 | public UIndicator(Context context) {
77 | this(context, null);
78 | }
79 |
80 | public UIndicator(Context context, @Nullable AttributeSet attrs) {
81 | this(context, attrs, 0);
82 | }
83 |
84 | public UIndicator(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
85 | super(context, attrs, defStyleAttr);
86 | mContext = context;
87 | init(attrs);
88 | intPaint();
89 | checkItemCount();
90 | }
91 |
92 | /**
93 | * 加载自定义属性
94 | */
95 | private void init(AttributeSet attrs) {
96 | // 加载自定义属性集合
97 | TypedArray ta = mContext.obtainStyledAttributes(attrs, R.styleable.Indicator);
98 | // 第二个参数是默认设置颜色
99 | selectedColor = ta.getColorStateList(R.styleable.Indicator_selected_color);
100 | normalColor = ta.getColorStateList(R.styleable.Indicator_normal_color);
101 | spacing = ta.getDimensionPixelSize(R.styleable.Indicator_spacing, dip2px(6));
102 | orientation = ta.getInt(R.styleable.Indicator_orientation, HORIZONTAL);
103 | mStyle = ta.getInt(R.styleable.Indicator_style, STYLE_CIRCLR_CIRCLE);
104 |
105 | circleCircleRadius = ta.getDimensionPixelSize(R.styleable.Indicator_circle_circle_radius, dip2px(3));
106 |
107 | rectRectCorner = ta.getDimensionPixelSize(R.styleable.Indicator_rect_rect_corner, 0);
108 | rectRectItemHeight = ta.getDimensionPixelSize(R.styleable.Indicator_rect_rect_itemHeight, dip2px(3));
109 | rectRectItemWidth = ta.getDimensionPixelSize(R.styleable.Indicator_rect_rect_itemWidth, dip2px(15));
110 |
111 | circleRectCorner = ta.getDimensionPixelSize(R.styleable.Indicator_circle_rect_corner, 0);
112 | circleRectRadius = ta.getDimensionPixelSize(R.styleable.Indicator_circle_rect_radius, dip2px(3));
113 | circleRectItemHeight = ta.getDimensionPixelSize(R.styleable.Indicator_circle_rect_itemHeight, dip2px(3));
114 | circleRectItemWidth = ta.getDimensionPixelSize(R.styleable.Indicator_circle_rect_itemWidth, dip2px(15));
115 |
116 | // 解析后释放资源
117 | ta.recycle();
118 | }
119 |
120 | private void intPaint() {
121 | normalPaint = new Paint();
122 | normalPaint.setStyle(Paint.Style.FILL);
123 | normalPaint.setAntiAlias(true);
124 | normalPaint.setColor(normalColor == null ? Color.GRAY : normalColor.getDefaultColor());
125 |
126 | selectedPaint = new Paint();
127 | selectedPaint.setStyle(Paint.Style.FILL);
128 | selectedPaint.setAntiAlias(true);
129 | selectedPaint.setColor(selectedColor == null ? Color.RED : selectedColor.getDefaultColor());
130 |
131 | mRectF = new RectF();
132 | }
133 |
134 |
135 | @Override
136 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
137 | super.onMeasure(widthMeasureSpec, heightMeasureSpec);
138 | int heightSize = MeasureSpec.getSize(heightMeasureSpec);
139 | int widthSize = MeasureSpec.getSize(widthMeasureSpec);
140 | switch (mStyle) {
141 | case STYLE_CIRCLR_CIRCLE:
142 | if (orientation == HORIZONTAL){
143 | width = 2 * circleCircleRadius * itemCount + (itemCount - 1) * spacing;
144 | height = Math.max(heightSize, 2 * circleCircleRadius);
145 | } else {
146 | height = 2 * circleCircleRadius * itemCount + (itemCount - 1) * spacing;
147 | width = Math.max(widthSize, 2 * circleCircleRadius);
148 | }
149 | break;
150 | case STYLE_RECT_RECT:
151 | if (orientation == HORIZONTAL){
152 | width = rectRectItemWidth * itemCount + (itemCount - 1) * spacing;
153 | height = Math.max(heightSize, rectRectItemHeight);
154 | } else {
155 | height = rectRectItemHeight * itemCount + (itemCount - 1) * spacing;
156 | width = Math.max(widthSize, rectRectItemWidth);
157 | }
158 | break;
159 | case STYLE_CIRCLR_RECT:
160 | if (orientation == HORIZONTAL){
161 | int normalItemWidth = circleRectRadius * 2;
162 | width = (itemCount - 1) * normalItemWidth + circleRectItemWidth + (itemCount - 1) * spacing;
163 | int tempHeight = Math.max(circleRectItemHeight, circleRectRadius * 2);
164 | height = Math.max(heightSize, tempHeight);
165 | } else {
166 | int normalItemHeight = circleRectRadius * 2;
167 | height = (itemCount - 1) * normalItemHeight + circleRectItemHeight + (itemCount - 1) * spacing;
168 | int tempWidth = Math.max(circleRectItemWidth, circleRectRadius * 2);
169 | width = Math.max(widthSize, tempWidth);
170 | }
171 |
172 | break;
173 | }
174 |
175 | setMeasuredDimension(width, height);
176 | }
177 |
178 | @Override
179 | protected void onDraw(Canvas canvas) {
180 | super.onDraw(canvas);
181 | if (orientation == HORIZONTAL) {
182 | switch (mStyle) {
183 |
184 | case STYLE_CIRCLR_CIRCLE:
185 | float cy = height / 2;
186 | for (int i = 0; i < itemCount; i++) {
187 | int cx = (i + 1) * circleCircleRadius + i * spacing;
188 | //全部绘制圆点,画笔的区别
189 | canvas.drawCircle(cx, cy, circleCircleRadius, i == selection ? selectedPaint : normalPaint);
190 | }
191 | break;
192 |
193 | case STYLE_RECT_RECT:
194 | for (int i = 0; i < itemCount; i++) {
195 | int left = i * rectRectItemWidth + i * spacing;
196 | mRectF.set(left, 0, left + rectRectItemWidth, rectRectItemHeight);
197 | //全部绘制圆角矩形,画笔的区别
198 | canvas.drawRoundRect(mRectF, rectRectCorner, rectRectCorner, i == selection ? selectedPaint : normalPaint);
199 | }
200 | break;
201 |
202 | case STYLE_CIRCLR_RECT:
203 | for (int i = 0; i < itemCount; i++) {
204 | int left = selection * (circleRectRadius * 2 + spacing);
205 | int top;
206 | if (selection == i) {
207 | //选中的绘制圆角矩形
208 | top = (height - circleRectItemHeight) / 2;
209 | mRectF.set(left, top, left + circleRectItemWidth, circleRectItemHeight + top);
210 | canvas.drawRoundRect(mRectF, circleRectCorner, circleRectCorner, selectedPaint);
211 | } else {
212 | //未选中的绘制圆点,距离需要判断position在选中的左边或者右边,从而确定cx
213 | top = (height - circleRectRadius * 2) / 2;
214 | int cx = 0;
215 | float cy1 = circleRectRadius + top;
216 | if (selection < i) {
217 | cx = (i - 1) * circleRectRadius * 2 + i * spacing + circleRectItemWidth + circleRectRadius;
218 | } else {
219 | cx = i * (circleRectRadius * 2) + i * spacing + circleRectRadius;
220 | }
221 | canvas.drawCircle(cx, cy1, circleRectRadius, normalPaint);
222 | }
223 |
224 | }
225 | break;
226 | }
227 | } else {
228 | switch (mStyle) {
229 |
230 | case STYLE_CIRCLR_CIRCLE:
231 | float cx = width / 2;
232 | for (int i = 0; i < itemCount; i++) {
233 | int cy = i * (circleCircleRadius * 2 + spacing)+ circleCircleRadius;
234 | //全部绘制圆点,画笔的区别
235 | canvas.drawCircle(cx, cy, circleCircleRadius, i == selection ? selectedPaint : normalPaint);
236 | }
237 | break;
238 |
239 | case STYLE_RECT_RECT:
240 | for (int i = 0; i < itemCount; i++) {
241 | int top = i * rectRectItemHeight + i * spacing;
242 | int left = (width - rectRectItemWidth) / 2;
243 | mRectF.set(left, top, left + rectRectItemWidth, top + rectRectItemHeight);
244 | //全部绘制圆角矩形,画笔的区别
245 | canvas.drawRoundRect(mRectF, rectRectCorner, rectRectCorner, i == selection ? selectedPaint : normalPaint);
246 | }
247 | break;
248 |
249 | case STYLE_CIRCLR_RECT:
250 | for (int i = 0; i < itemCount; i++) {
251 | if (selection == i) {
252 | int left = (width - circleRectItemWidth) / 2;
253 | //选中的绘制圆角矩形
254 | int top = selection * (circleRectRadius * 2 + spacing);
255 | mRectF.set(left, top, left + circleRectItemWidth, top + circleRectItemHeight);
256 | canvas.drawRoundRect(mRectF, circleRectCorner, circleRectCorner, selectedPaint);
257 | } else {
258 | //未选中的绘制圆点,距离需要判断position在选中的左边或者右边,从而确定cx
259 | int cx1 = (width - 2 * circleRectRadius) / 2 + circleRectRadius;
260 | float cy1 = 0;
261 | if (selection < i) {
262 | cy1 = (i - 1) * circleRectRadius * 2 + i * spacing + circleRectItemHeight + circleRectRadius;
263 | } else {
264 | cy1 = i * (circleRectRadius * 2) + i * spacing + circleRectRadius;
265 | }
266 | canvas.drawCircle(cx1, cy1, circleRectRadius, normalPaint);
267 | }
268 |
269 | }
270 | break;
271 | }
272 | }
273 | }
274 |
275 | /**
276 | * 关联ViewPager
277 | *
278 | * @param viewPager
279 | */
280 | public void attachToViewPager(ViewPager viewPager) {
281 | this.viewPager = viewPager;
282 | PagerAdapter pagerAdapter = viewPager.getAdapter();
283 | if (pagerAdapter != null) {
284 | //TODO 如果项目使用了阿里开源库,UltraViewPager,想要兼容需要用以下方式获取 itemCount,否则去除这个if条件
285 | if (pagerAdapter instanceof UltraViewPagerAdapter) {
286 | //从UltraViewPagerAdapter获取真实的个数
287 | itemCount = ((UltraViewPagerAdapter) pagerAdapter).getRealCount();
288 | } else {
289 | itemCount = pagerAdapter.getCount();
290 | }
291 | selection = viewPager.getCurrentItem() % itemCount;
292 | checkItemCount();
293 | }
294 |
295 | viewPager.addOnPageChangeListener(this);
296 | }
297 |
298 | /**
299 | * 设置选中的值,当ViewPager只有一个item不显示指示器
300 | */
301 | private void checkItemCount() {
302 | if (selection >= itemCount) {
303 | selection = itemCount - 1;
304 | }
305 | setVisibility((itemCount <= 1) ? GONE : VISIBLE);
306 | }
307 |
308 | @Override
309 | public void onPageSelected(int i) {
310 | if (viewPager != null) {
311 | PagerAdapter pagerAdapter = viewPager.getAdapter();
312 | if (pagerAdapter != null) {
313 | selection = viewPager.getCurrentItem() % itemCount;
314 | }
315 | }
316 | postInvalidate();
317 | }
318 |
319 | @Override
320 | public void onPageScrolled(int i, float v, int i1) {
321 |
322 | }
323 |
324 |
325 | @Override
326 | public void onPageScrollStateChanged(int i) {
327 |
328 | }
329 |
330 | /**
331 | * dp to px
332 | */
333 | public int dip2px(float dpValue) {
334 | final float scale = getContext().getResources().getDisplayMetrics().density;
335 | return (int) (dpValue * scale + 0.5f);
336 | }
337 | }
338 |
--------------------------------------------------------------------------------
/indicator/indicator.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 | generateDebugSources
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
--------------------------------------------------------------------------------
/app/app.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 | generateDebugSources
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 |
164 |
165 |
166 |
167 |
--------------------------------------------------------------------------------