├── app ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── values │ │ │ │ ├── strings.xml │ │ │ │ ├── colors.xml │ │ │ │ ├── dimens.xml │ │ │ │ └── styles.xml │ │ │ ├── drawable-xhdpi │ │ │ │ ├── pic_1.jpg │ │ │ │ ├── pic_2.jpg │ │ │ │ ├── pic_3.jpg │ │ │ │ └── pic_4.jpg │ │ │ ├── drawable │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-hdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-mdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xxhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xxxhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── values-w820dp │ │ │ │ └── dimens.xml │ │ │ └── layout │ │ │ │ └── activity_main.xml │ │ ├── AndroidManifest.xml │ │ └── java │ │ │ └── com │ │ │ └── xyzlf │ │ │ └── viewpager │ │ │ └── demo │ │ │ └── MainActivity.java │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── xyzlf │ │ │ └── autoplay │ │ │ └── viewpager │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── com │ │ └── xyzlf │ │ └── autoplay │ │ └── viewpager │ │ └── ExampleInstrumentedTest.java ├── proguard-rules.pro └── build.gradle ├── library ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── values │ │ │ │ ├── strings.xml │ │ │ │ └── attrs.xml │ │ │ └── layout │ │ │ │ └── autoplay_banner_layout.xml │ │ ├── AndroidManifest.xml │ │ └── java │ │ │ └── com │ │ │ └── xyzlf │ │ │ └── autoplay │ │ │ └── viewpager │ │ │ ├── AutoPlayConstant.java │ │ │ ├── AutoPagerAdapter.java │ │ │ ├── AutoPlayViewPager.java │ │ │ ├── CustomerBanner.java │ │ │ └── AutoPagerIndicator.java │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── xyzlf │ │ │ └── autoplay │ │ │ └── viewpager │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── com │ │ └── xyzlf │ │ └── autoplay │ │ └── viewpager │ │ └── ExampleInstrumentedTest.java ├── proguard-rules.pro └── build.gradle ├── settings.gradle ├── autoplay_view.gif ├── autoplay_view2.gif ├── .gitignore ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew.bat ├── README.md └── gradlew /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /library/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':library' 2 | -------------------------------------------------------------------------------- /autoplay_view.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xyzlf/AutoPlayViewPager/HEAD/autoplay_view.gif -------------------------------------------------------------------------------- /autoplay_view2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xyzlf/AutoPlayViewPager/HEAD/autoplay_view2.gif -------------------------------------------------------------------------------- /library/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | Library 3 | 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea 5 | .DS_Store 6 | /build 7 | /captures 8 | /gradle.properties 9 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | AutoPlayViewPager 3 | 4 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xyzlf/AutoPlayViewPager/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/pic_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xyzlf/AutoPlayViewPager/HEAD/app/src/main/res/drawable-xhdpi/pic_1.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/pic_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xyzlf/AutoPlayViewPager/HEAD/app/src/main/res/drawable-xhdpi/pic_2.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/pic_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xyzlf/AutoPlayViewPager/HEAD/app/src/main/res/drawable-xhdpi/pic_3.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/pic_4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xyzlf/AutoPlayViewPager/HEAD/app/src/main/res/drawable-xhdpi/pic_4.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xyzlf/AutoPlayViewPager/HEAD/app/src/main/res/drawable/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xyzlf/AutoPlayViewPager/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xyzlf/AutoPlayViewPager/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xyzlf/AutoPlayViewPager/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xyzlf/AutoPlayViewPager/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xyzlf/AutoPlayViewPager/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /library/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Apr 17 15:27:22 GMT+08:00 2017 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-3.3-all.zip 7 | -------------------------------------------------------------------------------- /library/src/main/java/com/xyzlf/autoplay/viewpager/AutoPlayConstant.java: -------------------------------------------------------------------------------- 1 | package com.xyzlf.autoplay.viewpager; 2 | 3 | /** 4 | * Created by zhanglifeng on 2016/7/22. 5 | * Constant 6 | */ 7 | 8 | public class AutoPlayConstant { 9 | 10 | public static final int DATA_COUNT_RATIO = 1000; 11 | public static final int START_INDICATOR_RATIO = DATA_COUNT_RATIO / 2; 12 | 13 | } 14 | -------------------------------------------------------------------------------- /app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/com/xyzlf/autoplay/viewpager/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.xyzlf.autoplay.viewpager; 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 | } -------------------------------------------------------------------------------- /library/src/test/java/com/xyzlf/autoplay/viewpager/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.xyzlf.autoplay.viewpager; 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 | } -------------------------------------------------------------------------------- /library/src/main/res/values/attrs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in D:\Android\sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /library/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in D:\Android\sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /library/src/main/res/layout/autoplay_banner_layout.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 11 | 12 | 19 | 20 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/xyzlf/autoplay/viewpager/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.xyzlf.autoplay.viewpager; 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 | * Instrumentation 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.xyzlf.autoplay.viewpager", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /library/src/androidTest/java/com/xyzlf/autoplay/viewpager/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.xyzlf.autoplay.viewpager; 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 | * Instrumentation 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.xyzlf.autoplay.viewpager.test", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 24 5 | buildToolsVersion '25.0.0' 6 | defaultConfig { 7 | applicationId "com.xyzlf.autoplay.viewpager" 8 | minSdkVersion 11 9 | targetSdkVersion 24 10 | versionCode 1 11 | versionName "1.0" 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 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 25 | exclude group: 'com.android.support', module: 'support-annotations' 26 | }) 27 | compile 'com.android.support:appcompat-v7:24.0.0' 28 | testCompile 'junit:junit:4.12' 29 | 30 | // compile project (':library') 31 | 32 | //使用 autoplay viewpager 33 | compile ('com.xyzlf.autoplay.viewpager:viewpager:0.0.5') { 34 | exclude group: 'com.android.support', module: 'appcompat-v7' 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 13 | 14 | 21 | 22 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /library/src/main/java/com/xyzlf/autoplay/viewpager/AutoPagerAdapter.java: -------------------------------------------------------------------------------- 1 | package com.xyzlf.autoplay.viewpager; 2 | 3 | import android.support.v4.view.PagerAdapter; 4 | import android.view.LayoutInflater; 5 | import android.view.View; 6 | import android.view.ViewGroup; 7 | 8 | public abstract class AutoPagerAdapter extends PagerAdapter { 9 | 10 | @Override 11 | public Object instantiateItem(ViewGroup container, int position) { 12 | View view = getView(LayoutInflater.from(container.getContext()), position); 13 | container.addView(view); 14 | return view; 15 | } 16 | 17 | @Override 18 | public void destroyItem(ViewGroup container, int position, Object object) { 19 | container.removeView((View) object); 20 | } 21 | 22 | public int getPositionForIndicator(int position) { 23 | if (getDataCount() <= 0) { 24 | return 0; 25 | } 26 | return position % getDataCount(); 27 | } 28 | 29 | public abstract View getView(LayoutInflater layoutInflater, int position); 30 | 31 | public abstract int getDataCount(); 32 | 33 | @Override 34 | public boolean isViewFromObject(View view, Object o) { 35 | return view == o; 36 | } 37 | 38 | @Override 39 | public int getCount() { 40 | return getDataCount() > 1 ? getDataCount() * AutoPlayConstant.DATA_COUNT_RATIO : getDataCount(); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /library/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'com.novoda.bintray-release'//添加 3 | 4 | android { 5 | compileSdkVersion 24 6 | buildToolsVersion '25.0.0' 7 | 8 | defaultConfig { 9 | minSdkVersion 11 10 | targetSdkVersion 24 11 | versionCode 3 12 | versionName "1.2" 13 | 14 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 15 | 16 | } 17 | buildTypes { 18 | release { 19 | minifyEnabled false 20 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 21 | } 22 | } 23 | 24 | lintOptions { 25 | checkReleaseBuilds false 26 | // Or, if you prefer, you can continue to check for errors in release builds, 27 | // but continue the build even when errors are found: 28 | abortOnError false 29 | } 30 | } 31 | 32 | dependencies { 33 | compile fileTree(dir: 'libs', include: ['*.jar']) 34 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 35 | exclude group: 'com.android.support', module: 'support-annotations' 36 | }) 37 | compile 'com.android.support:appcompat-v7:24.0.0' 38 | testCompile 'junit:junit:4.12' 39 | } 40 | 41 | Properties properties = new Properties() 42 | properties.load(project.rootProject.file('local.properties').newDataInputStream()) 43 | 44 | String localBintrayUser = properties.getProperty("bintray.user") 45 | String localBintrayApikey = properties.getProperty("bintray.apikey") 46 | 47 | //推送指令 48 | //gradlew clean build bintrayUpload 49 | //添加 50 | publish { 51 | bintrayUser = localBintrayUser //bintray.com用户名 52 | bintrayKey = localBintrayApikey //bintray.com apikey 53 | dryRun = false 54 | userOrg = localBintrayUser 55 | groupId = 'com.'+ localBintrayUser +'.autoplay.viewpager'//jcenter上的路径 56 | artifactId = 'viewpager'//项目名称 57 | publishVersion = '0.0.5'//版本号 58 | desc = 'Auto play viewpager.' 59 | website = 'https://github.com/xyzlf/AutoPlayViewPager' 60 | } 61 | 62 | -------------------------------------------------------------------------------- /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/xyzlf/viewpager/demo/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.xyzlf.viewpager.demo; 2 | 3 | import android.app.Activity; 4 | import android.os.Bundle; 5 | import android.support.v4.content.ContextCompat; 6 | import android.view.LayoutInflater; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | import android.view.ViewTreeObserver; 10 | import android.view.Window; 11 | import android.widget.ImageView; 12 | 13 | import com.xyzlf.autoplay.viewpager.AutoPagerAdapter; 14 | import com.xyzlf.autoplay.viewpager.CustomerBanner; 15 | 16 | import java.util.ArrayList; 17 | import java.util.List; 18 | 19 | public class MainActivity extends Activity { 20 | 21 | @Override 22 | protected void onCreate(Bundle savedInstanceState) { 23 | super.onCreate(savedInstanceState); 24 | requestWindowFeature(Window.FEATURE_NO_TITLE); 25 | setContentView(R.layout.activity_main); 26 | 27 | initView(); 28 | } 29 | 30 | @Override 31 | protected void onResume() { 32 | super.onResume(); 33 | } 34 | 35 | private void initView() { 36 | List list = new ArrayList<>(); 37 | list.add(R.drawable.pic_1); 38 | list.add(R.drawable.pic_2); 39 | list.add(R.drawable.pic_3); 40 | list.add(R.drawable.pic_4); 41 | 42 | final CustomerBanner banner = (CustomerBanner) findViewById(R.id.banner); 43 | banner.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { 44 | @Override 45 | public boolean onPreDraw() { 46 | 47 | banner.getWidth(); 48 | return false; 49 | } 50 | }); 51 | banner.setAdapter(new AutoPlayPagerAdapter(list)); 52 | } 53 | 54 | public class AutoPlayPagerAdapter extends AutoPagerAdapter { 55 | 56 | private List list; 57 | 58 | public AutoPlayPagerAdapter(List list) { 59 | this.list = list; 60 | } 61 | 62 | @Override 63 | public View getView(LayoutInflater layoutInflater, int position) { 64 | ImageView imageView = new ImageView(getApplicationContext()); 65 | ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); 66 | imageView.setLayoutParams(params); 67 | int resId = list.get(getPositionForIndicator(position)); 68 | imageView.setImageDrawable(ContextCompat.getDrawable(MainActivity.this, resId)); 69 | return imageView; 70 | } 71 | 72 | @Override 73 | public int getDataCount() { 74 | return list == null ? 0 : list.size(); 75 | } 76 | } 77 | 78 | } 79 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AutoPlayViewPager 2 | 仿京东、淘宝,横向自动轮播图。Auto Play ViewPager。 3 | 4 | # 效果 5 | 6 | 7 | # Gradle 8 | //使用 autoplay viewpager 9 | compile ('com.xyzlf.autoplay.viewpager:viewpager:0.0.5') { 10 | exclude group: 'com.android.support', module: 'appcompat-v7' 11 | } 12 | 13 | # 使用方式 14 | 15 | 1、在xml中使用,如下: 16 | 17 | 18 | 22 | 23 | 24 | 33 | 34 | 35 | 2、代码中使用: 36 | 37 | List list = new ArrayList<>(); 38 | list.add(R.drawable.pic_1); 39 | list.add(R.drawable.pic_2); 40 | list.add(R.drawable.pic_3); 41 | list.add(R.drawable.pic_4); 42 | 43 | CustomerBanner banner = (CustomerBanner) findViewById(R.id.banner); 44 | banner.setAdapter(new AutoPlayPagerAdapter(list)); 45 | 46 | 47 | 3、Adapter需要继承 **AutoPagerAdapter**,完整Adapter代码如下: 48 | 49 | public class AutoPlayPagerAdapter extends AutoPagerAdapter { 50 | 51 | private List list; 52 | 53 | public AutoPlayPagerAdapter(List list) { 54 | this.list = list; 55 | } 56 | 57 | @Override 58 | public View getView(LayoutInflater layoutInflater, int position) { 59 | ImageView imageView = new ImageView(getApplicationContext()); 60 | ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); 61 | imageView.setLayoutParams(params); 62 | int resId = list.get(getPositionForIndicator(position)); 63 | imageView.setImageDrawable(ContextCompat.getDrawable(MainActivity.this, resId)); 64 | return imageView; 65 | } 66 | 67 | @Override 68 | public int getDataCount() { 69 | return list == null ? 0 : list.size(); 70 | } 71 | } 72 | 73 | 74 | 75 | 可以下载Demo看看,代码是最好的老师。 76 | # 关于我 77 | 有任何使用问题,可以给我发邮件: 78 | 79 | Author:张利峰 80 | 81 | E-mail:519578280@qq.com 82 | 83 | # License 84 | 85 | Copyright(c) 2016 xyzlf Open Source Project 86 | 87 | Licensed under the Apache License, Version 2.0 (the "License"); 88 | you may not use this file except in compliance with the License. 89 | You may obtain a copy of the License at 90 | 91 | http://www.apache.org/licenses/LICENSE-2.0 92 | 93 | Unless required by applicable law or agreed to in writing, software 94 | distributed under the License is distributed on an "AS IS" BASIS, 95 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 96 | See the License for the specific language governing permissions and 97 | limitations under the License. -------------------------------------------------------------------------------- /library/src/main/java/com/xyzlf/autoplay/viewpager/AutoPlayViewPager.java: -------------------------------------------------------------------------------- 1 | package com.xyzlf.autoplay.viewpager; 2 | 3 | import android.content.Context; 4 | import android.os.Handler; 5 | import android.support.v4.view.ViewPager; 6 | import android.util.AttributeSet; 7 | import android.view.MotionEvent; 8 | 9 | /** 10 | * Created by zhanglifeng on 2016/7/20 11 | * Auto Play ViewPager 12 | */ 13 | 14 | public class AutoPlayViewPager extends ViewPager { 15 | 16 | private static final int DEFAULT_INTERVAL = 4000; 17 | 18 | public enum PlayDirection { 19 | TO_LEFT, TO_RIGHT 20 | } 21 | 22 | public enum PlayRecycleMode { 23 | REPEAT_FROM_START, PLAY_BACK 24 | } 25 | 26 | private PlayDirection mDirection = PlayDirection.TO_RIGHT; 27 | private PlayRecycleMode mPlayRecycleMode = PlayRecycleMode.REPEAT_FROM_START; 28 | 29 | private int timeInterval = DEFAULT_INTERVAL; 30 | 31 | private boolean isPause = false; 32 | private boolean isPlaying = false; 33 | private boolean skipNext = false; 34 | 35 | private final Handler mHandler = new Handler(); 36 | private Runnable timerTask; 37 | 38 | public AutoPlayViewPager(Context context) { 39 | this(context, null); 40 | } 41 | 42 | public AutoPlayViewPager(Context context, AttributeSet attrs) { 43 | super(context, attrs); 44 | } 45 | 46 | public void setTimeInterval(int timeInterval) { 47 | this.timeInterval = timeInterval; 48 | } 49 | 50 | public void startPlay(int startItem) { 51 | if (isPlaying) { 52 | return; 53 | } 54 | isPause = false; 55 | isPlaying = true; 56 | setCurrentItem(startItem * AutoPlayConstant.START_INDICATOR_RATIO); 57 | if (null != timerTask) { 58 | mHandler.removeCallbacks(timerTask); 59 | } 60 | timerTask = new TimerRunnable(); 61 | mHandler.postDelayed(timerTask, timeInterval); 62 | } 63 | 64 | public void stopPlay() { 65 | isPause = true; 66 | isPlaying = false; 67 | 68 | if (null != timerTask) { 69 | mHandler.removeCallbacks(timerTask); 70 | } 71 | } 72 | 73 | @Override 74 | public boolean dispatchTouchEvent(MotionEvent ev) { 75 | int action = ev.getAction(); 76 | switch (action) { 77 | case MotionEvent.ACTION_DOWN: 78 | //pause play 79 | isPause = true; 80 | break; 81 | case MotionEvent.ACTION_CANCEL: 82 | case MotionEvent.ACTION_UP: 83 | //start play 84 | isPause = false; 85 | break; 86 | default: 87 | break; 88 | } 89 | return super.dispatchTouchEvent(ev); 90 | } 91 | 92 | @Override 93 | protected void onDetachedFromWindow() { 94 | super.onDetachedFromWindow(); 95 | 96 | stopPlay(); 97 | } 98 | 99 | private class TimerRunnable implements Runnable { 100 | 101 | @Override 102 | public void run() { 103 | if (!isPause) { 104 | // 轮播next 105 | playNextFrame(); 106 | } 107 | 108 | if (isPlaying) { 109 | mHandler.postDelayed(this, timeInterval); 110 | } 111 | } 112 | } 113 | 114 | private void playNextFrame() { 115 | if (skipNext) { 116 | skipNext = false; 117 | return; 118 | } 119 | int total = getAdapter() == null ? 0 : getAdapter().getCount(); 120 | int current = getCurrentItem(); 121 | if (mDirection == PlayDirection.TO_RIGHT) { 122 | if (current == total - 1) { 123 | if (mPlayRecycleMode == PlayRecycleMode.PLAY_BACK) { 124 | mDirection = PlayDirection.TO_LEFT; 125 | playNextFrame(); 126 | } else { 127 | setCurrentItem(0, false); 128 | } 129 | } else { 130 | setCurrentItem(getCurrentItem() + 1, true); 131 | } 132 | } else { 133 | if (current == 0) { 134 | if (mPlayRecycleMode == PlayRecycleMode.PLAY_BACK) { 135 | mDirection = PlayDirection.TO_RIGHT; 136 | playNextFrame(); 137 | } else { 138 | setCurrentItem(total - 1, false); 139 | } 140 | } else { 141 | setCurrentItem(getCurrentItem() - 1, true); 142 | } 143 | } 144 | } 145 | 146 | } 147 | -------------------------------------------------------------------------------- /library/src/main/java/com/xyzlf/autoplay/viewpager/CustomerBanner.java: -------------------------------------------------------------------------------- 1 | package com.xyzlf.autoplay.viewpager; 2 | 3 | import android.content.Context; 4 | import android.content.res.TypedArray; 5 | import android.support.v4.view.ViewPager; 6 | import android.util.AttributeSet; 7 | import android.view.LayoutInflater; 8 | import android.widget.RelativeLayout; 9 | 10 | /** 11 | * Created by zhanglifeng on 2016/7/21. 12 | * banner view 13 | */ 14 | 15 | public class CustomerBanner extends RelativeLayout implements ViewPager.OnPageChangeListener { 16 | 17 | private AutoPlayViewPager viewPager; 18 | private AutoPagerIndicator indicator; 19 | 20 | private int mDotSpan = 36; 21 | private float mDotRadius = 6f; 22 | 23 | private int mBottomMargin; 24 | 25 | private int mSelectedColor = 0xFFFFFFFF; 26 | private int mUnSelectedColor = 0x9FB4B2B2; 27 | 28 | public CustomerBanner(Context context) { 29 | this(context, null); 30 | } 31 | 32 | public CustomerBanner(Context context, AttributeSet attrs) { 33 | this(context, attrs, 0); 34 | } 35 | 36 | public CustomerBanner(Context context, AttributeSet attrs, int defStyleAttr) { 37 | super(context, attrs, defStyleAttr); 38 | 39 | TypedArray arr = context.obtainStyledAttributes(attrs, R.styleable.AutoPagerIndicator, 0, 0); 40 | if (arr != null) { 41 | // indicator 大小 42 | if (arr.hasValue(R.styleable.AutoPagerIndicator_indicator_radius)) { 43 | mDotRadius = arr.getDimension(R.styleable.AutoPagerIndicator_indicator_radius, mDotRadius); 44 | } 45 | 46 | // indicator 间距 47 | if (arr.hasValue(R.styleable.AutoPagerIndicator_indicator_span)) { 48 | mDotSpan = (int) arr.getDimension(R.styleable.AutoPagerIndicator_indicator_span, mDotSpan); 49 | } 50 | 51 | // indicator 选中颜色 52 | mSelectedColor = arr.getColor(R.styleable.AutoPagerIndicator_indicator_selected_color, mSelectedColor); 53 | // indicator unselected color 54 | mUnSelectedColor = arr.getColor(R.styleable.AutoPagerIndicator_indicator_unselected_color, mUnSelectedColor); 55 | 56 | //indicator margin bottom 57 | mBottomMargin = (int) arr.getDimension(R.styleable.AutoPagerIndicator_indicator_bottommargin, 0); 58 | 59 | arr.recycle(); 60 | } 61 | 62 | init(); 63 | } 64 | 65 | private void init() { 66 | LayoutInflater.from(getContext()).inflate(R.layout.autoplay_banner_layout, this); 67 | 68 | viewPager = (AutoPlayViewPager) findViewById(R.id.viewpager); 69 | viewPager.addOnPageChangeListener(this); 70 | 71 | indicator = (AutoPagerIndicator) findViewById(R.id.indicator); 72 | indicator.setAttrs(mDotRadius, mDotSpan, mSelectedColor, mUnSelectedColor); 73 | 74 | if (mBottomMargin != 0) { 75 | LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 76 | params.addRule(RelativeLayout.ALIGN_BOTTOM, viewPager.getId()); 77 | params.bottomMargin = mBottomMargin; 78 | indicator.setLayoutParams(params); 79 | } 80 | } 81 | 82 | public void setmDotClickHandler(AutoPagerIndicator.OnDotClickHandler mOnDotClickHandler) { 83 | if (null != indicator) { 84 | indicator.setmDotClickHandler(mOnDotClickHandler); 85 | } 86 | } 87 | 88 | public void setOnPageChangeListener(ViewPager.OnPageChangeListener onPageChangeListener) { 89 | if (viewPager != null) { 90 | viewPager.addOnPageChangeListener(onPageChangeListener); 91 | } 92 | } 93 | 94 | public void setAdapter(AutoPagerAdapter adapter) { 95 | this.setAdapter(adapter, true); 96 | } 97 | 98 | public void setAdapter(AutoPagerAdapter adapter, boolean isAutoPlay) { 99 | if (null == adapter) { 100 | return; 101 | } 102 | if (null == viewPager || null == indicator) { 103 | return; 104 | } 105 | setVisibility(VISIBLE); 106 | viewPager.setAdapter(adapter); 107 | int dataSize = adapter.getDataCount(); 108 | indicator.setAdapter(adapter); 109 | if (dataSize > 1 && isAutoPlay) { 110 | viewPager.startPlay(dataSize); 111 | } else { 112 | stopPlay(); 113 | } 114 | } 115 | 116 | public void stopPlay() { 117 | if (null != viewPager) { 118 | viewPager.stopPlay(); 119 | } 120 | } 121 | 122 | @Override 123 | public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 124 | 125 | } 126 | 127 | @Override 128 | public void onPageSelected(int position) { 129 | indicator.onPageSelected(position); 130 | } 131 | 132 | @Override 133 | public void onPageScrollStateChanged(int state) { 134 | 135 | } 136 | 137 | 138 | public int dip2px(float dpValue) { 139 | final float scale = getContext().getResources().getDisplayMetrics().density; 140 | return (int) (dpValue * scale + 0.5f); 141 | } 142 | 143 | } 144 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /library/src/main/java/com/xyzlf/autoplay/viewpager/AutoPagerIndicator.java: -------------------------------------------------------------------------------- 1 | package com.xyzlf.autoplay.viewpager; 2 | 3 | import android.content.Context; 4 | import android.content.res.TypedArray; 5 | import android.graphics.Canvas; 6 | import android.graphics.Paint; 7 | import android.util.AttributeSet; 8 | import android.view.Gravity; 9 | import android.view.View; 10 | import android.widget.LinearLayout; 11 | 12 | /** 13 | * Created by zhanglifeng on 2016/7/20. 14 | * Pager indicator 15 | */ 16 | 17 | public class AutoPagerIndicator extends LinearLayout { 18 | 19 | public interface OnDotClickHandler { 20 | void onDotClick(int index); 21 | } 22 | 23 | private int mCurrent = 0; 24 | 25 | private int mLittleDotSize = -2; 26 | private int mDotSpan = 36; 27 | private float mDotRadius = 6f; 28 | 29 | private int mSelectedColor = 0xFFFFFFFF; 30 | private int mUnSelectedColor = 0x9FB4B2B2; 31 | 32 | private OnDotClickHandler mOnDotClickHandler; 33 | 34 | private AutoPagerAdapter adapter; 35 | 36 | public AutoPagerIndicator(Context context) { 37 | super(context); 38 | } 39 | 40 | public AutoPagerIndicator(Context context, AttributeSet attrs) { 41 | this(context, attrs, 0); 42 | } 43 | 44 | public AutoPagerIndicator(Context context, AttributeSet attrs, int defStyleAttr) { 45 | super(context, attrs, defStyleAttr); 46 | 47 | setGravity(Gravity.CENTER); 48 | 49 | TypedArray arr = context.obtainStyledAttributes(attrs, R.styleable.AutoPagerIndicator, 0, 0); 50 | if (arr != null) { 51 | // indicator 大小 52 | if (arr.hasValue(R.styleable.AutoPagerIndicator_indicator_radius)) { 53 | mDotRadius = arr.getDimension(R.styleable.AutoPagerIndicator_indicator_radius, mDotRadius); 54 | } 55 | 56 | // indicator 间距 57 | if (arr.hasValue(R.styleable.AutoPagerIndicator_indicator_span)) { 58 | mDotSpan = (int) arr.getDimension(R.styleable.AutoPagerIndicator_indicator_span, mDotSpan); 59 | } 60 | 61 | // indicator 选中颜色 62 | mSelectedColor = arr.getColor(R.styleable.AutoPagerIndicator_indicator_selected_color, mSelectedColor); 63 | // indicator unselected color 64 | mUnSelectedColor = arr.getColor(R.styleable.AutoPagerIndicator_indicator_unselected_color, mUnSelectedColor); 65 | arr.recycle(); 66 | } 67 | 68 | mLittleDotSize = (int) (mDotSpan / 2 + mDotRadius * 2); 69 | } 70 | 71 | public final void setAttrs(float mDotRadius) { 72 | this.setAttrs(mDotRadius, mDotSpan); 73 | } 74 | 75 | public final void setAttrs(float mDotRadius, int mDotSpan) { 76 | this.setAttrs(mDotRadius, mDotSpan, mSelectedColor); 77 | } 78 | 79 | public final void setAttrs(float mDotRadius, int mDotSpan, int mSelectedColor) { 80 | this.setAttrs(mDotRadius, mDotSpan, mSelectedColor, mUnSelectedColor); 81 | } 82 | 83 | public final void setAttrs(float mDotRadius, int mDotSpan, int mSelectedColor, int mUnSelectedColor) { 84 | 85 | this.mDotRadius = mDotRadius; 86 | this.mDotSpan = mDotSpan; 87 | this.mSelectedColor = mSelectedColor; 88 | this.mUnSelectedColor = mUnSelectedColor; 89 | 90 | mLittleDotSize = (int) (mDotSpan / 2 + mDotRadius * 2); 91 | } 92 | 93 | public void setmDotClickHandler(OnDotClickHandler mOnDotClickHandler) { 94 | this.mOnDotClickHandler = mOnDotClickHandler; 95 | } 96 | 97 | public final void setAdapter(AutoPagerAdapter adapter) { 98 | if (null == adapter) { 99 | setVisibility(GONE); 100 | return; 101 | } 102 | this.adapter = adapter; 103 | generateDot(adapter.getDataCount()); 104 | } 105 | 106 | private void generateDot(int count) { 107 | if (count <= 1) { 108 | setVisibility(GONE); 109 | return; 110 | } 111 | setVisibility(VISIBLE); 112 | setOrientation(HORIZONTAL); 113 | 114 | removeAllViews(); 115 | 116 | for (int i = 0; i < count; i++) { 117 | LittleDot dot = new LittleDot(getContext(), i); 118 | if (i == 0) { 119 | dot.setColor(mSelectedColor); 120 | } else { 121 | dot.setColor(mUnSelectedColor); 122 | } 123 | dot.setLayoutParams(new LayoutParams(mLittleDotSize, (int) mDotRadius * 2)); 124 | dot.setClickable(true); 125 | dot.setOnClickListener(mDotClickHandler); 126 | addView(dot); 127 | } 128 | } 129 | 130 | public final void onPageSelected(int position) { 131 | if (null == adapter) { 132 | return; 133 | } 134 | int index = adapter.getPositionForIndicator(position); 135 | if (index >= getChildCount() || index < 0 || mCurrent == index) 136 | return; 137 | if (getChildAt(mCurrent) != null) { 138 | ((LittleDot) getChildAt(mCurrent)).setColor(mUnSelectedColor); 139 | } 140 | if (getChildAt(index) != null) { 141 | ((LittleDot) getChildAt(index)).setColor(mSelectedColor); 142 | } 143 | mCurrent = index; 144 | } 145 | 146 | private OnClickListener mDotClickHandler = new OnClickListener() { 147 | @Override 148 | public void onClick(View v) { 149 | if (v instanceof LittleDot && null != mOnDotClickHandler) { 150 | mOnDotClickHandler.onDotClick(((LittleDot) v).getIndex()); 151 | } 152 | } 153 | }; 154 | 155 | private class LittleDot extends View { 156 | 157 | private int mColor; 158 | private Paint mPaint; 159 | private int mIndex; 160 | 161 | public LittleDot(Context context, int index) { 162 | super(context); 163 | mPaint = new Paint(); 164 | mPaint.setAntiAlias(true); 165 | mIndex = index; 166 | } 167 | 168 | public int getIndex() { 169 | return mIndex; 170 | } 171 | 172 | public void setColor(int color) { 173 | if (color == mColor) { 174 | return; 175 | } 176 | mColor = color; 177 | invalidate(); 178 | } 179 | 180 | @Override 181 | protected void onDraw(Canvas canvas) { 182 | super.onDraw(canvas); 183 | mPaint.setColor(mColor); 184 | canvas.drawCircle(mLittleDotSize / 2, mDotRadius, mDotRadius, mPaint); 185 | } 186 | } 187 | 188 | } 189 | --------------------------------------------------------------------------------