├── Example ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── drawable-hdpi │ │ │ │ ├── image_a.png │ │ │ │ ├── image_b.png │ │ │ │ ├── ic_launcher.png │ │ │ │ ├── lincoln_color.png │ │ │ │ └── lincoln_black_white.png │ │ │ ├── drawable-mdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── drawable-xhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── drawable-xxhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── values │ │ │ │ ├── styles.xml │ │ │ │ ├── dimens.xml │ │ │ │ └── strings.xml │ │ │ ├── values-w820dp │ │ │ │ └── dimens.xml │ │ │ ├── layout │ │ │ │ └── main.xml │ │ │ └── menu │ │ │ │ └── main.xml │ │ ├── AndroidManifest.xml │ │ └── java │ │ │ └── com │ │ │ └── alihafizji │ │ │ └── example │ │ │ └── MainActivity.java │ └── androidTest │ │ └── java │ │ └── com │ │ └── alihafizji │ │ └── example │ │ └── ApplicationTest.java ├── build.gradle ├── proguard-rules.pro └── Example.iml ├── library ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── values │ │ │ │ ├── strings.xml │ │ │ │ └── attrs.xml │ │ │ ├── drawable-hdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── drawable-mdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── drawable-xhdpi │ │ │ │ └── ic_launcher.png │ │ │ └── drawable-xxhdpi │ │ │ │ └── ic_launcher.png │ │ ├── AndroidManifest.xml │ │ └── java │ │ │ └── com │ │ │ └── alihafizji │ │ │ └── splitimageview │ │ │ └── SplitImageView.java │ └── androidTest │ │ └── java │ │ └── com │ │ └── alihafizji │ │ └── splitimageview │ │ └── ApplicationTest.java ├── build.gradle ├── proguard-rules.pro └── library.iml ├── settings.gradle ├── split_image_view.gif ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── gradle.properties ├── gradlew.bat ├── README.md ├── gradlew └── LICENSE /Example/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /library/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':Example', ':library' 2 | -------------------------------------------------------------------------------- /split_image_view.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aliHafizji/SplitImageView/HEAD/split_image_view.gif -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aliHafizji/SplitImageView/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /library/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | SplitImageView 3 | 4 | -------------------------------------------------------------------------------- /Example/src/main/res/drawable-hdpi/image_a.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aliHafizji/SplitImageView/HEAD/Example/src/main/res/drawable-hdpi/image_a.png -------------------------------------------------------------------------------- /Example/src/main/res/drawable-hdpi/image_b.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aliHafizji/SplitImageView/HEAD/Example/src/main/res/drawable-hdpi/image_b.png -------------------------------------------------------------------------------- /Example/src/main/res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aliHafizji/SplitImageView/HEAD/Example/src/main/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /Example/src/main/res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aliHafizji/SplitImageView/HEAD/Example/src/main/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /Example/src/main/res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aliHafizji/SplitImageView/HEAD/Example/src/main/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /library/src/main/res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aliHafizji/SplitImageView/HEAD/library/src/main/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /library/src/main/res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aliHafizji/SplitImageView/HEAD/library/src/main/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /library/src/main/res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aliHafizji/SplitImageView/HEAD/library/src/main/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /Example/src/main/res/drawable-hdpi/lincoln_color.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aliHafizji/SplitImageView/HEAD/Example/src/main/res/drawable-hdpi/lincoln_color.png -------------------------------------------------------------------------------- /Example/src/main/res/drawable-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aliHafizji/SplitImageView/HEAD/Example/src/main/res/drawable-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /library/src/main/res/drawable-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aliHafizji/SplitImageView/HEAD/library/src/main/res/drawable-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /Example/src/main/res/drawable-hdpi/lincoln_black_white.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aliHafizji/SplitImageView/HEAD/Example/src/main/res/drawable-hdpi/lincoln_black_white.png -------------------------------------------------------------------------------- /Example/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Example/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Apr 10 15:27:10 PDT 2013 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=http\://services.gradle.org/distributions/gradle-1.12-all.zip 7 | -------------------------------------------------------------------------------- /library/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /Example/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /Example/src/main/res/layout/main.xml: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Example/src/androidTest/java/com/alihafizji/example/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.alihafizji.example; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | /local.properties 3 | /.idea/workspace.xml 4 | .DS_Store 5 | /build 6 | ======= 7 | # Built application files 8 | *.apk 9 | *.ap_ 10 | 11 | # Files for the Dalvik VM 12 | *.dex 13 | 14 | # Java class files 15 | *.class 16 | 17 | # Generated files 18 | bin/ 19 | gen/ 20 | 21 | # Gradle files 22 | .gradle/ 23 | build/ 24 | 25 | # Local configuration file (sdk path, etc) 26 | local.properties 27 | 28 | # Proguard folder generated by Eclipse 29 | proguard/ 30 | -------------------------------------------------------------------------------- /library/src/androidTest/java/com/alihafizji/splitimageview/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.alihafizji.splitimageview; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /library/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 19 5 | buildToolsVersion "20.0.0" 6 | 7 | defaultConfig { 8 | applicationId "com.alihafizji.splitimageview" 9 | minSdkVersion 14 10 | targetSdkVersion 19 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | runProguard false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | } 25 | -------------------------------------------------------------------------------- /Example/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 19 5 | buildToolsVersion "20.0.0" 6 | 7 | defaultConfig { 8 | applicationId "com.alihafizji.example" 9 | minSdkVersion 14 10 | targetSdkVersion 19 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | runProguard false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | compile project (':library') 25 | } 26 | -------------------------------------------------------------------------------- /Example/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 /Applications/Android Studio.app/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 /Applications/Android Studio.app/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 | -------------------------------------------------------------------------------- /Example/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 10 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Settings specified in this file will override any Gradle settings 5 | # configured through the IDE. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 14 | 15 | # When configured, Gradle will run in incubating parallel mode. 16 | # This option should only be used with decoupled projects. More details, visit 17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 18 | # org.gradle.parallel=true -------------------------------------------------------------------------------- /Example/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | SplitImageView 5 | Hello world! 6 | Settings 7 | 8 | Fit XY 9 | Fit Start 10 | Fit Center 11 | Fit End 12 | Center 13 | Center crop 14 | Center inside 15 | Add Color Filter 16 | Remove Color Filter 17 | Random mask percent 18 | Enable snap to bounds 19 | Disable snap to bounds 20 | Enable touch unveil 21 | Disable touch unveil 22 | Enable automatic animation 23 | Disable automatic animation 24 | 25 | -------------------------------------------------------------------------------- /library/src/main/res/values/attrs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /Example/src/main/res/menu/main.xml: -------------------------------------------------------------------------------- 1 | 4 | 8 | 9 | 13 | 14 | 18 | 19 | 23 | 24 | 28 | 29 | 33 | 34 | 38 | 39 | 43 | 44 | 48 | 49 | 53 | 54 | 58 | 59 | 63 | 64 | 68 | 69 | 73 | 74 | 78 | 79 | 83 | 84 | -------------------------------------------------------------------------------- /Example/src/main/java/com/alihafizji/example/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.alihafizji.example; 2 | 3 | import android.app.Activity; 4 | import android.graphics.Color; 5 | import android.graphics.PorterDuff; 6 | import android.os.Bundle; 7 | import android.view.Menu; 8 | import android.view.MenuItem; 9 | 10 | import com.alihafizji.splitimageview.SplitImageView; 11 | 12 | import java.util.Random; 13 | 14 | 15 | public class MainActivity extends Activity { 16 | 17 | private SplitImageView mSplitImageView; 18 | 19 | @Override 20 | protected void onCreate(Bundle savedInstanceState) { 21 | super.onCreate(savedInstanceState); 22 | setContentView(R.layout.main); 23 | 24 | mSplitImageView = (SplitImageView)findViewById(R.id.masked_image_view); 25 | mSplitImageView.setSplitPercent(100); 26 | mSplitImageView.setAutomaticAnimationDuration(2000); 27 | } 28 | 29 | @Override 30 | public boolean onCreateOptionsMenu(Menu menu) { 31 | getMenuInflater().inflate(R.menu.main, menu); 32 | return true; 33 | } 34 | 35 | @Override 36 | public boolean onOptionsItemSelected(MenuItem item) { 37 | int id = item.getItemId(); 38 | switch (id) { 39 | case R.id.fit_xy: 40 | mSplitImageView.setScaleType(SplitImageView.ScaleType.FIT_XY); 41 | break; 42 | case R.id.fit_start: 43 | mSplitImageView.setScaleType(SplitImageView.ScaleType.FIT_START); 44 | break; 45 | case R.id.fit_center: 46 | mSplitImageView.setScaleType(SplitImageView.ScaleType.FIT_CENTER); 47 | break; 48 | case R.id.fit_end: 49 | mSplitImageView.setScaleType(SplitImageView.ScaleType.FIT_END); 50 | break; 51 | case R.id.center: 52 | mSplitImageView.setScaleType(SplitImageView.ScaleType.CENTER); 53 | break; 54 | case R.id.center_crop: 55 | mSplitImageView.setScaleType(SplitImageView.ScaleType.CENTER_CROP); 56 | break; 57 | case R.id.center_inside: 58 | mSplitImageView.setScaleType(SplitImageView.ScaleType.CENTER_INSIDE); 59 | break; 60 | case R.id.add_color_filter: 61 | mSplitImageView.setColorFilter(Color.parseColor("#80FF0000"), PorterDuff.Mode.LIGHTEN); 62 | break; 63 | case R.id.remove_color_filter: 64 | mSplitImageView.clearColorFilter(); 65 | break; 66 | case R.id.apply_random_percent: 67 | Random random = new Random(); 68 | mSplitImageView.setSplitPercent(random.nextInt(100)); 69 | break; 70 | case R.id.enable_snap_mask_to_bounds: 71 | mSplitImageView.setSnapToBounds(true); 72 | break; 73 | case R.id.disable_snap_mask_to_bounds: 74 | mSplitImageView.setSnapToBounds(false); 75 | break; 76 | case R.id.enable_touch_unveil: 77 | mSplitImageView.setUnveilOnTouch(true); 78 | break; 79 | case R.id.disable_touch_unveil: 80 | mSplitImageView.setUnveilOnTouch(false); 81 | break; 82 | case R.id.enable_automatic_animation: 83 | mSplitImageView.setEnableAutomaticAnimation(true); 84 | break; 85 | case R.id.disable_automatic_animation: 86 | mSplitImageView.setEnableAutomaticAnimation(false); 87 | break; 88 | } 89 | return true; 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | SplitImageView 2 | ============== 3 | 4 | ![Preview of SplitImageView](https://raw.githubusercontent.com/aliHafizji/SplitImageView/master/split_image_view.gif) 5 | 6 | ## Introduction 7 | 8 | Ever wondered how the split image view is implemented in apps like kaliedoscope? Well today's your lucky day coz you found one that does just that. This is the android implementation of such a view. 9 | 10 | This view can be used to show a comparison between images or you could simply use it for its touch to unveil feature. 11 | 12 | ## Setup 13 | 14 | ### Method 1: Referencing library project 15 | 16 | SplitImageView is created as a standalone Android-Library project. You can easily include this project by referencing the library/ folder in your project using either eclipse or Android studio 17 | 18 | ### Method 2: Adding a remote referece 19 | 20 | The compiled version of the library is present on maven central. Using this method is easier than downloading the source and referencing it in your project. To add the library via a remote reference please follow these steps: 21 | 22 | * Open the build.gradle file for the module that will use this control. 23 | * At the root level add the following: 24 | 25 | ``` 26 | repositories { 27 | mavenCentral() 28 | } 29 | ``` 30 | 31 | * In the dependancy section add the following reference: 32 | 33 | ``` 34 | dependencies { 35 | compile 'com.alihafizji.splitimageview:library:+@aar' 36 | } 37 | ``` 38 | **Note: The dependacy section can have other dependencies** 39 | 40 | * Select the "Sync project with Gradle files" button in Android studio or simply run the assembleDebug gradle task via command line. 41 | 42 | ## Usage 43 | 44 | This view can be intialized using both code as well as xml. All the attributes needed for setup are defined in attrs.xml. 45 | 46 | To use it via xml, first add the namespace to the top element of the layout. 47 | 48 | ``` 49 | xmlns:app="http://schemas.android.com/apk/res-auto" 50 | ``` 51 | 52 | Add the view to the layout and set the respective height and width value to suit your needs. 53 | 54 | ``` 55 | 62 | ``` 63 | 64 | The `foregroundSrc` and `backgroundSrc` attributes represent the foreground and background images. The `splitPercent` attribute shows how much of the `backgroundSrc` will be unveiled. In the above example only 25% of the background image will be shown. 65 | 66 | SplitImageView support all the ScaleTypes that are supported by ImageView. You can simply use the public method `setScaleType(scaleType)` to change this variable. The scaleType is applied to both the foreground and background image. 67 | 68 | Automatic animation: This is a nifty little feature present in SplitImageView. You can turn it on by calling the public method `setEnableAutomaticAnimation(true)`. This will put the unveil animation on a loop, the preview in the first section shows this feature in action. The animation duration for this can also be changed using the public method `setAutomaticAnimationDuration(duration)`. 69 | 70 | There are lots of other useful things that the view can do. All the public APIs are well documented and should be easy to use. 71 | 72 | ## Developed by 73 | 74 | * Ali Hafizji 75 | 76 | [Follow me on twitter](https://twitter.com/Ali_hafizji). 77 | 78 | [![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-SplitImageView-blue.svg?style=flat)](https://android-arsenal.com/details/1/733) 79 | 80 | ## Inspired from 81 | 82 | This view has been inspired from Krzysztof Zablocki's implementation of the SplitImageView for iOS. Here is a [link](https://github.com/krzysztofzablocki/KZImageSplitView) to his repository. 83 | -------------------------------------------------------------------------------- /Example/Example.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 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 | -------------------------------------------------------------------------------- /library/library.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 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 | -------------------------------------------------------------------------------- /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 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 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 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. -------------------------------------------------------------------------------- /library/src/main/java/com/alihafizji/splitimageview/SplitImageView.java: -------------------------------------------------------------------------------- 1 | package com.alihafizji.splitimageview; 2 | 3 | import android.animation.Animator; 4 | import android.animation.AnimatorSet; 5 | import android.animation.ValueAnimator; 6 | import android.content.ContentResolver; 7 | import android.content.Context; 8 | import android.content.pm.PackageManager; 9 | import android.content.res.Resources; 10 | import android.content.res.TypedArray; 11 | import android.graphics.Bitmap; 12 | import android.graphics.Canvas; 13 | import android.graphics.Color; 14 | import android.graphics.ColorFilter; 15 | import android.graphics.Matrix; 16 | import android.graphics.Paint; 17 | import android.graphics.Path; 18 | import android.graphics.PorterDuff; 19 | import android.graphics.PorterDuffColorFilter; 20 | import android.graphics.RectF; 21 | import android.graphics.drawable.BitmapDrawable; 22 | import android.graphics.drawable.Drawable; 23 | import android.net.Uri; 24 | import android.os.Parcel; 25 | import android.os.Parcelable; 26 | import android.text.TextUtils; 27 | import android.util.AttributeSet; 28 | import android.util.Log; 29 | import android.view.GestureDetector; 30 | import android.view.MotionEvent; 31 | import android.view.View; 32 | import android.view.accessibility.AccessibilityEvent; 33 | import android.view.accessibility.AccessibilityNodeInfo; 34 | 35 | import java.io.FileNotFoundException; 36 | import java.io.IOException; 37 | import java.io.InputStream; 38 | import java.util.List; 39 | 40 | /** 41 | * Created by kauserali on 17/07/14. 42 | */ 43 | public class SplitImageView extends View { 44 | 45 | private static final String TAG = "SplitImageView"; 46 | private static final int DEFAULT_SPLIT_PERCENT = 50; 47 | private static final int SNAP_MARGIN_PERCENTAGE = 15; 48 | private static final int DEFAULT_AUTOMATIC_ANIMATION_DURATION = 600; 49 | 50 | // settable by the client 51 | private Uri mBackgroundUri, mForegroundUri; 52 | private int mBackgroundResId = 0, mForegroundResId = 0; 53 | private Matrix mMatrix; 54 | private ScaleType mScaleType; 55 | 56 | 57 | // these are applied to the drawable 58 | private ColorFilter mColorFilter; 59 | private int mAlpha = 255; 60 | private int mViewAlphaScale = 256; 61 | private boolean mColorMod = false; 62 | 63 | private Drawable mForegroundDrawable = null, mBackgroundDrawable = null; 64 | private int mMaxDrawableWidth; 65 | private int mMaxDrawableHeight; 66 | 67 | private Matrix mDrawMatrix = null; 68 | 69 | // Avoid allocations... 70 | private RectF mTempSrc = new RectF(); 71 | private RectF mTempDst = new RectF(); 72 | 73 | private boolean mCropToPadding, mHaveFrame, mUnveilOnTouch, mSnapToBounds; 74 | 75 | private int mSplitPercent; 76 | private Path mSplitDrawPath; 77 | private Paint mDebugDrawPaint; 78 | private boolean mEnableDebugDraw; 79 | private boolean mIsAnimating; 80 | 81 | private boolean mEnableAutomaticAnimation; 82 | private AnimatorSet mAutomaticAnimationAnimatorSet; 83 | private int mAutomaticAnimationDuration; 84 | private GestureDetector mGestureDetector; 85 | 86 | private static final ScaleType[] sScaleTypeArray = { 87 | ScaleType.MATRIX, 88 | ScaleType.FIT_XY, 89 | ScaleType.FIT_START, 90 | ScaleType.FIT_CENTER, 91 | ScaleType.FIT_END, 92 | ScaleType.CENTER, 93 | ScaleType.CENTER_CROP, 94 | ScaleType.CENTER_INSIDE 95 | }; 96 | 97 | public SplitImageView(Context context) { 98 | super(context); 99 | initImageView(); 100 | } 101 | 102 | public SplitImageView(Context context, AttributeSet attrs) { 103 | this(context, attrs, 0); 104 | } 105 | 106 | public SplitImageView(Context context, AttributeSet attrs, int defStyle) { 107 | super(context, attrs, defStyle); 108 | initImageView(); 109 | 110 | TypedArray a = context.obtainStyledAttributes(attrs, 111 | R.styleable.SplitImageView, defStyle, 0); 112 | 113 | mSplitPercent = a.getInt(R.styleable.SplitImageView_splitPercent, DEFAULT_SPLIT_PERCENT); 114 | if (mSplitPercent < 0 || mSplitPercent > 100) { 115 | throw new IllegalArgumentException("Split percent should be between 0 and 100 and not:" + mSplitPercent); 116 | } 117 | 118 | mUnveilOnTouch = a.getBoolean(R.styleable.SplitImageView_unveilOnTouch, true); 119 | mSnapToBounds = a.getBoolean(R.styleable.SplitImageView_snapToBounds, true); 120 | 121 | Drawable backgroundDrawable = a.getDrawable(R.styleable.SplitImageView_backgroundSrc); 122 | if (backgroundDrawable != null) { 123 | setBackgroundImageDrawable(backgroundDrawable); 124 | } 125 | 126 | Drawable foregroundDrawable = a.getDrawable(R.styleable.SplitImageView_foregroundSrc); 127 | if (foregroundDrawable != null) { 128 | setForegroundImageDrawable(foregroundDrawable); 129 | } 130 | 131 | int index = a.getInt(R.styleable.SplitImageView_scaleType, -1); 132 | if (index >= 0) { 133 | setScaleType(sScaleTypeArray[index]); 134 | } 135 | 136 | int tint = a.getInt(R.styleable.SplitImageView_tint, 0); 137 | if (tint != 0) { 138 | setColorFilter(tint); 139 | } 140 | 141 | int alpha = a.getInt(R.styleable.SplitImageView_drawableAlpha, 255); 142 | if (alpha != 255) { 143 | setAlpha(alpha); 144 | } 145 | 146 | mCropToPadding = a.getBoolean(R.styleable.SplitImageView_cropToPadding, false); 147 | mAutomaticAnimationDuration = DEFAULT_AUTOMATIC_ANIMATION_DURATION; 148 | a.recycle(); 149 | } 150 | 151 | public boolean isSnapToBounds() { 152 | return mSnapToBounds; 153 | } 154 | 155 | /** 156 | * This will enable or disable snap to bounds. 157 | * 158 | * Enabling this will snap the split between images to the bounds of the image. 159 | * 160 | * @param snapToBounds 161 | */ 162 | public void setSnapToBounds(boolean snapToBounds) { 163 | mSnapToBounds = snapToBounds; 164 | } 165 | 166 | /** 167 | * Controls how the image should be resized or moved to match the size 168 | * of this SplitImageView. 169 | * 170 | * @param scaleType The desired scaling mode. 171 | * 172 | */ 173 | public void setScaleType(ScaleType scaleType) { 174 | if (scaleType == null) { 175 | throw new NullPointerException(); 176 | } 177 | 178 | if (mScaleType != scaleType) { 179 | mScaleType = scaleType; 180 | configureBounds(); 181 | 182 | requestLayout(); 183 | invalidate(); 184 | } 185 | } 186 | 187 | /** 188 | * Return the current scale type in use by this SplitImageView. 189 | */ 190 | public ScaleType getScaleType() { 191 | return mScaleType; 192 | } 193 | 194 | /** Return the view's background drawable, or null if no drawable has been 195 | assigned. 196 | */ 197 | public Drawable getBackgroundDrawable() { 198 | return mBackgroundDrawable; 199 | } 200 | 201 | /** Return the view's foreground drawable, or null if no drawable has been 202 | assigned. 203 | */ 204 | public Drawable getForegroundDrawable() { 205 | return mForegroundDrawable; 206 | } 207 | 208 | /** 209 | * Sets a drawable as the background content of this SplitImageView. 210 | * 211 | * This is not the same as calling setBackground() on the view. This corresponds to the 212 | * resource that will be drawn below the foreground resource. This will be revealed when the 213 | * foreground resource is unveiled 214 | */ 215 | public void setBackgroundImageDrawable(Drawable drawable) { 216 | if (mBackgroundDrawable != drawable) { 217 | mBackgroundResId = 0; 218 | mBackgroundUri = null; 219 | 220 | final int oldWidth = mMaxDrawableWidth; 221 | final int oldHeight = mMaxDrawableHeight; 222 | 223 | updateDrawable(drawable, false); 224 | 225 | if (mMaxDrawableWidth > oldWidth || mMaxDrawableHeight > oldHeight) { 226 | requestLayout(); 227 | } 228 | invalidate(); 229 | 230 | createPathForSplitPercent(mSplitPercent); 231 | initGestureRecognizer(); 232 | 233 | if (mEnableAutomaticAnimation) { 234 | startAutomaticAnimation(); 235 | } 236 | } 237 | } 238 | 239 | /** 240 | * Sets a drawable as the foreground content of this SplitImageView. 241 | * 242 | * This is the image that will be used to unmask the background image 243 | */ 244 | public void setForegroundImageDrawable(Drawable drawable) { 245 | if (mForegroundDrawable != drawable) { 246 | mForegroundResId = 0; 247 | mForegroundUri = null; 248 | 249 | final int oldWidth = mMaxDrawableWidth; 250 | final int oldHeight = mMaxDrawableHeight; 251 | 252 | updateDrawable(drawable, true); 253 | 254 | if (mMaxDrawableWidth > oldWidth || mMaxDrawableHeight > oldHeight) { 255 | requestLayout(); 256 | } 257 | invalidate(); 258 | 259 | createPathForSplitPercent(mSplitPercent); 260 | initGestureRecognizer(); 261 | 262 | if (mEnableAutomaticAnimation) { 263 | startAutomaticAnimation(); 264 | } 265 | } 266 | } 267 | 268 | /** 269 | * Sets the background content of this SplitImageView to the specified Uri. 270 | * 271 | *

This does Bitmap reading and decoding on the UI 272 | * thread, which can cause a latency hiccup. If that's a concern, 273 | * consider using {@link #setBackgroundImageDrawable(android.graphics.drawable.Drawable)} or 274 | * {@link #setBackgroundImageBitmap(android.graphics.Bitmap)} and 275 | * {@link android.graphics.BitmapFactory} instead.

276 | * 277 | * @param uri The Uri of an image 278 | */ 279 | public void setBackgroundImageURI(Uri uri) { 280 | if (mBackgroundResId != 0 || 281 | (mBackgroundUri != uri && 282 | (uri == null || mBackgroundUri == null || !uri.equals(mBackgroundUri)))) { 283 | updateDrawable(null, false); 284 | mBackgroundResId = 0; 285 | mBackgroundUri = uri; 286 | 287 | final int oldWidth = mMaxDrawableWidth; 288 | final int oldHeight = mMaxDrawableHeight; 289 | 290 | resolveUri(false); 291 | 292 | if (mMaxDrawableWidth > oldWidth || mMaxDrawableHeight > oldHeight) { 293 | requestLayout(); 294 | } 295 | invalidate(); 296 | 297 | createPathForSplitPercent(mSplitPercent); 298 | initGestureRecognizer(); 299 | 300 | if (mEnableAutomaticAnimation) { 301 | startAutomaticAnimation(); 302 | } 303 | } 304 | } 305 | 306 | /** 307 | * Sets the foreground content of this SplitImageView to the specified Uri. 308 | * 309 | *

This does Bitmap reading and decoding on the UI 310 | * thread, which can cause a latency hiccup. If that's a concern, 311 | * consider using {@link #setForegroundImageDrawable(android.graphics.drawable.Drawable)} or 312 | * {@link #setForegroundImageBitmap(android.graphics.Bitmap)} and 313 | * {@link android.graphics.BitmapFactory} instead.

314 | * 315 | * @param uri The Uri of an image 316 | */ 317 | public void setForegroundImageURI(Uri uri) { 318 | if (mForegroundResId != 0 || 319 | (mForegroundUri != uri && 320 | (uri == null || mForegroundUri == null || !uri.equals(mForegroundUri)))) { 321 | updateDrawable(null, true); 322 | mForegroundResId = 0; 323 | mForegroundUri = uri; 324 | 325 | final int oldWidth = mMaxDrawableWidth; 326 | final int oldHeight = mMaxDrawableHeight; 327 | 328 | resolveUri(true); 329 | 330 | if (mMaxDrawableWidth > oldWidth || mMaxDrawableHeight > oldHeight) { 331 | requestLayout(); 332 | } 333 | invalidate(); 334 | 335 | createPathForSplitPercent(mSplitPercent); 336 | initGestureRecognizer(); 337 | 338 | if (mEnableAutomaticAnimation) { 339 | startAutomaticAnimation(); 340 | } 341 | } 342 | } 343 | 344 | /** 345 | * Sets a Bitmap as the background content of this SplitImageView. 346 | * 347 | * @param bm The bitmap to set 348 | */ 349 | public void setBackgroundImageBitmap(Bitmap bm) { 350 | // if this is used frequently, may handle bitmaps explicitly 351 | // to reduce the intermediate drawable object 352 | setBackgroundImageDrawable(new BitmapDrawable(getContext().getResources(), bm)); 353 | } 354 | 355 | /** 356 | * Sets a Bitmap as the foreground content of this SplitImageView. 357 | * 358 | * @param bm The bitmap to set 359 | */ 360 | public void setForegroundImageBitmap(Bitmap bm) { 361 | // if this is used frequently, may handle bitmaps explicitly 362 | // to reduce the intermediate drawable object 363 | setForegroundImageDrawable(new BitmapDrawable(getContext().getResources(), bm)); 364 | } 365 | 366 | /** Return the view's optional matrix. This is applied to the 367 | view's drawables when it is drawn. If there is no matrix, 368 | this method will return an identity matrix. 369 | Do not change this matrix in place but make a copy. 370 | If you want a different matrix applied to the drawable, 371 | be sure to call setImageMatrix(). 372 | */ 373 | public Matrix getImageMatrix() { 374 | if (mDrawMatrix == null) { 375 | return new Matrix(); 376 | } 377 | return mDrawMatrix; 378 | } 379 | 380 | public void setImageMatrix(Matrix matrix) { 381 | // collaps null and identity to just null 382 | if (matrix != null && matrix.isIdentity()) { 383 | matrix = null; 384 | } 385 | 386 | // don't invalidate unless we're actually changing our matrix 387 | if (matrix == null && !mMatrix.isIdentity() || 388 | matrix != null && !mMatrix.equals(matrix)) { 389 | mMatrix.set(matrix); 390 | configureBounds(); 391 | invalidate(); 392 | } 393 | } 394 | 395 | /** 396 | * Return whether this SplitImageView crops to padding. 397 | * 398 | * @return whether this SplitImageView crops to padding 399 | * 400 | */ 401 | public boolean getCropToPadding() { 402 | return mCropToPadding; 403 | } 404 | 405 | /** 406 | * Sets whether this SplitImageView will crop to padding. 407 | * 408 | * @param cropToPadding whether this SplitImageView will crop to padding 409 | * 410 | * @see #getCropToPadding() 411 | * 412 | */ 413 | public void setCropToPadding(boolean cropToPadding) { 414 | if (mCropToPadding != cropToPadding) { 415 | mCropToPadding = cropToPadding; 416 | requestLayout(); 417 | invalidate(); 418 | } 419 | } 420 | 421 | /** 422 | * Returns the active color filter for this SplitImageView. 423 | * 424 | * @return the active color filter for this SplitImageView 425 | * 426 | * @see #setColorFilter(android.graphics.ColorFilter) 427 | */ 428 | public ColorFilter getColorFilter() { 429 | return mColorFilter; 430 | } 431 | 432 | /** 433 | * Set a tinting option for the image. 434 | * 435 | * @param color Color tint to apply. 436 | * @param mode How to apply the color. The standard mode is 437 | * {@link android.graphics.PorterDuff.Mode#SRC_ATOP} 438 | * 439 | */ 440 | public final void setColorFilter(int color, PorterDuff.Mode mode) { 441 | setColorFilter(new PorterDuffColorFilter(color, mode)); 442 | } 443 | 444 | public final void setColorFilter(int color) { 445 | setColorFilter(color, PorterDuff.Mode.SRC_ATOP); 446 | } 447 | 448 | public void setColorFilter(ColorFilter cf) { 449 | if (mColorFilter != cf) { 450 | mColorFilter = cf; 451 | mColorMod = true; 452 | applyColorMod(); 453 | invalidate(); 454 | } 455 | } 456 | 457 | public final void clearColorFilter() { 458 | setColorFilter(null); 459 | } 460 | 461 | public boolean isDebugDrawEnabled() { 462 | return mEnableDebugDraw; 463 | } 464 | 465 | /** 466 | * This method will enable debug draw. It will draw a red border around the area that is 467 | * masked/split. 468 | * 469 | * @param enableDebugDraw 470 | */ 471 | public void setDebugDraw(boolean enableDebugDraw) { 472 | mEnableDebugDraw = enableDebugDraw; 473 | } 474 | 475 | public boolean isEnableAutomaticAnimation() { 476 | return mEnableAutomaticAnimation; 477 | } 478 | 479 | /** 480 | * Will enable automatic animation. For this to work the foreground drawable & background 481 | * drawable will have to be set. Also when enabled touch unveil will not work. 482 | * @param enable 483 | */ 484 | public void setEnableAutomaticAnimation(boolean enable) { 485 | mEnableAutomaticAnimation = enable; 486 | initGestureRecognizer(); 487 | if (mEnableAutomaticAnimation) { 488 | startAutomaticAnimation(); 489 | } else { 490 | stopAutomaticAnimation(); 491 | } 492 | } 493 | 494 | /** 495 | * This method will set the percentage of split that is applied to the Images. 496 | * Lesser the value more the amount that is reveled. 497 | * By default this is 50%. 498 | * 499 | * @param percent 500 | */ 501 | public void setSplitPercent(int percent) { 502 | if (percent >= 0 && percent <= 100) { 503 | mSplitPercent = percent; 504 | createPathForSplitPercent(mSplitPercent); 505 | } else { 506 | Log.e(TAG, "Split percentage should be between 0 and 100."); 507 | } 508 | } 509 | 510 | /** 511 | * Returns the alpha that will be applied to the drawables of this SplitImageView. 512 | * 513 | * @return the alpha that will be applied to the drawable of this SplitImageView 514 | * 515 | * @see #setImageAlpha(int) 516 | */ 517 | public int getImageAlpha() { 518 | return mAlpha; 519 | } 520 | 521 | /** 522 | * Sets the alpha value that should be applied to the image. 523 | * 524 | * @param alpha the alpha value that should be applied to the image 525 | * 526 | * @see #getImageAlpha() 527 | */ 528 | public void setImageAlpha(int alpha) { 529 | setAlpha(alpha); 530 | } 531 | 532 | public boolean unveilOnTouch() { 533 | return mUnveilOnTouch; 534 | } 535 | 536 | /** 537 | * Enables the touch to unveil feature 538 | * @param unveilOnTouch 539 | */ 540 | public void setUnveilOnTouch(boolean unveilOnTouch) { 541 | mUnveilOnTouch = unveilOnTouch; 542 | initGestureRecognizer(); 543 | } 544 | 545 | public int getAutomaticAnimationDuration() { 546 | return mAutomaticAnimationDuration; 547 | } 548 | 549 | /** 550 | * Set the duration of the automatic animation. 551 | * If the automatic animation has already started this won't take effect. 552 | * Call this before calling @setEnableAutomaticAnimation 553 | * @param automaticAnimationDuration 554 | */ 555 | public void setAutomaticAnimationDuration(int automaticAnimationDuration) { 556 | mAutomaticAnimationDuration = automaticAnimationDuration; 557 | } 558 | 559 | /** 560 | * Sets the alpha value that should be applied to the image. 561 | * 562 | * @param alpha the alpha value that should be applied to the image 563 | * 564 | * @deprecated use #setImageAlpha(int) instead 565 | */ 566 | @Deprecated 567 | public void setAlpha(int alpha) { 568 | alpha &= 0xFF; // keep it legal 569 | if (mAlpha != alpha) { 570 | mAlpha = alpha; 571 | mColorMod = true; 572 | applyColorMod(); 573 | invalidate(); 574 | } 575 | } 576 | 577 | @Override 578 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { 579 | super.onSizeChanged(w, h, oldw, oldh); 580 | mHaveFrame = true; 581 | configureBounds(); 582 | } 583 | 584 | @Override 585 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 586 | resolveUris(); 587 | int w; 588 | int h; 589 | 590 | if (mForegroundDrawable == null && mBackgroundDrawable == null) { 591 | // If no drawable, its intrinsic size is 0. 592 | mMaxDrawableWidth = -1; 593 | mMaxDrawableHeight = -1; 594 | w = h = 0; 595 | } else { 596 | w = mMaxDrawableWidth; 597 | h = mMaxDrawableHeight; 598 | if (w <= 0) w = 1; 599 | if (h <= 0) h = 1; 600 | } 601 | 602 | int pleft = getPaddingLeft(); 603 | int pright = getPaddingRight(); 604 | int ptop = getPaddingTop(); 605 | int pbottom = getPaddingBottom(); 606 | 607 | int widthSize; 608 | int heightSize; 609 | 610 | w += pleft + pright; 611 | h += ptop + pbottom; 612 | 613 | w = Math.max(w, getSuggestedMinimumWidth()); 614 | h = Math.max(h, getSuggestedMinimumHeight()); 615 | 616 | widthSize = resolveSizeAndState(w, widthMeasureSpec, 0); 617 | heightSize = resolveSizeAndState(h, heightMeasureSpec, 0); 618 | 619 | setMeasuredDimension(widthSize, heightSize); 620 | } 621 | 622 | @Override 623 | protected void onDraw(Canvas canvas) { 624 | super.onDraw(canvas); 625 | 626 | if (mForegroundDrawable == null && mBackgroundDrawable == null) { 627 | return; // couldn't resolve the URI 628 | } 629 | 630 | if (mMaxDrawableWidth == 0 || mMaxDrawableHeight == 0) { 631 | return; // nothing to draw (empty bounds) 632 | } 633 | 634 | if (mDrawMatrix == null && getPaddingTop() == 0 && getPaddingLeft() == 0) { 635 | if (mBackgroundDrawable != null) { 636 | mBackgroundDrawable.draw(canvas); 637 | } 638 | 639 | if (mSplitDrawPath != null) { 640 | canvas.clipPath(mSplitDrawPath); 641 | } 642 | 643 | if (mForegroundDrawable != null) { 644 | mForegroundDrawable.draw(canvas); 645 | } 646 | 647 | if (mEnableDebugDraw && mSplitDrawPath != null) { 648 | canvas.drawPath(mSplitDrawPath, mDebugDrawPaint); 649 | } 650 | } else { 651 | int saveCount = canvas.getSaveCount(); 652 | canvas.save(); 653 | 654 | if (mCropToPadding) { 655 | final int scrollX = getScrollX(); 656 | final int scrollY = getScrollY(); 657 | canvas.clipRect(scrollX + getPaddingLeft(), scrollY + getPaddingTop(), 658 | scrollX + getRight() - getLeft() - getPaddingRight(), 659 | scrollY + getBottom() - getTop() - getPaddingBottom()); 660 | } 661 | 662 | canvas.translate(getPaddingLeft(), getPaddingTop()); 663 | 664 | if (mDrawMatrix != null) { 665 | canvas.concat(mDrawMatrix); 666 | } 667 | if (mBackgroundDrawable != null) { 668 | mBackgroundDrawable.draw(canvas); 669 | } 670 | 671 | /** 672 | * Add a clipping path 673 | */ 674 | if (mSplitDrawPath != null) { 675 | canvas.clipPath(mSplitDrawPath); 676 | } 677 | 678 | if (mForegroundDrawable != null) { 679 | mForegroundDrawable.draw(canvas); 680 | } 681 | 682 | if (mEnableDebugDraw && mSplitDrawPath != null) { 683 | canvas.drawPath(mSplitDrawPath, mDebugDrawPaint); 684 | } 685 | canvas.restoreToCount(saveCount); 686 | } 687 | } 688 | 689 | @Override 690 | public void invalidateDrawable(Drawable dr) { 691 | if (dr == mForegroundDrawable || dr == mBackgroundDrawable) { 692 | /* we invalidate the whole view in this case because it's very 693 | * hard to know where the drawable actually is. This is made 694 | * complicated because of the offsets and transformations that 695 | * can be applied. In theory we could get the drawable's bounds 696 | * and run them through the transformation and offsets, but this 697 | * is probably not worth the effort. 698 | */ 699 | invalidate(); 700 | } else { 701 | super.invalidateDrawable(dr); 702 | } 703 | } 704 | 705 | @Override 706 | protected void onAttachedToWindow() { 707 | super.onAttachedToWindow(); 708 | if (mBackgroundDrawable != null) { 709 | mBackgroundDrawable.setVisible(getVisibility() == VISIBLE, false); 710 | } 711 | if (mForegroundDrawable != null) { 712 | mForegroundDrawable.setVisible(getVisibility() == VISIBLE, false); 713 | } 714 | } 715 | 716 | @Override 717 | protected void onDetachedFromWindow() { 718 | super.onDetachedFromWindow(); 719 | if (mBackgroundDrawable != null) { 720 | mBackgroundDrawable.setVisible(false, false); 721 | } 722 | if (mForegroundDrawable != null) { 723 | mForegroundDrawable.setVisible(false, false); 724 | } 725 | } 726 | 727 | @Override 728 | public void onInitializeAccessibilityEvent(AccessibilityEvent event) { 729 | super.onInitializeAccessibilityEvent(event); 730 | event.setClassName(SplitImageView.class.getName()); 731 | } 732 | 733 | @Override 734 | public void onPopulateAccessibilityEvent(AccessibilityEvent event) { 735 | super.onPopulateAccessibilityEvent(event); 736 | CharSequence contentDescription = getContentDescription(); 737 | if (!TextUtils.isEmpty(contentDescription)) { 738 | event.getText().add(contentDescription); 739 | } 740 | } 741 | 742 | @Override 743 | public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { 744 | super.onInitializeAccessibilityNodeInfo(info); 745 | info.setClassName(SplitImageView.class.getName()); 746 | } 747 | 748 | @Override 749 | protected boolean verifyDrawable(Drawable dr) { 750 | return mForegroundDrawable == dr || mBackgroundDrawable == dr || super.verifyDrawable(dr); 751 | } 752 | 753 | @Override 754 | public boolean onTouchEvent(MotionEvent event) { 755 | if (mGestureDetector != null) { 756 | mGestureDetector.onTouchEvent(event); 757 | } 758 | 759 | if (event.getAction() == MotionEvent.ACTION_UP) { 760 | snapSplitToBounds(); 761 | } 762 | return mUnveilOnTouch; 763 | } 764 | 765 | @Override 766 | protected Parcelable onSaveInstanceState() { 767 | if (!mEnableAutomaticAnimation) { 768 | Parcelable parcelable = super.onSaveInstanceState(); 769 | SavedState savedState = new SavedState(parcelable); 770 | savedState.splitPercent = mSplitPercent; 771 | return savedState; 772 | } 773 | return super.onSaveInstanceState(); 774 | } 775 | 776 | @Override 777 | protected void onRestoreInstanceState(Parcelable state) { 778 | if (!(state instanceof SavedState)) { 779 | super.onRestoreInstanceState(state); 780 | return; 781 | } 782 | 783 | SavedState savedState = (SavedState)state; 784 | super.onRestoreInstanceState(savedState.getSuperState()); 785 | if (mSplitPercent != savedState.splitPercent) { 786 | mSplitPercent = savedState.splitPercent; 787 | createPathForSplitPercent(mSplitPercent); 788 | } 789 | } 790 | 791 | private void initImageView() { 792 | setLayerType(LAYER_TYPE_SOFTWARE, null); 793 | mMatrix = new Matrix(); 794 | mScaleType = ScaleType.FIT_CENTER; 795 | 796 | mDebugDrawPaint = new Paint(); 797 | mDebugDrawPaint.setColor(Color.RED); 798 | mDebugDrawPaint.setStrokeWidth(5); 799 | mDebugDrawPaint.setStyle(Paint.Style.STROKE); 800 | } 801 | 802 | private void resolveUris() { 803 | resolveUri(false); 804 | resolveUri(true); 805 | } 806 | 807 | private void resolveUri(boolean forForegroundContent) { 808 | Drawable drawable = forForegroundContent ? mForegroundDrawable : mBackgroundDrawable; 809 | if (drawable != null) { 810 | return; 811 | } 812 | 813 | Resources rsrc = getResources(); 814 | if (rsrc == null) { 815 | return; 816 | } 817 | 818 | Drawable d = null; 819 | 820 | int resource = forForegroundContent ? mForegroundResId : mBackgroundResId; 821 | Uri uri = forForegroundContent ? mForegroundUri : mBackgroundUri; 822 | if (resource != 0) { 823 | try { 824 | d = rsrc.getDrawable(resource); 825 | } catch (Exception e) { 826 | Log.w(TAG, "Unable to find resource: " + resource, e); 827 | // Don't try again. 828 | 829 | if (forForegroundContent) { 830 | mForegroundUri = null; 831 | } else { 832 | mBackgroundUri = null; 833 | } 834 | } 835 | } else if (uri != null) { 836 | String scheme = uri.getScheme(); 837 | if (ContentResolver.SCHEME_ANDROID_RESOURCE.equals(scheme)) { 838 | try { 839 | // Load drawable through Resources, to get the source density information 840 | OpenResourceIdResult r = getResourceId(uri); 841 | d = r.r.getDrawable(r.id); 842 | } catch (Exception e) { 843 | Log.w(TAG, "Unable to open content: " + uri, e); 844 | } 845 | } else if (ContentResolver.SCHEME_CONTENT.equals(scheme) 846 | || ContentResolver.SCHEME_FILE.equals(scheme)) { 847 | InputStream stream = null; 848 | try { 849 | stream = getContext().getContentResolver().openInputStream(uri); 850 | d = Drawable.createFromStream(stream, null); 851 | } catch (Exception e) { 852 | Log.w(TAG, "Unable to open content: " + uri, e); 853 | } finally { 854 | if (stream != null) { 855 | try { 856 | stream.close(); 857 | } catch (IOException e) { 858 | Log.w(TAG, "Unable to close content: " + uri, e); 859 | } 860 | } 861 | } 862 | } else { 863 | d = Drawable.createFromPath(uri.toString()); 864 | } 865 | 866 | if (d == null) { 867 | System.out.println("resolveUri failed on bad bitmap uri: " + uri); 868 | // Don't try again. 869 | if (forForegroundContent) { 870 | mForegroundUri = null; 871 | } else { 872 | mBackgroundUri = null; 873 | } 874 | } 875 | } else { 876 | return; 877 | } 878 | 879 | updateDrawable(d, forForegroundContent); 880 | } 881 | 882 | private void updateDrawable(Drawable d, boolean isForegroundDrawable) { 883 | Drawable drawable = isForegroundDrawable ? mForegroundDrawable : mBackgroundDrawable; 884 | if (drawable != null) { 885 | drawable.setCallback(null); 886 | unscheduleDrawable(drawable); 887 | } 888 | 889 | if (isForegroundDrawable) { 890 | mForegroundDrawable = d; 891 | } else { 892 | mBackgroundDrawable = d; 893 | } 894 | 895 | if (d != null) { 896 | d.setCallback(this); 897 | d.setVisible(getVisibility() == VISIBLE, true); 898 | mMaxDrawableWidth = Math.max(d.getIntrinsicWidth(), mMaxDrawableWidth); 899 | mMaxDrawableHeight = Math.max(d.getIntrinsicHeight(), mMaxDrawableHeight); 900 | applyColorMod(); 901 | configureBounds(); 902 | } 903 | } 904 | 905 | private void configureBounds() { 906 | if ((mForegroundDrawable == null && mBackgroundDrawable == null) || !mHaveFrame) { 907 | return; 908 | } 909 | 910 | int dwidth = mMaxDrawableWidth; 911 | int dheight = mMaxDrawableHeight; 912 | 913 | int vwidth = getWidth() - getPaddingLeft() - getPaddingRight(); 914 | int vheight = getHeight() - getPaddingTop() - getPaddingBottom(); 915 | 916 | boolean fits = (dwidth < 0 || vwidth == dwidth) && 917 | (dheight < 0 || vheight == dheight); 918 | 919 | if (dwidth <= 0 || dheight <= 0 || ScaleType.FIT_XY == mScaleType) { 920 | 921 | if (mBackgroundDrawable != null) { 922 | mBackgroundDrawable.setBounds(0, 0, vwidth, vheight); 923 | } 924 | if (mForegroundDrawable != null) { 925 | mForegroundDrawable.setBounds(0, 0, vwidth, vheight); 926 | } 927 | mDrawMatrix = null; 928 | } else { 929 | 930 | if (mForegroundDrawable != null) { 931 | mForegroundDrawable.setBounds(0, 0, dwidth, dheight); 932 | } 933 | 934 | if (mBackgroundDrawable != null) { 935 | mBackgroundDrawable.setBounds(0, 0, dwidth, dheight); 936 | } 937 | 938 | if (ScaleType.MATRIX == mScaleType) { 939 | if (mMatrix.isIdentity()) { 940 | mDrawMatrix = null; 941 | } else { 942 | mDrawMatrix = mMatrix; 943 | } 944 | } else if (fits) { 945 | mDrawMatrix = null; 946 | } else if (ScaleType.CENTER == mScaleType) { 947 | mDrawMatrix = mMatrix; 948 | mDrawMatrix.setTranslate((int) ((vwidth - dwidth) * 0.5f + 0.5f), 949 | (int) ((vheight - dheight) * 0.5f + 0.5f)); 950 | } else if (ScaleType.CENTER_CROP == mScaleType) { 951 | mDrawMatrix = mMatrix; 952 | 953 | float scale; 954 | float dx = 0, dy = 0; 955 | 956 | if (dwidth * vheight > vwidth * dheight) { 957 | scale = (float) vheight / (float) dheight; 958 | dx = (vwidth - dwidth * scale) * 0.5f; 959 | } else { 960 | scale = (float) vwidth / (float) dwidth; 961 | dy = (vheight - dheight * scale) * 0.5f; 962 | } 963 | 964 | mDrawMatrix.setScale(scale, scale); 965 | mDrawMatrix.postTranslate((int) (dx + 0.5f), (int) (dy + 0.5f)); 966 | } else if (ScaleType.CENTER_INSIDE == mScaleType) { 967 | mDrawMatrix = mMatrix; 968 | float scale; 969 | float dx; 970 | float dy; 971 | 972 | if (dwidth <= vwidth && dheight <= vheight) { 973 | scale = 1.0f; 974 | } else { 975 | scale = Math.min((float) vwidth / (float) dwidth, 976 | (float) vheight / (float) dheight); 977 | } 978 | 979 | dx = (int) ((vwidth - dwidth * scale) * 0.5f + 0.5f); 980 | dy = (int) ((vheight - dheight * scale) * 0.5f + 0.5f); 981 | 982 | mDrawMatrix.setScale(scale, scale); 983 | mDrawMatrix.postTranslate(dx, dy); 984 | } else { 985 | mTempSrc.set(0, 0, dwidth, dheight); 986 | mTempDst.set(0, 0, vwidth, vheight); 987 | 988 | mDrawMatrix = mMatrix; 989 | mDrawMatrix.setRectToRect(mTempSrc, mTempDst, scaleTypeToScaleToFit(mScaleType)); 990 | } 991 | } 992 | } 993 | 994 | private void createPathForSplitPercent(int splitPercent) { 995 | 996 | if (hasForegroundContent() && hasBackgroundContent() && mMaxDrawableHeight > 0 997 | && mMaxDrawableWidth > 0) { 998 | int width = getScaleType() == ScaleType.FIT_XY ? getWidth() : mMaxDrawableWidth; 999 | int height = getScaleType() == ScaleType.FIT_XY ? getHeight() : mMaxDrawableHeight; 1000 | 1001 | float aspectRatio = width/(height * 1.0f); 1002 | int max = Math.max(width, height); 1003 | float offset = -max + (max * 2) * (splitPercent/100.0f); 1004 | 1005 | mSplitDrawPath = new Path(); 1006 | mSplitDrawPath.moveTo(0, height); 1007 | mSplitDrawPath.lineTo(0, -offset); 1008 | mSplitDrawPath.lineTo(width + offset * aspectRatio, height); 1009 | mSplitDrawPath.close(); 1010 | } else { 1011 | mSplitDrawPath = null; 1012 | } 1013 | invalidate(); 1014 | } 1015 | 1016 | private boolean hasForegroundContent() { 1017 | return mForegroundDrawable != null || mForegroundUri != null || mForegroundResId != 0; 1018 | } 1019 | 1020 | private boolean hasBackgroundContent() { 1021 | return mBackgroundDrawable != null || mBackgroundUri != null || mBackgroundResId != 0; 1022 | } 1023 | 1024 | private void applyColorMod() { 1025 | // Only mutate and apply when modifications have occurred. This should 1026 | // not reset the mColorMod flag, since these filters need to be 1027 | // re-applied if the Drawable is changed. 1028 | if (mColorMod) { 1029 | if (mBackgroundDrawable != null) { 1030 | mBackgroundDrawable = mBackgroundDrawable.mutate(); 1031 | mBackgroundDrawable.setColorFilter(mColorFilter); 1032 | mBackgroundDrawable.setAlpha(mAlpha * mViewAlphaScale >> 8); 1033 | } 1034 | 1035 | if (mForegroundDrawable != null) { 1036 | mForegroundDrawable = mForegroundDrawable.mutate(); 1037 | mForegroundDrawable.setColorFilter(mColorFilter); 1038 | mForegroundDrawable.setAlpha(mAlpha * mViewAlphaScale >> 8); 1039 | } 1040 | } 1041 | } 1042 | 1043 | private void snapSplitToBounds() { 1044 | if (mUnveilOnTouch && mSnapToBounds) { 1045 | if (mSplitPercent >= 100 - SNAP_MARGIN_PERCENTAGE || mSplitPercent <= SNAP_MARGIN_PERCENTAGE) { 1046 | animateSplitPercent(mSplitPercent <= SNAP_MARGIN_PERCENTAGE ? 0 : 100); 1047 | } 1048 | } 1049 | } 1050 | 1051 | private void animateSplitPercent(int toPercent) { 1052 | animateSplitPercent(mSplitPercent, toPercent); 1053 | } 1054 | 1055 | private void animateSplitPercent(int fromPercent, int toPercent) { 1056 | if (!mIsAnimating && fromPercent >=0 && fromPercent <= 100 && toPercent >=0 1057 | && toPercent <= 100) { 1058 | ValueAnimator valueAnimator = ValueAnimator.ofInt(fromPercent, toPercent); 1059 | valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 1060 | @Override 1061 | public void onAnimationUpdate(ValueAnimator animation) { 1062 | setSplitPercent((Integer) animation.getAnimatedValue()); 1063 | } 1064 | }); 1065 | valueAnimator.addListener(new Animator.AnimatorListener() { 1066 | @Override 1067 | public void onAnimationStart(Animator animation) { 1068 | mIsAnimating = true; 1069 | } 1070 | 1071 | @Override 1072 | public void onAnimationEnd(Animator animation) { 1073 | mIsAnimating = false; 1074 | } 1075 | 1076 | @Override 1077 | public void onAnimationCancel(Animator animation) { 1078 | mIsAnimating = false; 1079 | } 1080 | 1081 | @Override 1082 | public void onAnimationRepeat(Animator animation) { 1083 | 1084 | } 1085 | }); 1086 | valueAnimator.setDuration((300 * Math.abs(toPercent - fromPercent))/SNAP_MARGIN_PERCENTAGE); 1087 | valueAnimator.start(); 1088 | } else { 1089 | Log.e(TAG, "Error in animateSplitPercent, fromPercent and toPercent should be between 0 - 100"); 1090 | } 1091 | } 1092 | 1093 | private static final Matrix.ScaleToFit[] sS2FArray = { 1094 | Matrix.ScaleToFit.FILL, 1095 | Matrix.ScaleToFit.START, 1096 | Matrix.ScaleToFit.CENTER, 1097 | Matrix.ScaleToFit.END 1098 | }; 1099 | 1100 | private static Matrix.ScaleToFit scaleTypeToScaleToFit(ScaleType st) { 1101 | // ScaleToFit enum to their corresponding Matrix.ScaleToFit values 1102 | return sS2FArray[st.nativeInt - 1]; 1103 | } 1104 | 1105 | private OpenResourceIdResult getResourceId(Uri uri) throws FileNotFoundException { 1106 | String authority = uri.getAuthority(); 1107 | Resources r; 1108 | if (TextUtils.isEmpty(authority)) { 1109 | throw new FileNotFoundException("No authority: " + uri); 1110 | } else { 1111 | try { 1112 | r = getContext().getPackageManager().getResourcesForApplication(authority); 1113 | } catch (PackageManager.NameNotFoundException ex) { 1114 | throw new FileNotFoundException("No package found for authority: " + uri); 1115 | } 1116 | } 1117 | List path = uri.getPathSegments(); 1118 | if (path == null) { 1119 | throw new FileNotFoundException("No path: " + uri); 1120 | } 1121 | int len = path.size(); 1122 | int id; 1123 | if (len == 1) { 1124 | try { 1125 | id = Integer.parseInt(path.get(0)); 1126 | } catch (NumberFormatException e) { 1127 | throw new FileNotFoundException("Single path segment is not a resource ID: " + uri); 1128 | } 1129 | } else if (len == 2) { 1130 | id = r.getIdentifier(path.get(1), path.get(0), authority); 1131 | } else { 1132 | throw new FileNotFoundException("More than two path segments: " + uri); 1133 | } 1134 | if (id == 0) { 1135 | throw new FileNotFoundException("No resource found for: " + uri); 1136 | } 1137 | OpenResourceIdResult res = new OpenResourceIdResult(); 1138 | res.r = r; 1139 | res.id = id; 1140 | return res; 1141 | } 1142 | 1143 | private void initGestureRecognizer() { 1144 | if (mUnveilOnTouch && !mEnableAutomaticAnimation && hasForegroundContent() && hasBackgroundContent() && mGestureDetector == null) { 1145 | mGestureDetector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() { 1146 | @Override 1147 | public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { 1148 | if (mIsAnimating || mEnableAutomaticAnimation) { 1149 | return false; 1150 | } 1151 | float locationX = e2.getX(); 1152 | float locationY = e2.getY(); 1153 | 1154 | double distance = Math.sqrt(locationX * locationX + Math.pow(getHeight() - locationY, 2)); 1155 | double maxDistance = Math.sqrt(getWidth() * getWidth() + getHeight() * getHeight()); 1156 | 1157 | double fraction = distance/maxDistance; 1158 | int fractionPercent = (int) (fraction * 100); 1159 | 1160 | setSplitPercent(fractionPercent); 1161 | Log.i(TAG, "Changing split percent:" + fractionPercent); 1162 | return true; 1163 | } 1164 | }); 1165 | } else { 1166 | mGestureDetector = null; 1167 | } 1168 | } 1169 | 1170 | private void startAutomaticAnimation() { 1171 | if (mEnableAutomaticAnimation && hasForegroundContent() && hasBackgroundContent()) { 1172 | 1173 | ValueAnimator currentSplitPercentToMax = ValueAnimator.ofInt(mSplitPercent, 100); 1174 | currentSplitPercentToMax.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 1175 | @Override 1176 | public void onAnimationUpdate(ValueAnimator animation) { 1177 | setSplitPercent((Integer) animation.getAnimatedValue()); 1178 | } 1179 | }); 1180 | currentSplitPercentToMax.setDuration(((100 - mSplitPercent) * mAutomaticAnimationDuration)/100); 1181 | 1182 | ValueAnimator toMin = ValueAnimator.ofInt(100, 0); 1183 | toMin.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 1184 | @Override 1185 | public void onAnimationUpdate(ValueAnimator animation) { 1186 | if (mEnableAutomaticAnimation) { 1187 | setSplitPercent((Integer) animation.getAnimatedValue()); 1188 | } 1189 | } 1190 | }); 1191 | toMin.setDuration(mAutomaticAnimationDuration); 1192 | 1193 | ValueAnimator toMax = ValueAnimator.ofInt(0, 100); 1194 | toMax.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 1195 | @Override 1196 | public void onAnimationUpdate(ValueAnimator animation) { 1197 | if (mEnableAutomaticAnimation) { 1198 | setSplitPercent((Integer) animation.getAnimatedValue()); 1199 | } 1200 | } 1201 | }); 1202 | toMax.setDuration(mAutomaticAnimationDuration); 1203 | 1204 | currentSplitPercentToMax.start(); 1205 | currentSplitPercentToMax.addListener(new Animator.AnimatorListener() { 1206 | @Override 1207 | public void onAnimationStart(Animator animation) { 1208 | 1209 | } 1210 | 1211 | @Override 1212 | public void onAnimationEnd(Animator animation) { 1213 | mAutomaticAnimationAnimatorSet.start(); 1214 | } 1215 | 1216 | @Override 1217 | public void onAnimationCancel(Animator animation) { 1218 | 1219 | } 1220 | 1221 | @Override 1222 | public void onAnimationRepeat(Animator animation) { 1223 | 1224 | } 1225 | }); 1226 | mAutomaticAnimationAnimatorSet = new AnimatorSet(); 1227 | mAutomaticAnimationAnimatorSet.playSequentially(toMin, toMax); 1228 | 1229 | mAutomaticAnimationAnimatorSet.addListener(new Animator.AnimatorListener() { 1230 | @Override 1231 | public void onAnimationStart(Animator animation) { 1232 | 1233 | } 1234 | 1235 | @Override 1236 | public void onAnimationEnd(Animator animation) { 1237 | if (mEnableAutomaticAnimation) { 1238 | mAutomaticAnimationAnimatorSet.start(); 1239 | } 1240 | } 1241 | 1242 | @Override 1243 | public void onAnimationCancel(Animator animation) { 1244 | mEnableAutomaticAnimation = false; 1245 | } 1246 | 1247 | @Override 1248 | public void onAnimationRepeat(Animator animation) { 1249 | 1250 | } 1251 | }); 1252 | } 1253 | } 1254 | 1255 | private void stopAutomaticAnimation() { 1256 | if (mEnableAutomaticAnimation) { 1257 | mEnableAutomaticAnimation = false; 1258 | if (mAutomaticAnimationAnimatorSet != null) { 1259 | mAutomaticAnimationAnimatorSet.cancel(); 1260 | } 1261 | initGestureRecognizer(); 1262 | } 1263 | } 1264 | 1265 | /** 1266 | * A resource identified by the {@link android.content.res.Resources} that contains it, and a resource id. 1267 | * 1268 | */ 1269 | private class OpenResourceIdResult { 1270 | public Resources r; 1271 | public int id; 1272 | } 1273 | 1274 | /** 1275 | * Options for scaling the bounds of both images to the bounds of this view. 1276 | */ 1277 | public enum ScaleType { 1278 | /** 1279 | * Scale using the image matrix when drawing. The image matrix can be set using 1280 | * setImageMatrix 1281 | * 1282 | * From XML, use this syntax: 1283 | * namespace:scaleType="matrix". 1284 | */ 1285 | MATRIX (0), 1286 | /** 1287 | * Scale the image using {@link android.graphics.Matrix.ScaleToFit#FILL}. 1288 | * From XML, use this syntax: namespace:scaleType="fitXY". 1289 | */ 1290 | FIT_XY (1), 1291 | /** 1292 | * Scale the image using {@link android.graphics.Matrix.ScaleToFit#START}. 1293 | * From XML, use this syntax: namespace:scaleType="fitStart". 1294 | */ 1295 | FIT_START (2), 1296 | /** 1297 | * Scale the image using {@link android.graphics.Matrix.ScaleToFit#CENTER}. 1298 | * From XML, use this syntax: 1299 | * namespace:scaleType="fitCenter". 1300 | */ 1301 | FIT_CENTER (3), 1302 | /** 1303 | * Scale the image using {@link android.graphics.Matrix.ScaleToFit#END}. 1304 | * From XML, use this syntax: namespace:scaleType="fitEnd". 1305 | */ 1306 | FIT_END (4), 1307 | /** 1308 | * Center the image in the view, but perform no scaling. 1309 | * From XML, use this syntax: android:scaleType="center". 1310 | */ 1311 | CENTER (5), 1312 | /** 1313 | * Scale the image uniformly (maintain the image's aspect ratio) so 1314 | * that both dimensions (width and height) of the image will be equal 1315 | * to or larger than the corresponding dimension of the view 1316 | * (minus padding). The image is then centered in the view. 1317 | * From XML, use this syntax: namespace:scaleType="centerCrop". 1318 | */ 1319 | CENTER_CROP (6), 1320 | /** 1321 | * Scale the image uniformly (maintain the image's aspect ratio) so 1322 | * that both dimensions (width and height) of the image will be equal 1323 | * to or less than the corresponding dimension of the view 1324 | * (minus padding). The image is then centered in the view. 1325 | * From XML, use this syntax: namespace:scaleType="centerInside". 1326 | */ 1327 | CENTER_INSIDE (7); 1328 | 1329 | ScaleType(int ni) { 1330 | nativeInt = ni; 1331 | } 1332 | final int nativeInt; 1333 | } 1334 | 1335 | static class SavedState extends BaseSavedState { 1336 | 1337 | int splitPercent; 1338 | 1339 | public SavedState(Parcel source) { 1340 | super(source); 1341 | splitPercent = source.readInt(); 1342 | } 1343 | 1344 | public SavedState(Parcelable superState) { 1345 | super(superState); 1346 | } 1347 | 1348 | @Override 1349 | public void writeToParcel(Parcel dest, int flags) { 1350 | super.writeToParcel(dest, flags); 1351 | dest.writeInt(splitPercent); 1352 | } 1353 | 1354 | //required field that makes Parcelables from a Parcel 1355 | public static final Parcelable.Creator CREATOR = 1356 | new Parcelable.Creator() { 1357 | public SavedState createFromParcel(Parcel in) { 1358 | return new SavedState(in); 1359 | } 1360 | 1361 | public SavedState[] newArray(int size) { 1362 | return new SavedState[size]; 1363 | } 1364 | }; 1365 | } 1366 | } 1367 | --------------------------------------------------------------------------------