├── .gitignore ├── LICENSE ├── README.md ├── build.gradle ├── gradle.properties ├── library ├── .gitignore ├── build.gradle ├── gradle-mvn-push.gradle ├── gradle.properties └── src │ ├── androidTest │ ├── assets │ │ ├── loop_once.gif │ │ ├── w3c_home.gif │ │ └── w3c_home_animation.gif │ └── java │ │ └── com │ │ └── doctoror │ │ └── imagefactory │ │ └── test │ │ └── ImageFactoryTest.java │ └── main │ ├── AndroidManifest.xml │ └── java │ └── com │ └── doctoror │ └── imagefactory │ └── ImageFactory.java ├── sample ├── .gitignore ├── build.gradle └── src │ └── main │ ├── AndroidManifest.xml │ ├── assets │ ├── LoopOnce.gif │ ├── Rotating_earth_(large).gif │ ├── Static_earth.gif │ ├── ru9gag.gif │ ├── ru9gag1.gif │ ├── ru9gag3.gif │ ├── ru9gag4.gif │ ├── small.gif │ └── smallest.gif │ ├── java │ └── com │ │ └── doctoror │ │ └── imagefactory │ │ └── sample │ │ ├── BaseAdapter2.java │ │ └── DemoActivity.java │ └── res │ ├── layout │ ├── activity_demo.xml │ └── grid_item_demo.xml │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ └── values │ ├── strings.xml │ └── styles.xml └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | #Android Studio files 2 | .gradle 3 | local.properties 4 | .idea/workspace.xml 5 | .idea/libraries 6 | .DS_Store 7 | build 8 | 9 | # built application files 10 | *.apk 11 | *.ap_ 12 | 13 | # files for the dex VM 14 | *.dex 15 | 16 | # Java class files 17 | *.class 18 | 19 | # generated files 20 | proguard_logs 21 | proguard/ 22 | bin/ 23 | gen/ 24 | target/ 25 | obj/ 26 | .apt_generated 27 | 28 | # Local configuration file (sdk path, etc) 29 | local.properties 30 | .directory 31 | .gitattributes 32 | 33 | # Eclipse project files 34 | .classpath 35 | #.project 36 | .settings 37 | 38 | # Proguard folder generated by Eclipse 39 | proguard/ 40 | 41 | # Intellij project files 42 | *.iml 43 | *.ipr 44 | *.iws 45 | .idea/ 46 | 47 | #Gradle files or folders 48 | gradlew* 49 | gradle 50 | .gradle 51 | build 52 | 53 | #Web site 54 | node_modules/ 55 | .sass-cache/ 56 | npm-debug.log 57 | WebSite/upload/ 58 | -------------------------------------------------------------------------------- /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 2015 Yaroslav Mytkalyk 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. 202 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ##Description 2 | 3 | ImageFactory is much like BitmapFactory but returns GifDrawable if decoded image is an animated GIF or BitmapDrawable if not. 4 | 5 | Depends on [koral--/android-gif-drawable](https://github.com/koral--/android-gif-drawable) 6 | 7 | Min API level 8 8 | 9 | [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.doctoror.imagefactory/library/badge.png?style=flat)](https://maven-badges.herokuapp.com/maven-central/com.github.doctoror.imagefactory/library) 10 | 11 | Add this to build.gradle dependencies, replacing x.x.x with version number 12 | 13 | ```groovy 14 | dependencies { 15 | compile 'com.github.doctoror.imagefactory:library:x.x.x' 16 | } 17 | ``` 18 | 19 | 20 | ##License 21 | 22 | ``` 23 | Copyright 2015 Yaroslav Mytkalyk 24 | 25 | Licensed under the Apache License, Version 2.0 (the "License"); 26 | you may not use this file except in compliance with the License. 27 | You may obtain a copy of the License at 28 | 29 | http://www.apache.org/licenses/LICENSE-2.0 30 | 31 | Unless required by applicable law or agreed to in writing, software 32 | distributed under the License is distributed on an "AS IS" BASIS, 33 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 34 | See the License for the specific language governing permissions and 35 | limitations under the License. 36 | 37 | ``` 38 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:1.1.3' 9 | 10 | // NOTE: Do not place your application dependencies here; they belong 11 | // in the individual module build.gradle files 12 | } 13 | } 14 | 15 | allprojects { 16 | repositories { 17 | jcenter() 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | # 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 19 | 20 | VERSION_NAME=1.0.1 21 | VERSION_CODE=1 22 | GROUP=com.github.doctoror.imagefactory 23 | 24 | POM_DESCRIPTION=ImageFactory is much like BitmapFactory but returns GifDrawable if decoded image is an animated GIF or BitmapDrawable if not. 25 | POM_URL=https://github.com/Doctoror/ImageFactory 26 | POM_SCM_URL=https://github.com/Doctoror/ImageFactory 27 | POM_SCM_CONNECTION=scm:git@github.com:Doctoror/ImageFactory.git 28 | POM_SCM_DEV_CONNECTION=scm:git@github.com:Doctoror/ImageFactory.git 29 | POM_LICENCE_NAME=The Apache Software License, Version 2.0 30 | POM_LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0.txt 31 | POM_LICENCE_DIST=repo 32 | POM_DEVELOPER_ID=Doctoror 33 | POM_DEVELOPER_NAME=Yaroslav Mytkalyk 34 | -------------------------------------------------------------------------------- /library/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /library/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 22 5 | buildToolsVersion "22.0.1" 6 | 7 | defaultConfig { 8 | minSdkVersion 8 9 | targetSdkVersion 22 10 | versionCode 1 11 | versionName "1.0.1" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | } 17 | } 18 | } 19 | 20 | dependencies { 21 | compile fileTree(dir: 'libs', include: ['*.jar']) 22 | compile 'pl.droidsonroids.gif:android-gif-drawable:1.1.3' 23 | compile 'com.android.support:support-annotations:22.0.0' 24 | } 25 | apply from: 'gradle-mvn-push.gradle' 26 | -------------------------------------------------------------------------------- /library/gradle-mvn-push.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 Chris Banes 3 | * Copyright 2015 Yaroslav Mytkalyk 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | apply plugin: 'maven' 19 | apply plugin: 'signing' 20 | 21 | def isReleaseBuild() { 22 | return VERSION_NAME.contains("SNAPSHOT") == false 23 | } 24 | 25 | def getReleaseRepositoryUrl() { 26 | return hasProperty('RELEASE_REPOSITORY_URL') ? RELEASE_REPOSITORY_URL 27 | : "https://oss.sonatype.org/service/local/staging/deploy/maven2/" 28 | } 29 | 30 | def getSnapshotRepositoryUrl() { 31 | return hasProperty('SNAPSHOT_REPOSITORY_URL') ? SNAPSHOT_REPOSITORY_URL 32 | : "https://oss.sonatype.org/content/repositories/snapshots/" 33 | } 34 | 35 | def getRepositoryUsername() { 36 | return hasProperty('NEXUS_USERNAME') ? NEXUS_USERNAME : "" 37 | } 38 | 39 | def getRepositoryPassword() { 40 | return hasProperty('NEXUS_PASSWORD') ? NEXUS_PASSWORD : "" 41 | } 42 | 43 | afterEvaluate { project -> 44 | uploadArchives { 45 | repositories { 46 | mavenDeployer { 47 | beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) } 48 | 49 | pom.groupId = GROUP 50 | pom.artifactId = POM_ARTIFACT_ID 51 | pom.version = VERSION_NAME 52 | 53 | repository(url: getReleaseRepositoryUrl()) { 54 | authentication(userName: getRepositoryUsername(), password: getRepositoryPassword()) 55 | } 56 | snapshotRepository(url: getSnapshotRepositoryUrl()) { 57 | authentication(userName: getRepositoryUsername(), password: getRepositoryPassword()) 58 | } 59 | 60 | pom.project { 61 | name POM_NAME 62 | packaging POM_PACKAGING 63 | description POM_DESCRIPTION 64 | url POM_URL 65 | 66 | scm { 67 | url POM_SCM_URL 68 | connection POM_SCM_CONNECTION 69 | developerConnection POM_SCM_DEV_CONNECTION 70 | } 71 | 72 | licenses { 73 | license { 74 | name POM_LICENCE_NAME 75 | url POM_LICENCE_URL 76 | distribution POM_LICENCE_DIST 77 | } 78 | } 79 | 80 | developers { 81 | developer { 82 | id POM_DEVELOPER_ID 83 | name POM_DEVELOPER_NAME 84 | } 85 | } 86 | } 87 | } 88 | } 89 | } 90 | 91 | signing { 92 | required { isReleaseBuild() && gradle.taskGraph.hasTask("uploadArchives") } 93 | sign configurations.archives 94 | } 95 | 96 | task androidJavadocs(type: Javadoc) { 97 | source = android.sourceSets.main.java.srcDirs 98 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) 99 | failOnError false 100 | } 101 | 102 | task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) { 103 | classifier = 'javadoc' 104 | from androidJavadocs.destinationDir 105 | } 106 | 107 | task androidSourcesJar(type: Jar) { 108 | classifier = 'sources' 109 | from android.sourceSets.main.java.sourceFiles 110 | } 111 | 112 | artifacts { 113 | archives androidSourcesJar 114 | archives androidJavadocsJar 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /library/gradle.properties: -------------------------------------------------------------------------------- 1 | POM_NAME=ImageFactory 2 | POM_ARTIFACT_ID=library 3 | POM_PACKAGING=aar 4 | -------------------------------------------------------------------------------- /library/src/androidTest/assets/loop_once.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doctoror/ImageFactory/355b291bbea9d475d5d6b88d719c0c81341fa379/library/src/androidTest/assets/loop_once.gif -------------------------------------------------------------------------------- /library/src/androidTest/assets/w3c_home.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doctoror/ImageFactory/355b291bbea9d475d5d6b88d719c0c81341fa379/library/src/androidTest/assets/w3c_home.gif -------------------------------------------------------------------------------- /library/src/androidTest/assets/w3c_home_animation.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doctoror/ImageFactory/355b291bbea9d475d5d6b88d719c0c81341fa379/library/src/androidTest/assets/w3c_home_animation.gif -------------------------------------------------------------------------------- /library/src/androidTest/java/com/doctoror/imagefactory/test/ImageFactoryTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Yaroslav Mytkalyk 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.doctoror.imagefactory.test; 18 | 19 | import com.doctoror.imagefactory.ImageFactory; 20 | 21 | import android.content.Context; 22 | import android.content.res.AssetManager; 23 | import android.graphics.drawable.BitmapDrawable; 24 | import android.graphics.drawable.Drawable; 25 | import android.test.InstrumentationTestCase; 26 | 27 | import java.io.BufferedInputStream; 28 | import java.io.ByteArrayInputStream; 29 | import java.io.ByteArrayOutputStream; 30 | import java.io.IOException; 31 | import java.io.InputStream; 32 | 33 | import pl.droidsonroids.gif.GifDrawable; 34 | 35 | public final class ImageFactoryTest extends InstrumentationTestCase { 36 | 37 | public void testStaticGifAsAssetInputStream() throws Throwable { 38 | final Context context = getInstrumentation().getContext(); 39 | final InputStream is1 = context.getAssets() 40 | .open("w3c_home.gif", AssetManager.ACCESS_RANDOM); 41 | try { 42 | assertFalse(ImageFactory.isAnimatedGif(new BufferedInputStream(is1))); 43 | } finally { 44 | is1.close(); 45 | } 46 | 47 | final InputStream is = context.getAssets().open("w3c_home.gif", AssetManager.ACCESS_RANDOM); 48 | try { 49 | assertTrue(ImageFactory 50 | .decodeStream(context.getResources(), is) instanceof BitmapDrawable); 51 | } finally { 52 | is.close(); 53 | } 54 | } 55 | 56 | public void testStaticGifAsByteArray() throws Throwable { 57 | final Context context = getInstrumentation().getContext(); 58 | final InputStream is1 = context.getAssets() 59 | .open("w3c_home.gif", AssetManager.ACCESS_RANDOM); 60 | final byte[] data; 61 | try { 62 | data = toByteArray(is1); 63 | } finally { 64 | is1.close(); 65 | } 66 | 67 | assertFalse( 68 | ImageFactory 69 | .isAnimatedGif(new BufferedInputStream(new ByteArrayInputStream(data)))); 70 | assertTrue(ImageFactory 71 | .decodeByteArray(context.getResources(), data) instanceof BitmapDrawable); 72 | } 73 | 74 | public void testLoopOnceAnimatedGifAsAssetInputStream() throws Throwable { 75 | final Context context = getInstrumentation().getContext(); 76 | final InputStream is1 = context.getAssets() 77 | .open("loop_once.gif", AssetManager.ACCESS_RANDOM); 78 | try { 79 | assertTrue(ImageFactory.isAnimatedGif(new BufferedInputStream(is1))); 80 | } finally { 81 | is1.close(); 82 | } 83 | 84 | final InputStream is = context.getAssets() 85 | .open("loop_once.gif", AssetManager.ACCESS_RANDOM); 86 | final Drawable result; 87 | try { 88 | result = ImageFactory.decodeStreamOrThrow(context.getResources(), is, null, null); 89 | } finally { 90 | is.close(); 91 | } 92 | assertTrue(result instanceof GifDrawable); 93 | assertTrue(((GifDrawable) result).getLoopCount() == 1); 94 | } 95 | 96 | public void testLoopOnceAnimatedGifAsByteArray() throws Throwable { 97 | final Context context = getInstrumentation().getContext(); 98 | final InputStream is1 = context.getAssets() 99 | .open("loop_once.gif", AssetManager.ACCESS_RANDOM); 100 | final byte[] data; 101 | try { 102 | data = toByteArray(is1); 103 | } finally { 104 | is1.close(); 105 | } 106 | 107 | assertTrue(ImageFactory 108 | .isAnimatedGif(new BufferedInputStream(new ByteArrayInputStream(data)))); 109 | final Drawable result = ImageFactory.decodeByteArray(context.getResources(), data); 110 | assertTrue(result instanceof GifDrawable); 111 | assertTrue(((GifDrawable) result).getLoopCount() == 1); 112 | } 113 | 114 | public void testLoopedAnimatedGifAsAssetInputStream() throws Throwable { 115 | final Context context = getInstrumentation().getContext(); 116 | final InputStream is1 = context.getAssets() 117 | .open("w3c_home_animation.gif", AssetManager.ACCESS_RANDOM); 118 | try { 119 | assertTrue(ImageFactory.isAnimatedGif(new BufferedInputStream(is1))); 120 | } finally { 121 | is1.close(); 122 | } 123 | 124 | final InputStream is = context.getAssets() 125 | .open("w3c_home_animation.gif", AssetManager.ACCESS_RANDOM); 126 | final Drawable result; 127 | try { 128 | result = ImageFactory.decodeStream(context.getResources(), is); 129 | } finally { 130 | is.close(); 131 | } 132 | assertTrue(result instanceof GifDrawable); 133 | assertTrue(((GifDrawable) result).getLoopCount() == 0); 134 | } 135 | 136 | public void testLoopedAnimatedGifAsByteArray() throws Throwable { 137 | final Context context = getInstrumentation().getContext(); 138 | final InputStream is1 = context.getAssets() 139 | .open("w3c_home_animation.gif", AssetManager.ACCESS_RANDOM); 140 | final byte[] data; 141 | try { 142 | data = toByteArray(is1); 143 | } finally { 144 | is1.close(); 145 | } 146 | 147 | assertTrue(ImageFactory 148 | .isAnimatedGif(new BufferedInputStream(new ByteArrayInputStream(data)))); 149 | 150 | final Drawable result = ImageFactory.decodeByteArray(context.getResources(), data); 151 | assertTrue(result instanceof GifDrawable); 152 | assertTrue(((GifDrawable) result).getLoopCount() == 0); 153 | } 154 | 155 | private byte[] toByteArray(final InputStream is) throws IOException { 156 | final ByteArrayOutputStream baos = new ByteArrayOutputStream( 157 | is.available() > 0 ? is.available() : 10240); 158 | final byte[] buffer = new byte[10240]; 159 | int read; 160 | while ((read = is.read(buffer)) != -1) { 161 | baos.write(buffer, 0, read); 162 | } 163 | return baos.toByteArray(); 164 | } 165 | 166 | } 167 | -------------------------------------------------------------------------------- /library/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /library/src/main/java/com/doctoror/imagefactory/ImageFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Yaroslav Mytkalyk 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.doctoror.imagefactory; 18 | 19 | import android.content.res.AssetFileDescriptor; 20 | import android.content.res.Resources; 21 | import android.graphics.Bitmap; 22 | import android.graphics.BitmapFactory; 23 | import android.graphics.Rect; 24 | import android.graphics.drawable.BitmapDrawable; 25 | import android.graphics.drawable.Drawable; 26 | import android.support.annotation.DrawableRes; 27 | import android.support.annotation.NonNull; 28 | import android.support.annotation.Nullable; 29 | import android.support.annotation.RawRes; 30 | import android.util.Log; 31 | 32 | import java.io.BufferedInputStream; 33 | import java.io.ByteArrayInputStream; 34 | import java.io.FileDescriptor; 35 | import java.io.FileInputStream; 36 | import java.io.IOException; 37 | import java.io.InputStream; 38 | 39 | import pl.droidsonroids.gif.GifDrawable; 40 | 41 | /** 42 | * Used for decoding regular images or animated GIF into a {@link Drawable} 43 | * The interface is much like {@link BitmapFactory}. 44 | */ 45 | @SuppressWarnings("UnusedDeclaration") 46 | public final class ImageFactory { 47 | 48 | private ImageFactory() { 49 | // Private constructor, do not instantiate 50 | } 51 | 52 | private static final String TAG = "ImageFactory"; 53 | 54 | /** 55 | * Decodes image from byte array. 56 | * Returns {@link GifDrawable} if the image is an animated GIF. 57 | * Returns {@link BitmapDrawable} if the image is s valid static image {@link BitmapFactory} 58 | * can decode. 59 | * Returns null on error. 60 | * 61 | * @param res Resources to use if creating a BitmapDrawable 62 | * @param data byte array of compressed image data 63 | * @return decoded {@link Drawable} or null on error 64 | * @throws NullPointerException if the data byte array is null 65 | */ 66 | @Nullable 67 | public static Drawable decodeByteArray(@NonNull final Resources res, 68 | final byte[] data) { 69 | return decodeByteArray(res, data, null); 70 | } 71 | 72 | /** 73 | * Decodes image from byte array. 74 | * Returns {@link GifDrawable} if the image is an animated GIF. 75 | * Returns {@link BitmapDrawable} if the image is s valid static image {@link BitmapFactory} 76 | * can decode. 77 | * Returns null on error. 78 | * 79 | * @param res Resources to use if creating a BitmapDrawable 80 | * @param data byte array of compressed image data 81 | * @param options optional options if an image will be decoded to a Bitmap 82 | * @return decoded {@link Drawable} or null on error 83 | * @throws NullPointerException if the data byte array is null 84 | */ 85 | @Nullable 86 | public static Drawable decodeByteArray(@Nullable final Resources res, 87 | final byte[] data, 88 | @Nullable final BitmapFactory.Options options) { 89 | try { 90 | return decodeByteArrayOrThrow(res, data, options); 91 | } catch (IOException e) { 92 | Log.w(TAG, "decodeByteArray() " + e); 93 | return null; 94 | } 95 | } 96 | 97 | /** 98 | * Decodes image from byte array. 99 | * Returns {@link GifDrawable} if the image is an animated GIF. 100 | * Returns {@link BitmapDrawable} if the image is s valid static image {@link BitmapFactory} 101 | * can decode. 102 | * Returns null on error. 103 | * 104 | * @param res Resources to use if creating a BitmapDrawable 105 | * @param data byte array of compressed image data 106 | * @param options optional options if an image will be decoded to a Bitmap 107 | * @return decoded {@link Drawable} 108 | * @throws IOException on error 109 | * @throws NullPointerException if the data byte array is null 110 | */ 111 | @NonNull 112 | public static Drawable decodeByteArrayOrThrow(@Nullable final Resources res, 113 | final byte[] data, 114 | @Nullable final BitmapFactory.Options options) throws IOException { 115 | if (data == null) { 116 | throw new NullPointerException("data byte array must not be null"); 117 | } 118 | final boolean animated = isAnimatedGif( 119 | new BufferedInputStream(new ByteArrayInputStream(data))); 120 | if (animated) { 121 | return new GifDrawable(data); 122 | } else { 123 | final Bitmap decoded = BitmapFactory.decodeByteArray(data, 0, data.length, options); 124 | if (decoded == null) { 125 | throw new IOException("BitmapFactory returned null"); 126 | } 127 | return new BitmapDrawable(res, decoded); 128 | } 129 | } 130 | 131 | /** 132 | * Decodes image from InputStream. 133 | * Returns {@link GifDrawable} if the image is an animated GIF. 134 | * Returns {@link BitmapDrawable} if the image is s valid static image {@link BitmapFactory} 135 | * can decode. 136 | * Returns null on error. 137 | * 138 | * @param res Resources to use if creating a BitmapDrawable 139 | * @param is The input stream that holds the raw data to be decoded into a Drawable 140 | * @return decoded {@link Drawable} or null on error 141 | * @throws NullPointerException if the InputStream is null 142 | */ 143 | @Nullable 144 | public static Drawable decodeStream(@Nullable final Resources res, final InputStream is) { 145 | return decodeStream(res, is, null, null); 146 | } 147 | 148 | /** 149 | * Decodes image from InputStream. 150 | * Returns {@link GifDrawable} if the image is an animated GIF. 151 | * Returns {@link BitmapDrawable} if the image is s valid static image {@link BitmapFactory} 152 | * can decode. 153 | * Returns null on error. 154 | * 155 | * @param res Resources to use if creating a BitmapDrawable 156 | * @param is The input stream that holds the raw data to be decoded into a drawable 157 | * @param outPadding optional outPadding if an image will be decoded to a Bitmap 158 | * @param options optional options if an image will be decoded to a Bitmap 159 | * @return decoded {@link Drawable} or null on error 160 | * @throws NullPointerException if the InputStream is null 161 | */ 162 | @Nullable 163 | public static Drawable decodeStream(@Nullable final Resources res, 164 | final InputStream is, 165 | @Nullable final Rect outPadding, 166 | @Nullable final BitmapFactory.Options options) { 167 | try { 168 | return decodeStreamOrThrow(res, is, outPadding, options); 169 | } catch (IOException e) { 170 | Log.w(TAG, "decodeStream: " + e); 171 | return null; 172 | } 173 | } 174 | 175 | /** 176 | * Decodes image from InputStream. 177 | * Returns {@link GifDrawable} if the image is an animated GIF. 178 | * Returns {@link BitmapDrawable} if the image is s valid static image {@link BitmapFactory} 179 | * can decode. 180 | * 181 | * @param res Resources to use if creating a BitmapDrawable 182 | * @param is The input stream that holds the raw data to be decoded into a drawable 183 | * @param outPadding optional outPadding if an image will be decoded to a Bitmap 184 | * @param options optional options if an image will be decoded to a Bitmap 185 | * @return decoded {@link Drawable} 186 | * @throws IOException on error 187 | * @throws NullPointerException if the InputStream is null 188 | */ 189 | @NonNull 190 | public static Drawable decodeStreamOrThrow(@Nullable final Resources res, 191 | final InputStream is, 192 | @Nullable final Rect outPadding, 193 | @Nullable final BitmapFactory.Options options) throws IOException { 194 | if (is == null) { 195 | throw new NullPointerException("InputStream must not be null"); 196 | } 197 | 198 | // android-gif-drawable gives libc crash or not decoding properly if it's not a BufferedInputStream. 199 | final BufferedInputStream bis; 200 | if (is instanceof BufferedInputStream) { 201 | bis = (BufferedInputStream) is; 202 | } else { 203 | bis = new BufferedInputStream(is); 204 | } 205 | bis.mark(Integer.MAX_VALUE); 206 | final boolean animated = isAnimatedGif(bis); 207 | bis.reset(); 208 | if (animated) { 209 | return new GifDrawable(bis); 210 | } else { 211 | final Bitmap decoded = BitmapFactory.decodeStream(is, outPadding, options); 212 | if (decoded == null) { 213 | throw new IOException("BitmapFactory returned null"); 214 | } 215 | return new BitmapDrawable(res, decoded); 216 | } 217 | } 218 | 219 | /** 220 | * Decodes image from file path. 221 | * Returns {@link GifDrawable} if the image is an animated GIF. 222 | * Returns {@link BitmapDrawable} if the image is s valid static image {@link BitmapFactory} 223 | * can decode. 224 | * Returns null on error. 225 | * 226 | * @param res Resources to use if creating a BitmapDrawable 227 | * @param filePath complete path for the file to be decoded. 228 | * @return decoded {@link Drawable} or null on error 229 | * @throws NullPointerException if the file path is null 230 | * @throws IllegalArgumentException if the file path is empty 231 | */ 232 | @Nullable 233 | public static Drawable decodeFile(@Nullable final Resources res, final String filePath) { 234 | return decodeFile(res, filePath, null); 235 | } 236 | 237 | /** 238 | * Decodes image from file path. 239 | * Returns {@link GifDrawable} if the image is an animated GIF. 240 | * Returns {@link BitmapDrawable} if the image is s valid static image {@link BitmapFactory} 241 | * can decode. 242 | * Returns null on error. 243 | * 244 | * @param res Resources to use if creating a BitmapDrawable 245 | * @param filePath complete path for the file to be decoded. 246 | * @param options optional options if an image will be decoded to a Bitmap 247 | * @return decoded {@link Drawable} or null on error 248 | * @throws NullPointerException if the file path is null 249 | * @throws IllegalArgumentException if the file path is empty 250 | */ 251 | @Nullable 252 | public static Drawable decodeFile(@Nullable final Resources res, 253 | final String filePath, 254 | @Nullable final BitmapFactory.Options options) { 255 | try { 256 | return decodeFileOrThrow(res, filePath, options); 257 | } catch (IOException e) { 258 | Log.w(TAG, "decodeFile: " + e); 259 | return null; 260 | } 261 | } 262 | 263 | /** 264 | * Decodes image from file path. 265 | * Returns {@link GifDrawable} if the image is an animated GIF. 266 | * Returns {@link BitmapDrawable} if the image is s valid static image {@link BitmapFactory} 267 | * can decode. 268 | * 269 | * @param res Resources to use if creating a BitmapDrawable 270 | * @param filePath complete path for the file to be decoded. 271 | * @param options optional options if an image will be decoded to a Bitmap 272 | * @return decoded {@link Drawable} 273 | * @throws IOException on error 274 | * @throws NullPointerException if the file path is null 275 | * @throws IllegalArgumentException if the file path is empty 276 | */ 277 | @NonNull 278 | public static Drawable decodeFileOrThrow(@Nullable final Resources res, 279 | final String filePath, 280 | @Nullable final BitmapFactory.Options options) throws IOException { 281 | if (filePath == null) { 282 | throw new NullPointerException("filePath must not be null"); 283 | } 284 | if (filePath.length() == 0) { 285 | throw new IllegalArgumentException("filePath must not be empty"); 286 | } 287 | return decodeStreamOrThrow(res, new FileInputStream(filePath), null, options); 288 | } 289 | 290 | /** 291 | * Decodes image from FileDescriptor. 292 | * Returns {@link GifDrawable} if the image is an animated GIF. 293 | * Returns {@link BitmapDrawable} if the image is s valid static image {@link BitmapFactory} 294 | * can decode. 295 | * Returns null on error. 296 | * 297 | * @param res Resources to use if creating a BitmapDrawable 298 | * @param fd The file descriptor containing the data to decode 299 | * @return decoded {@link Drawable} or null on error 300 | * @throws NullPointerException if FileDescriptor is null 301 | */ 302 | @Nullable 303 | public static Drawable decodeFileDescriptor(@Nullable final Resources res, 304 | final FileDescriptor fd) { 305 | return decodeFileDescriptor(res, fd, null, null); 306 | } 307 | 308 | /** 309 | * Decodes image from FileDescriptor. 310 | * Returns {@link GifDrawable} if the image is an animated GIF. 311 | * Returns {@link BitmapDrawable} if the image is s valid static image {@link BitmapFactory} 312 | * can decode. 313 | * Returns null on error. 314 | * 315 | * @param res Resources to use if creating a BitmapDrawable 316 | * @param fd The file descriptor containing the data to decode 317 | * @param outPadding optional outPadding if an image will be decoded as Bitmap 318 | * @param options optional options if an image will be decoded to a Bitmap 319 | * @return decoded {@link Drawable} or null on error 320 | * @throws NullPointerException if FileDescriptor is null 321 | */ 322 | @Nullable 323 | public static Drawable decodeFileDescriptor(@Nullable final Resources res, 324 | final FileDescriptor fd, 325 | @Nullable final Rect outPadding, 326 | @Nullable final BitmapFactory.Options options) { 327 | try { 328 | return decodeFileDescriptorOrThrow(res, fd, outPadding, options); 329 | } catch (IOException e) { 330 | Log.w(TAG, "decodeFileDescriptor() " + e); 331 | return null; 332 | } 333 | } 334 | 335 | /** 336 | * Decodes image from FileDescriptor. 337 | * Returns {@link GifDrawable} if the image is an animated GIF. 338 | * Returns {@link BitmapDrawable} if the image is s valid static image {@link BitmapFactory} 339 | * can decode. 340 | * 341 | * @param res Resources to use if creating a BitmapDrawable 342 | * @param fd The file descriptor containing the data to decode 343 | * @param outPadding optional outPadding if an image will be decoded as Bitmap 344 | * @param options optional options if an image will be decoded to a Bitmap 345 | * @return decoded {@link Drawable} 346 | * @throws IOException on error 347 | * @throws NullPointerException if FileDescriptor is null 348 | */ 349 | @NonNull 350 | public static Drawable decodeFileDescriptorOrThrow(@Nullable final Resources res, 351 | final FileDescriptor fd, 352 | @Nullable final Rect outPadding, 353 | @Nullable final BitmapFactory.Options options) throws IOException { 354 | if (fd == null) { 355 | throw new NullPointerException("FileDescriptor must not be null"); 356 | } 357 | return decodeStreamOrThrow(res, new FileInputStream(fd), outPadding, options); 358 | } 359 | 360 | /** 361 | * Decodes image from resource. 362 | * Returns {@link GifDrawable} if the image is an animated GIF. 363 | * Returns {@link BitmapDrawable} if the image is s valid static image {@link BitmapFactory} 364 | * can decode. 365 | * Returns null on error. 366 | * 367 | * @param res The resources object containing the image data 368 | * @param id The resource id of the image data 369 | * @return decoded {@link Drawable} or null on error 370 | * @throws NullPointerException if Resources is null 371 | * @throws Resources.NotFoundException if resource under the given id does not exist 372 | */ 373 | @Nullable 374 | public static Drawable decodeResource(final Resources res, @DrawableRes @RawRes final int id) { 375 | return decodeResource(res, id, null); 376 | } 377 | 378 | /** 379 | * Decodes image from resource. 380 | * Returns {@link GifDrawable} if the image is an animated GIF. 381 | * Returns {@link BitmapDrawable} if the image is s valid static image {@link BitmapFactory} 382 | * can decode. 383 | * Returns null on error. 384 | * 385 | * @param res The resources object containing the image data 386 | * @param id The resource id of the image data 387 | * @param options optional options if an image will be decoded to a Bitmap 388 | * @return decoded {@link Drawable} or null on error 389 | * @throws NullPointerException if Resources is null 390 | * @throws Resources.NotFoundException if resource under the given id does not exist 391 | */ 392 | @Nullable 393 | public static Drawable decodeResource(final Resources res, @DrawableRes @RawRes final int id, 394 | @Nullable final BitmapFactory.Options options) { 395 | try { 396 | return decodeResourceOrThrow(res, id, options); 397 | } catch (IOException e) { 398 | Log.w(TAG, "decodeResource(): " + e); 399 | return null; 400 | } 401 | } 402 | 403 | /** 404 | * Decodes image from resource. 405 | * Returns {@link GifDrawable} if the image is an animated GIF. 406 | * Returns {@link BitmapDrawable} if the image is s valid static image {@link BitmapFactory} 407 | * can decode. 408 | * 409 | * @param res The resources object containing the image data 410 | * @param id The resource id of the image data 411 | * @param options optional options if an image will be decoded to a Bitmap 412 | * @return decoded {@link Drawable} 413 | * @throws IOException on error 414 | * @throws NullPointerException if Resources is null 415 | * @throws Resources.NotFoundException if resource under the given id does not exist 416 | */ 417 | @NonNull 418 | public static Drawable decodeResourceOrThrow(final Resources res, 419 | @DrawableRes @RawRes final int id, 420 | @Nullable final BitmapFactory.Options options) throws IOException { 421 | if (res == null) { 422 | throw new NullPointerException("Resources must not be null"); 423 | } 424 | 425 | final AssetFileDescriptor descriptor = res.openRawResourceFd(id); 426 | try { 427 | return decodeStreamOrThrow(res, descriptor.createInputStream(), null, options); 428 | } finally { 429 | descriptor.close(); 430 | } 431 | } 432 | 433 | /** 434 | * Detects animated GIF. 435 | * 436 | * @param is InputStream pointing to data to analyze 437 | * @return true, if the reader's content is an animated gif. False if not a gif or not animated 438 | */ 439 | @SuppressWarnings("ResultOfMethodCallIgnored") 440 | public static boolean isAnimatedGif(@NonNull final BufferedInputStream is) 441 | throws IOException { 442 | final byte h1 = (byte) is.read(); 443 | final byte h2 = (byte) is.read(); 444 | final byte h3 = (byte) is.read(); 445 | 446 | //False inspection. Why? 447 | //noinspection ConstantConditions 448 | if (h1 != 'G' || h2 != 'I' || h3 != 'F') { 449 | return false; 450 | } 451 | 452 | final byte v1 = (byte) is.read(); 453 | final byte v2 = (byte) is.read(); 454 | final byte v3 = (byte) is.read(); 455 | 456 | if (v1 != '8' || (v2 != '7' && v2 != '9') || v3 != 'a') { 457 | return false; 458 | } 459 | 460 | is.skip(2); // logical screen width 461 | is.skip(2); // logical screen height 462 | 463 | // read as unsigned int 8 464 | final short flags = (short) (is.read() & 0xFF); 465 | 466 | // First three bits = (BPP - 1) 467 | final int colorTableSize = 1 << ((flags & 7) + 1); 468 | 469 | // 89a 470 | // if (v2 == '9') { 471 | // boolean isColorTableSorted = (flags & 8) != 0; 472 | // } 473 | 474 | //final int bitsPerPixel = ((flags & 0x70) >> 4) + 1; 475 | final boolean hasGlobalColorTable = (flags & 0xf) != 0; 476 | 477 | is.skip(1); // background color index 478 | is.skip(1); // aspect ratio byte 479 | 480 | if (hasGlobalColorTable) { 481 | is.skip(colorTableSize * 3); 482 | } 483 | 484 | while (true) { 485 | int code = is.read() & 0xff; 486 | switch (code) { 487 | case 0x2c: 488 | // an image block 489 | return false; 490 | 491 | case 0x21: 492 | // extension 493 | code = is.read() & 0xff; 494 | switch (code) { 495 | case 0xf9: 496 | return true; 497 | 498 | case 0xff: // application extension 499 | return true; 500 | 501 | case 0xfe:// comment extension 502 | skip(is); 503 | break; 504 | 505 | case 0x01:// plain text extension 506 | skip(is); 507 | break; 508 | 509 | default: // uninteresting extension 510 | skip(is); 511 | break; 512 | } 513 | break; 514 | 515 | case 0x3b: // terminator 516 | default: 517 | return false; 518 | } 519 | } 520 | } 521 | 522 | /** 523 | * Skips variable length blocks up to and including next zero length block. 524 | */ 525 | 526 | private static void skip(@NonNull final BufferedInputStream reader) throws IOException { 527 | int blockSize; 528 | do { 529 | blockSize = reader.read() & 0xff; 530 | if (blockSize > 0) { 531 | try { 532 | int n = 0; 533 | int count; 534 | while (n < blockSize) { 535 | count = blockSize - n; 536 | n += reader.skip(count); 537 | } 538 | } catch (Exception e) { 539 | Log.w(TAG, "Error Reading Block", e); 540 | throw new IOException("Format error " + e); 541 | } 542 | } 543 | } while ((blockSize > 0)); 544 | } 545 | } 546 | -------------------------------------------------------------------------------- /sample/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /sample/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 22 5 | buildToolsVersion "22.0.1" 6 | 7 | defaultConfig { 8 | applicationId "com.doctoror.imagefactory.sample" 9 | minSdkVersion 8 10 | targetSdkVersion 22 11 | versionCode 1 12 | versionName "1.0.1" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | compile project(':library') 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | compile 'com.github.doctoror.aspectratiolayout:library:1.0.1' 25 | compile 'com.android.support:appcompat-v7:22.0.0' 26 | } 27 | -------------------------------------------------------------------------------- /sample/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 8 | 9 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /sample/src/main/assets/LoopOnce.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doctoror/ImageFactory/355b291bbea9d475d5d6b88d719c0c81341fa379/sample/src/main/assets/LoopOnce.gif -------------------------------------------------------------------------------- /sample/src/main/assets/Rotating_earth_(large).gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doctoror/ImageFactory/355b291bbea9d475d5d6b88d719c0c81341fa379/sample/src/main/assets/Rotating_earth_(large).gif -------------------------------------------------------------------------------- /sample/src/main/assets/Static_earth.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doctoror/ImageFactory/355b291bbea9d475d5d6b88d719c0c81341fa379/sample/src/main/assets/Static_earth.gif -------------------------------------------------------------------------------- /sample/src/main/assets/ru9gag.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doctoror/ImageFactory/355b291bbea9d475d5d6b88d719c0c81341fa379/sample/src/main/assets/ru9gag.gif -------------------------------------------------------------------------------- /sample/src/main/assets/ru9gag1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doctoror/ImageFactory/355b291bbea9d475d5d6b88d719c0c81341fa379/sample/src/main/assets/ru9gag1.gif -------------------------------------------------------------------------------- /sample/src/main/assets/ru9gag3.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doctoror/ImageFactory/355b291bbea9d475d5d6b88d719c0c81341fa379/sample/src/main/assets/ru9gag3.gif -------------------------------------------------------------------------------- /sample/src/main/assets/ru9gag4.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doctoror/ImageFactory/355b291bbea9d475d5d6b88d719c0c81341fa379/sample/src/main/assets/ru9gag4.gif -------------------------------------------------------------------------------- /sample/src/main/assets/small.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doctoror/ImageFactory/355b291bbea9d475d5d6b88d719c0c81341fa379/sample/src/main/assets/small.gif -------------------------------------------------------------------------------- /sample/src/main/assets/smallest.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doctoror/ImageFactory/355b291bbea9d475d5d6b88d719c0c81341fa379/sample/src/main/assets/smallest.gif -------------------------------------------------------------------------------- /sample/src/main/java/com/doctoror/imagefactory/sample/BaseAdapter2.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Yaroslav Mytkalyk 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.doctoror.imagefactory.sample; 18 | 19 | import android.content.Context; 20 | import android.support.annotation.NonNull; 21 | import android.support.annotation.Nullable; 22 | import android.view.LayoutInflater; 23 | import android.widget.BaseAdapter; 24 | 25 | import java.util.ArrayList; 26 | import java.util.Arrays; 27 | import java.util.List; 28 | 29 | /** 30 | * Extended {@link BaseAdapter} which contains items {@link List}, {@link Context} and {@link 31 | * LayoutInflater}. 32 | */ 33 | public abstract class BaseAdapter2 extends BaseAdapter { 34 | 35 | private final ArrayList mItems = new ArrayList<>(); 36 | 37 | @NonNull 38 | private final Context mContext; 39 | 40 | @NonNull 41 | private final LayoutInflater mLayoutInflater; 42 | 43 | public BaseAdapter2(@NonNull final Context context) { 44 | this(context, (List) null); 45 | } 46 | 47 | public BaseAdapter2(@NonNull final Context context, @Nullable final T[] items) { 48 | this(context, items != null ? Arrays.asList(items) : null); 49 | } 50 | 51 | public BaseAdapter2(@NonNull final Context context, @Nullable final List items) { 52 | mContext = context; 53 | mLayoutInflater = LayoutInflater.from(context); 54 | if (items != null) { 55 | mItems.addAll(items); 56 | } 57 | } 58 | 59 | protected final List getItems() { 60 | return mItems; 61 | } 62 | 63 | public void updateData(@Nullable final List data) { 64 | if (data == null && mItems.isEmpty() || data != null && mItems.equals(data)) { 65 | return; 66 | } 67 | mItems.clear(); 68 | if (data != null) { 69 | mItems.addAll(data); 70 | } 71 | notifyDataSetChanged(); 72 | } 73 | 74 | @NonNull 75 | protected final Context getContext() { 76 | return mContext; 77 | } 78 | 79 | @NonNull 80 | protected final LayoutInflater getLayoutInflater() { 81 | return mLayoutInflater; 82 | } 83 | 84 | @Override 85 | public int getCount() { 86 | return mItems.size(); 87 | } 88 | 89 | @Override 90 | public boolean isEmpty() { 91 | return mItems.isEmpty(); 92 | } 93 | 94 | @Override 95 | public T getItem(final int position) { 96 | return mItems.get(position); 97 | } 98 | 99 | @Override 100 | public long getItemId(final int position) { 101 | return 0; 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /sample/src/main/java/com/doctoror/imagefactory/sample/DemoActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Yaroslav Mytkalyk 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.doctoror.imagefactory.sample; 18 | 19 | import com.doctoror.imagefactory.ImageFactory; 20 | 21 | import android.content.Context; 22 | import android.content.res.AssetManager; 23 | import android.graphics.drawable.Drawable; 24 | import android.os.Bundle; 25 | import android.support.annotation.NonNull; 26 | import android.support.annotation.Nullable; 27 | import android.support.v7.app.ActionBarActivity; 28 | import android.view.View; 29 | import android.view.ViewGroup; 30 | import android.widget.GridView; 31 | import android.widget.ImageView; 32 | 33 | import java.io.IOException; 34 | import java.io.InputStream; 35 | import java.util.ArrayList; 36 | import java.util.List; 37 | 38 | public final class DemoActivity extends ActionBarActivity { 39 | 40 | @Override 41 | protected void onCreate(Bundle savedInstanceState) { 42 | super.onCreate(savedInstanceState); 43 | setContentView(R.layout.activity_demo); 44 | 45 | final GridView grid = (GridView) findViewById(R.id.activity_demo_grid); 46 | grid.setAdapter(new DemoAdapter(this, generateImageInfo())); 47 | } 48 | 49 | @NonNull 50 | private List generateImageInfo() { 51 | final String[] names = new String[]{ 52 | "Rotating_earth_(large).gif", 53 | "Static_earth.gif", 54 | "LoopOnce.gif", 55 | "small.gif", 56 | "smallest.gif", 57 | "ru9gag.gif", 58 | "ru9gag1.gif", 59 | "ru9gag3.gif", 60 | "ru9gag4.gif" 61 | }; 62 | final List list = new ArrayList<>(names.length); 63 | for (final String name : names) { 64 | InputStream is = null; 65 | try { 66 | is = getAssets().open(name, AssetManager.ACCESS_RANDOM); 67 | final Drawable item = ImageFactory.decodeStream(getResources(), is); 68 | if (item != null) { 69 | list.add(item); 70 | } 71 | } catch (IOException e) { 72 | e.printStackTrace(); 73 | } finally { 74 | if (is != null) { 75 | try { 76 | is.close(); 77 | } catch (IOException e) { 78 | e.printStackTrace(); 79 | } 80 | } 81 | } 82 | } 83 | return list; 84 | } 85 | 86 | private static final class DemoAdapter extends BaseAdapter2 { 87 | 88 | private DemoAdapter(@NonNull final Context context, 89 | @Nullable final List items) { 90 | super(context, items); 91 | } 92 | 93 | @Override 94 | public View getView(final int position, View convertView, final ViewGroup parent) { 95 | final ImageView imageView; 96 | if (convertView == null) { 97 | convertView = getLayoutInflater().inflate(R.layout.grid_item_demo, parent, false); 98 | imageView = (ImageView) convertView.findViewById(R.id.image); 99 | convertView.setTag(imageView); 100 | } else { 101 | imageView = (ImageView) convertView.getTag(); 102 | } 103 | 104 | imageView.setImageDrawable(getItem(position)); 105 | return convertView; 106 | } 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /sample/src/main/res/layout/activity_demo.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 14 | 15 | 22 | 23 | -------------------------------------------------------------------------------- /sample/src/main/res/layout/grid_item_demo.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 23 | 24 | -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doctoror/ImageFactory/355b291bbea9d475d5d6b88d719c0c81341fa379/sample/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doctoror/ImageFactory/355b291bbea9d475d5d6b88d719c0c81341fa379/sample/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doctoror/ImageFactory/355b291bbea9d475d5d6b88d719c0c81341fa379/sample/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doctoror/ImageFactory/355b291bbea9d475d5d6b88d719c0c81341fa379/sample/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | ImageFactorySample 3 | 4 | Note: second Earth is static to display how ImageFactory loads static images, see assets and source code for details.\nThe counter stops on "GO!" because loop count == 1 5 | 6 | 7 | -------------------------------------------------------------------------------- /sample/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':library', ':sample' 2 | --------------------------------------------------------------------------------