├── app ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── values │ │ │ │ ├── strings.xml │ │ │ │ ├── colors.xml │ │ │ │ └── styles.xml │ │ │ ├── drawable-xhdpi │ │ │ │ ├── baidu.png │ │ │ │ ├── emoji_cry.gif │ │ │ │ ├── emoji_doubt.gif │ │ │ │ ├── emoji_giggle.gif │ │ │ │ ├── emoji_appearance.gif │ │ │ │ ├── emoji_heartstopper.gif │ │ │ │ └── emoji_heartstopper_n.gif │ │ │ ├── mipmap-hdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-mdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxxhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-anydpi-v26 │ │ │ │ ├── ic_launcher.xml │ │ │ │ └── ic_launcher_round.xml │ │ │ ├── layout │ │ │ │ ├── photo_gallery_item_photo_show.xml │ │ │ │ ├── activity_main.xml │ │ │ │ └── photo_gallery_activity_main.xml │ │ │ ├── drawable-v24 │ │ │ │ └── ic_launcher_foreground.xml │ │ │ └── drawable │ │ │ │ └── ic_launcher_background.xml │ │ ├── AndroidManifest.xml │ │ └── java │ │ │ └── gallery │ │ │ └── demo │ │ │ └── com │ │ │ └── galleryview │ │ │ └── MainActivity.java │ ├── test │ │ └── java │ │ │ └── gallery │ │ │ └── demo │ │ │ └── com │ │ │ └── galleryview │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── gallery │ │ └── demo │ │ └── com │ │ └── galleryview │ │ └── ExampleInstrumentedTest.java ├── proguard-rules.pro └── build.gradle ├── gallery ├── .gitignore ├── src │ └── main │ │ ├── AndroidManifest.xml │ │ ├── java │ │ └── gallery │ │ │ └── demo │ │ │ └── com │ │ │ └── gallery │ │ │ ├── util │ │ │ ├── GalleryDownLoadCallBack.java │ │ │ ├── GalleryScreenUtil.java │ │ │ └── GalleryDownloadThread.java │ │ │ ├── model │ │ │ ├── GalleryPhotoModel.java │ │ │ └── GalleryPhotoParameterModel.java │ │ │ ├── view │ │ │ ├── GalleryPhotoView.java │ │ │ ├── GalleryViewPager.java │ │ │ └── GalleryView.java │ │ │ └── adapter │ │ │ └── GalleryPhotoAdapter.java │ │ └── res │ │ ├── values │ │ ├── strings.xml │ │ └── attrs.xml │ │ └── layout │ │ └── gallery_view_main.xml ├── proguard-rules.pro └── build.gradle ├── settings.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── .idea ├── vcs.xml ├── modules.xml ├── runConfigurations.xml ├── gradle.xml └── misc.xml ├── gradle.properties ├── gradlew.bat ├── gradlew └── README.md /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /gallery/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':gallery' 2 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | GalleryView 3 | 4 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cedear/GalleryView/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/baidu.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cedear/GalleryView/HEAD/app/src/main/res/drawable-xhdpi/baidu.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cedear/GalleryView/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cedear/GalleryView/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/emoji_cry.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cedear/GalleryView/HEAD/app/src/main/res/drawable-xhdpi/emoji_cry.gif -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/emoji_doubt.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cedear/GalleryView/HEAD/app/src/main/res/drawable-xhdpi/emoji_doubt.gif -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cedear/GalleryView/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cedear/GalleryView/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cedear/GalleryView/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/emoji_giggle.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cedear/GalleryView/HEAD/app/src/main/res/drawable-xhdpi/emoji_giggle.gif -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/emoji_appearance.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cedear/GalleryView/HEAD/app/src/main/res/drawable-xhdpi/emoji_appearance.gif -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cedear/GalleryView/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cedear/GalleryView/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cedear/GalleryView/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cedear/GalleryView/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /gallery/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/emoji_heartstopper.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cedear/GalleryView/HEAD/app/src/main/res/drawable-xhdpi/emoji_heartstopper.gif -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cedear/GalleryView/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/emoji_heartstopper_n.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cedear/GalleryView/HEAD/app/src/main/res/drawable-xhdpi/emoji_heartstopper_n.gif -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /gallery/src/main/java/gallery/demo/com/gallery/util/GalleryDownLoadCallBack.java: -------------------------------------------------------------------------------- 1 | package gallery.demo.com.gallery.util; 2 | 3 | public interface GalleryDownLoadCallBack { 4 | 5 | void onDownLoadSuccess(); 6 | 7 | void onDownLoadFailed(); 8 | 9 | } 10 | -------------------------------------------------------------------------------- /gallery/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | gallery 3 | 保存 4 | 写存储卡权限申请失败, 无法保存图片 5 | 6 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Mar 26 13:54:27 CST 2018 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip 7 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /gallery/src/main/res/values/attrs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /gallery/src/main/java/gallery/demo/com/gallery/model/GalleryPhotoModel.java: -------------------------------------------------------------------------------- 1 | package gallery.demo.com.gallery.model; 2 | 3 | import android.support.annotation.DrawableRes; 4 | 5 | 6 | public class GalleryPhotoModel { 7 | 8 | public Object photoSource; 9 | 10 | public GalleryPhotoModel(@DrawableRes int drawableRes) { 11 | this.photoSource = drawableRes; 12 | } 13 | 14 | public GalleryPhotoModel(String path) { 15 | this.photoSource = path; 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/test/java/gallery/demo/com/galleryview/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package gallery.demo.com.galleryview; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() throws Exception { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /gallery/src/main/java/gallery/demo/com/gallery/model/GalleryPhotoParameterModel.java: -------------------------------------------------------------------------------- 1 | package gallery.demo.com.gallery.model; 2 | 3 | import android.widget.ImageView; 4 | 5 | public class GalleryPhotoParameterModel { 6 | 7 | //索引 8 | public int index; 9 | // 图片的类型 10 | public Object photoObj; 11 | // 在屏幕上的位置 12 | public int[] locOnScreen = new int[]{-1, -1}; 13 | // 图片的宽 14 | public int photoWidth = 0; 15 | // 图片的高 16 | public int photoHeight = 0; 17 | // ImageView的宽 18 | public int imageWidth = 0; 19 | // ImageView的高 20 | public int imageHeight = 0; 21 | // 22 | public ImageView.ScaleType scaleType; 23 | 24 | } 25 | -------------------------------------------------------------------------------- /app/src/main/res/layout/photo_gallery_item_photo_show.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 11 | 12 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 18 | 19 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | org.gradle.jvmargs=-Xmx1536m 13 | 14 | # When configured, Gradle will run in incubating parallel mode. 15 | # This option should only be used with decoupled projects. More details, visit 16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 17 | # org.gradle.parallel=true 18 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /gallery/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /gallery/src/main/java/gallery/demo/com/gallery/view/GalleryPhotoView.java: -------------------------------------------------------------------------------- 1 | package gallery.demo.com.gallery.view; 2 | 3 | import android.content.Context; 4 | import android.graphics.Canvas; 5 | 6 | import com.bumptech.glide.Glide; 7 | import com.github.chrisbanes.photoview.PhotoView; 8 | 9 | import gallery.demo.com.gallery.model.GalleryPhotoModel; 10 | 11 | public class GalleryPhotoView extends PhotoView { 12 | 13 | private GalleryPhotoModel photoModel; 14 | 15 | public GalleryPhotoView(Context context, GalleryPhotoModel photoModel) { 16 | super(context); 17 | this.photoModel = photoModel; 18 | } 19 | 20 | public void startGlide() { 21 | Glide.with(getContext()).load(photoModel.photoSource).into(GalleryPhotoView.this); 22 | } 23 | 24 | @Override 25 | protected void onDraw(Canvas canvas) { 26 | super.onDraw(canvas); 27 | 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /app/src/androidTest/java/gallery/demo/com/galleryview/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package gallery.demo.com.galleryview; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumented test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("gallery.demo.com.galleryview", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 26 5 | defaultConfig { 6 | applicationId "gallery.demo.com.galleryview" 7 | minSdkVersion 24 8 | targetSdkVersion 26 9 | versionCode 1 10 | versionName "1.0" 11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | compileOptions { 20 | sourceCompatibility JavaVersion.VERSION_1_8 21 | targetCompatibility JavaVersion.VERSION_1_8 22 | } 23 | } 24 | 25 | dependencies { 26 | implementation fileTree(include: ['*.jar'], dir: 'libs') 27 | implementation 'com.android.support:appcompat-v7:26.1.0' 28 | implementation 'com.android.support.constraint:constraint-layout:1.0.2' 29 | testImplementation 'junit:junit:4.12' 30 | androidTestImplementation 'com.android.support.test:runner:1.0.1' 31 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' 32 | compile project(path: ':gallery') 33 | } 34 | -------------------------------------------------------------------------------- /gallery/src/main/java/gallery/demo/com/gallery/view/GalleryViewPager.java: -------------------------------------------------------------------------------- 1 | package gallery.demo.com.gallery.view; 2 | 3 | import android.content.Context; 4 | import android.support.v4.view.ViewPager; 5 | import android.util.AttributeSet; 6 | import android.view.MotionEvent; 7 | 8 | public class GalleryViewPager extends ViewPager { 9 | 10 | public GalleryViewPager(Context context) { 11 | super(context); 12 | } 13 | 14 | public GalleryViewPager(Context context, AttributeSet attrs) { 15 | super(context, attrs); 16 | } 17 | 18 | private boolean mIsDisallowIntercept = false; 19 | @Override 20 | public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) { 21 | // keep the info about if the innerViews do 22 | // requestDisallowInterceptTouchEvent 23 | mIsDisallowIntercept = disallowIntercept; 24 | super.requestDisallowInterceptTouchEvent(disallowIntercept); 25 | } 26 | 27 | @Override 28 | public boolean dispatchTouchEvent(MotionEvent ev) { 29 | // the incorrect array size will only happen in the multi-touch 30 | // scenario. 31 | if (ev.getPointerCount() > 1 && mIsDisallowIntercept) { 32 | requestDisallowInterceptTouchEvent(false); 33 | boolean handled = super.dispatchTouchEvent(ev); 34 | requestDisallowInterceptTouchEvent(true); 35 | return handled; 36 | } else { 37 | return super.dispatchTouchEvent(ev); 38 | } 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /gallery/src/main/java/gallery/demo/com/gallery/util/GalleryScreenUtil.java: -------------------------------------------------------------------------------- 1 | package gallery.demo.com.gallery.util; 2 | 3 | import android.content.Context; 4 | import android.graphics.RectF; 5 | import android.util.DisplayMetrics; 6 | import android.view.WindowManager; 7 | 8 | import java.lang.reflect.Field; 9 | 10 | public class GalleryScreenUtil { 11 | 12 | /** 13 | * @return 整个屏幕的坐标 14 | */ 15 | public static RectF getDisplayPixes(Context context) { 16 | DisplayMetrics metrics = new DisplayMetrics(); 17 | WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 18 | manager.getDefaultDisplay().getMetrics(metrics); 19 | return new RectF(0, 0, metrics.widthPixels, metrics.heightPixels); 20 | } 21 | 22 | /** 23 | * 用于获取状态栏的高度。 24 | * 25 | * @return 返回状态栏高度的像素值。 26 | */ 27 | public static int getStatusBarHeight(Context context) { 28 | int statusBarHeight = 0; 29 | if (context != null) { 30 | try { 31 | Class c = Class.forName("com.android.internal.R$dimen"); 32 | Object o = c.newInstance(); 33 | Field field = c.getField("status_bar_height"); 34 | int x = (Integer) field.get(o); 35 | statusBarHeight = context.getResources().getDimensionPixelSize(x); 36 | } catch (Exception e) { 37 | e.printStackTrace(); 38 | } 39 | } 40 | return statusBarHeight; 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /gallery/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 26 5 | 6 | 7 | 8 | defaultConfig { 9 | minSdkVersion 24 10 | targetSdkVersion 26 11 | versionCode 1 12 | versionName "1.0" 13 | 14 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 15 | 16 | } 17 | 18 | buildTypes { 19 | release { 20 | minifyEnabled false 21 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 22 | } 23 | } 24 | 25 | compileOptions { 26 | sourceCompatibility JavaVersion.VERSION_1_8 27 | targetCompatibility JavaVersion.VERSION_1_8 28 | } 29 | } 30 | 31 | dependencies { 32 | implementation fileTree(include: ['*.jar'], dir: 'libs') 33 | implementation 'com.android.support:appcompat-v7:26.1.0' 34 | testImplementation 'junit:junit:4.12' 35 | androidTestImplementation 'com.android.support.test:runner:1.0.1' 36 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' 37 | compile 'com.github.chrisbanes:PhotoView:2.1.3' 38 | //glide库 39 | compile 'com.github.bumptech.glide:glide:4.0.0-RC0' 40 | //这个---- 41 | compile 'com.android.support:support-v4:25.3.1' 42 | //这个用于我们自定义GlideModule的注解 43 | annotationProcessor 'com.github.bumptech.glide:compiler:4.0.0-RC0' 44 | //glide默认是httpconnection,加这个是换成okhttp 45 | compile 'com.github.bumptech.glide:okhttp3-integration:4.0.0-RC0' 46 | //权限申请 47 | //RxPermissions 48 | compile 'com.tbruyelle.rxpermissions2:rxpermissions:0.9.3@aar' 49 | //RxJava2 50 | compile 'io.reactivex.rxjava2:rxjava:2.0.0' 51 | } 52 | -------------------------------------------------------------------------------- /gallery/src/main/java/gallery/demo/com/gallery/adapter/GalleryPhotoAdapter.java: -------------------------------------------------------------------------------- 1 | package gallery.demo.com.gallery.adapter; 2 | 3 | import android.support.v4.view.PagerAdapter; 4 | import android.view.View; 5 | import android.view.ViewGroup; 6 | 7 | import java.util.ArrayList; 8 | import java.util.List; 9 | 10 | import gallery.demo.com.gallery.view.GalleryPhotoView; 11 | 12 | /** 13 | * Created by bjhl on 2018/3/26. 14 | */ 15 | 16 | public class GalleryPhotoAdapter extends PagerAdapter { 17 | 18 | private List viewList = new ArrayList<>(); 19 | private List positions = new ArrayList<>(); 20 | public GalleryPhotoAdapter(List list) { 21 | if(list != null) { 22 | viewList.addAll(list); 23 | } 24 | } 25 | 26 | @Override 27 | public int getCount() { 28 | return this.viewList.size(); 29 | } 30 | 31 | @Override 32 | public boolean isViewFromObject(View view, Object object) { 33 | return view == object; 34 | } 35 | 36 | @Override 37 | public Object instantiateItem(ViewGroup container, int position) { 38 | if (!positions.contains(position)) { 39 | positions.add(position); 40 | //创建一个新的item 41 | viewList.get(position).startGlide(); 42 | } 43 | container.addView(viewList.get(position)); 44 | return viewList.get(position); 45 | } 46 | 47 | @Override 48 | public void destroyItem(ViewGroup container, int position, Object object) { 49 | container.removeView(viewList.get(position)); 50 | } 51 | 52 | @Override 53 | public int getItemPosition(Object object) { 54 | return POSITION_NONE; 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /gallery/src/main/res/layout/gallery_view_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 11 | 12 | 13 | 16 | 17 | 18 | 23 | 24 | 25 | 26 | 31 | 32 | 42 | 43 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 16 | 26 | 27 | 28 | 29 | 30 | 31 | 33 | 34 | 35 | 36 | 37 | 1.8 38 | 39 | 44 | 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /app/src/main/res/layout/photo_gallery_activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 12 | 13 | 16 | 17 | 26 | 27 | 35 | 36 | 46 | 47 | 55 | 56 | 57 | 58 | 59 | 60 | 68 | 69 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /gallery/src/main/java/gallery/demo/com/gallery/util/GalleryDownloadThread.java: -------------------------------------------------------------------------------- 1 | package gallery.demo.com.gallery.util; 2 | 3 | import android.app.Activity; 4 | import android.content.Context; 5 | import android.content.Intent; 6 | import android.database.Cursor; 7 | import android.graphics.Bitmap; 8 | import android.media.MediaScannerConnection; 9 | import android.net.Uri; 10 | import android.os.Environment; 11 | import android.provider.MediaStore; 12 | import android.widget.Toast; 13 | 14 | import com.bumptech.glide.Glide; 15 | import com.bumptech.glide.request.target.Target; 16 | 17 | import java.util.concurrent.ExecutionException; 18 | 19 | public class GalleryDownloadThread implements Runnable{ 20 | 21 | 22 | private Object photoObj; 23 | private Context context; 24 | private GalleryDownLoadCallBack callBack; 25 | private boolean isSucceed; 26 | 27 | public GalleryDownloadThread(Context context, Object photoObj, GalleryDownLoadCallBack callBack) { 28 | this.context = context; 29 | this.photoObj = photoObj; 30 | this.callBack = callBack; 31 | } 32 | 33 | 34 | @Override 35 | public void run() { 36 | Bitmap bitmap = null; 37 | try { 38 | bitmap = Glide.with(context) 39 | .asBitmap() 40 | .load(photoObj) 41 | .into(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) 42 | .get(); 43 | if (bitmap != null) { 44 | saveImageToGallery(context, bitmap); 45 | } 46 | } catch (InterruptedException e) { 47 | e.printStackTrace(); 48 | } catch (ExecutionException e) { 49 | e.printStackTrace(); 50 | } finally { 51 | if (bitmap != null && isSucceed) { 52 | callBack.onDownLoadSuccess(); 53 | } else { 54 | callBack.onDownLoadFailed(); 55 | } 56 | } 57 | } 58 | 59 | 60 | public void saveImageToGallery(final Context context, final Bitmap bitmap) { 61 | //确认有没有SD卡 62 | if (Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) { 63 | String imgName = System.currentTimeMillis() + ".jpg"; 64 | String s = null; 65 | Cursor cursor = null; 66 | try{ 67 | s = MediaStore.Images.Media.insertImage(context.getContentResolver(), bitmap, imgName, null); 68 | String[] proj = {MediaStore.Images.Media.DATA}; 69 | cursor = ((Activity) context).managedQuery(Uri.parse(s), proj, null, null, null); 70 | if (cursor == null) { 71 | context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse(s))); 72 | } else { 73 | //兼容华为4x(型号) 5.0系统问题(保存进文件后,相册并没有刷新) 74 | int actual_image_column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 75 | cursor.moveToFirst(); 76 | String img_path = cursor.getString(actual_image_column_index); 77 | MediaScannerConnection.scanFile(context, new String[]{img_path}, null, null); 78 | } 79 | isSucceed = true; 80 | } catch (Exception e) { 81 | isSucceed = false; 82 | } 83 | } 84 | } 85 | 86 | } 87 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/java/gallery/demo/com/galleryview/MainActivity.java: -------------------------------------------------------------------------------- 1 | package gallery.demo.com.galleryview; 2 | 3 | import android.support.v7.app.AppCompatActivity; 4 | import android.os.Bundle; 5 | import android.view.LayoutInflater; 6 | import android.view.View; 7 | import android.view.ViewGroup; 8 | import android.view.Window; 9 | import android.widget.BaseAdapter; 10 | import android.widget.GridView; 11 | import android.widget.ImageView; 12 | 13 | import com.bumptech.glide.Glide; 14 | 15 | import java.util.ArrayList; 16 | import java.util.List; 17 | 18 | import gallery.demo.com.gallery.model.GalleryPhotoModel; 19 | import gallery.demo.com.gallery.view.GalleryView; 20 | 21 | public class MainActivity extends AppCompatActivity { 22 | 23 | 24 | private GridView urlTypeGridView, drawableTypeGridView; 25 | private GalleryView photoGalleryView; 26 | private LayoutInflater inflater; 27 | 28 | private List urlList = new ArrayList<>(); 29 | private List resourceList = new ArrayList<>(); 30 | 31 | 32 | @Override 33 | protected void onCreate(Bundle savedInstanceState) { 34 | super.onCreate(savedInstanceState); 35 | supportRequestWindowFeature(Window.FEATURE_NO_TITLE); 36 | setContentView(R.layout.photo_gallery_activity_main); 37 | initData(); 38 | initView(); 39 | } 40 | 41 | private void initData() { 42 | inflater = LayoutInflater.from(this); 43 | urlList.add(new GalleryPhotoModel("http://img0.imgtn.bdimg.com/it/u=2715862021,3180817113&fm=27&gp=0.jpg")); 44 | urlList.add(new GalleryPhotoModel("http://img4.imgtn.bdimg.com/it/u=1121460335,1804591133&fm=27&gp=0.jpg")); 45 | urlList.add(new GalleryPhotoModel("http://img5.imgtn.bdimg.com/it/u=3926003122,1701882005&fm=27&gp=0.jpg")); 46 | urlList.add(new GalleryPhotoModel("http://img2.imgtn.bdimg.com/it/u=2956928272,541387468&fm=27&gp=0.jpg")); 47 | urlList.add(new GalleryPhotoModel("http://img3.imgtn.bdimg.com/it/u=3524652925,4234956354&fm=27&gp=0.jpg")); 48 | urlList.add(new GalleryPhotoModel("https://imgs.genshuixue.com/49509748_tkiz8i0z.jpeg")); 49 | 50 | resourceList.add(new GalleryPhotoModel("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1521717658692&di=8897bcf879d0da55db484e293a44ca12&imgtype=0&src=http%3A%2F%2Fimg.redocn.com%2Fsheji%2F20140924%2Fliushiwuzhounianguoqingshuxingzhanban_3128211.jpg")); 51 | resourceList.add(new GalleryPhotoModel(R.drawable.emoji_cry)); 52 | resourceList.add(new GalleryPhotoModel(R.drawable.emoji_doubt)); 53 | resourceList.add(new GalleryPhotoModel(R.drawable.emoji_giggle)); 54 | resourceList.add(new GalleryPhotoModel(R.drawable.emoji_heartstopper)); 55 | resourceList.add(new GalleryPhotoModel(R.drawable.emoji_heartstopper_n)); 56 | } 57 | 58 | 59 | private void initView() { 60 | photoGalleryView = (GalleryView) findViewById(R.id.photo_gallery_view); 61 | urlTypeGridView = (GridView) findViewById(R.id.url_type_gridView); 62 | drawableTypeGridView = (GridView) findViewById(R.id.drawabl_type_gridView); 63 | urlTypeGridView.setAdapter(new BaseAdapter() { 64 | @Override 65 | public int getCount() { 66 | return urlList.size(); 67 | } 68 | 69 | @Override 70 | public Object getItem(int position) { 71 | return urlList.get(position); 72 | } 73 | 74 | @Override 75 | public long getItemId(int position) { 76 | return position; 77 | } 78 | 79 | @Override 80 | public View getView(final int position, View convertView, ViewGroup viewGroup) { 81 | ViewHoder viewHoder = null; 82 | if (convertView == null) { 83 | convertView = inflater.inflate(R.layout.photo_gallery_item_photo_show, null); 84 | viewHoder = new ViewHoder(); 85 | viewHoder.imageView = (ImageView) convertView.findViewById(R.id.item_photo); 86 | convertView.setTag(viewHoder); 87 | } else { 88 | viewHoder = (ViewHoder) convertView.getTag(); 89 | } 90 | Glide.with(MainActivity.this).load(urlList.get(position).photoSource).into(viewHoder.imageView); 91 | final ViewHoder finalViewHoder = viewHoder; 92 | viewHoder.imageView.setOnClickListener(new View.OnClickListener() { 93 | @Override 94 | public void onClick(View view) { 95 | photoGalleryView.showPhotoGallery(position, urlList, finalViewHoder.imageView); 96 | } 97 | }); 98 | 99 | return convertView; 100 | } 101 | }); 102 | drawableTypeGridView.setAdapter(new BaseAdapter() { 103 | @Override 104 | public int getCount() { 105 | return resourceList.size(); 106 | } 107 | 108 | @Override 109 | public Object getItem(int i) { 110 | return resourceList.get(i); 111 | } 112 | 113 | @Override 114 | public long getItemId(int i) { 115 | return i; 116 | } 117 | 118 | @Override 119 | public View getView(final int position, View convertView, ViewGroup viewGroup) { 120 | ViewHoder viewHoder = null; 121 | if (convertView == null) { 122 | convertView = inflater.inflate(R.layout.photo_gallery_item_photo_show, null); 123 | viewHoder = new ViewHoder(); 124 | viewHoder.imageView = (ImageView) convertView.findViewById(R.id.item_photo); 125 | convertView.setTag(viewHoder); 126 | } else { 127 | viewHoder = (ViewHoder) convertView.getTag(); 128 | } 129 | Glide.with(MainActivity.this).load(resourceList.get(position).photoSource).into(viewHoder.imageView); 130 | final ViewHoder finalViewHoder = viewHoder; 131 | viewHoder.imageView.setOnClickListener(new View.OnClickListener() { 132 | @Override 133 | public void onClick(View view) { 134 | photoGalleryView.showPhotoGallery(position, resourceList, finalViewHoder.imageView); 135 | } 136 | }); 137 | 138 | return convertView; 139 | } 140 | }); 141 | } 142 | 143 | class ViewHoder { 144 | private ImageView imageView; 145 | } 146 | 147 | } 148 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## 图片大图预览 2 | > 在我现在的项目当中,也存在大图预览的功能,但其实现过于繁重,采用一个Activity实现,并且在图片展示的过程中会产生卡顿感,整体感觉很是不好,正巧项目也在重构过程中,所以决定将这一功能写成一个成型的控件。话不多说,先上图看下效果。 3 | 4 | ![image](https://github.com/cedear/Cedear.github.io/blob/master/%E5%9B%BE%E7%89%87%E6%96%87%E4%BB%B6%E5%A4%B9/%E4%B8%8B%E8%BD%BD.gif?raw=true) 5 | ### 整体实现思路 6 | > 图片展示:PhotoView(大图支持双击放大) 7 | 图片加载:Glide(加载网络图片、本地图片、资源文件) 8 | 小图变大图时的实现:动画 9 | 图片的下载:插入系统相册 10 | 11 | > 该控件采用自定义View的方式,通过一些基本的控件的组合,来形成一个具有大图预览的控件。上代码 12 | 13 | ### 使用方法 14 | (1)在布局文件中引用该view 15 | ``` 16 | 24 | ``` 25 | (2)具体使用方法 26 | GalleryView galleryView = findViewById(R.id.photo_gallery_view); 27 | galleryView.showPhotoGallery(index, List, ImageView); 28 | > 到这里就结束了,就是这么简单! 29 | ### 具体实现 30 | 31 | (1)先从showPhotoGallery(index, List, ImageView)这个方法讲起 32 | > int index:我们想要展示的一个图片列表中的第几个 33 | List list: 我们要传入的要展示的图片类型list(支持网络图片、资源图片、本地图片(本地图片与网络图片其实都是一个字符串地址)) 34 | 35 | ``` 36 | public class GalleryPhotoModel { 37 | 38 | public Object photoSource; 39 | 40 | public GalleryPhotoModel(@DrawableRes int drawableRes) { 41 | this.photoSource = drawableRes; 42 | } 43 | 44 | public GalleryPhotoModel(String path) { 45 | this.photoSource = path; 46 | } 47 | 48 | } 49 | ``` 50 | > ImageView:即你点击想要展示的那个图片 51 | 52 | (2)对传入GalleryView的数据进行处理 53 | 54 | ``` 55 | /** 56 | * @param index 想要展示的图片的索引值 57 | * @param photoList 图片集合(URL、Drawable、Bitmap) 58 | * @param clickImageView 点击的第一个图片 59 | */ 60 | public void showPhotoGallery(int index, List photoList, ImageView clickImageView) { 61 | GalleryPhotoParameterModel photoParameter = new GalleryPhotoParameterModel(); 62 | //图片 63 | photoParameter.photoObj = photoList.get(index).photoSource; 64 | //图片在list中的索引 65 | photoParameter.index = index; 66 | int[] locationOnScreen = new int[2]; 67 | //图片位置参数 68 | clickImageView.getLocationOnScreen(locationOnScreen); 69 | photoParameter.locOnScreen = locationOnScreen; 70 | //图片的宽高 71 | int width = clickImageView.getDrawable().getBounds().width(); 72 | int height = clickImageView.getDrawable().getBounds().height(); 73 | photoParameter.imageWidth = clickImageView.getWidth(); 74 | photoParameter.imageHeight = clickImageView.getHeight(); 75 | photoParameter.photoHeight = height; 76 | photoParameter.photoWidth = width; 77 | //scaleType 78 | photoParameter.scaleType = clickImageView.getScaleType(); 79 | //将第一个点击的图片参数连同整个图片列表传入 80 | this.setVisibility(View.VISIBLE); 81 | post(new Runnable() { 82 | @Override 83 | public void run() { 84 | requestFocus(); 85 | } 86 | }); 87 | setGalleryPhotoList(photoList, photoParameter); 88 | } 89 | ``` 90 | > 通过传递进来的ImageView,获取被点击View参数,并拼装成参数model,再进行数据的相关处理。 91 | 92 | (3)GalleryView的实现机制 93 | > 该View的实现思路主要是:最外层是一个RelativeLayout,内部有一个充满父布局的ImageView和ViewPager。ImageView用来进行图片的动画缩放,ViewPager用来进行最后的图片的展示。其实该View最主要的地方就是通过点击ImageView到最后ViewPager的展示的动画。接下来主要是讲解一下这个地方。先看一下被点击ImageView的参数Model。GalleryPhotoParameterModel 94 | 95 | ``` 96 | public class GalleryPhotoParameterModel { 97 | 98 | //索引 99 | public int index; 100 | // 图片的类型 101 | public Object photoObj; 102 | // 在屏幕上的位置 103 | public int[] locOnScreen = new int[]{-1, -1}; 104 | // 图片的宽 105 | public int photoWidth = 0; 106 | // 图片的高 107 | public int photoHeight = 0; 108 | // ImageView的宽 109 | public int imageWidth = 0; 110 | // ImageView的高 111 | public int imageHeight = 0; 112 | // ImageView的缩放类型 113 | public ImageView.ScaleType scaleType; 114 | 115 | } 116 | ``` 117 | 3.1图片放大操作 118 | 119 | ``` 120 | private void handleZoomAnimation() { 121 | // 屏幕的宽高 122 | this.mScreenRect = GalleryScreenUtil.getDisplayPixes(getContext()); 123 | //将被缩放的图片放在一个单独的ImageView上进行单独的动画处理。 124 | Glide.with(getContext()).load(firstClickItemParameterModel.photoObj).into(mScaleImageView); 125 | //开启动画 126 | mScaleImageView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 127 | @Override 128 | public void onGlobalLayout() { 129 | //开始放大操作 130 | calculateScaleAndStartZoomInAnim(firstClickItemParameterModel); 131 | // 132 | mScaleImageView.getViewTreeObserver().removeGlobalOnLayoutListener(this); 133 | } 134 | }); 135 | } 136 | ``` 137 | 138 | ``` 139 | /** 140 | * 计算放大比例,开启放大动画 141 | * 142 | * @param photoData 143 | */ 144 | private void calculateScaleAndStartZoomInAnim(final GalleryPhotoParameterModel photoData) { 145 | mScaleImageView.setVisibility(View.VISIBLE); 146 | 147 | // 放大动画参数 148 | int translationX = (photoData.locOnScreen[0] + photoData.imageWidth / 2) - (int) (mScreenRect.width() / 2); 149 | int translationY = (photoData.locOnScreen[1] + photoData.imageHeight / 2) - (int) ((mScreenRect.height() + GalleryScreenUtil.getStatusBarHeight(getContext())) / 2); 150 | float scale = getImageViewScale(photoData); 151 | // 开启放大动画 152 | executeZoom(mScaleImageView, translationX, translationY, scale, true, new Animator.AnimatorListener() { 153 | @Override 154 | public void onAnimationStart(Animator animation) {} 155 | 156 | @Override 157 | public void onAnimationEnd(Animator animation) { 158 | showOtherViews(); 159 | tvPhotoSize.setText(String.format("%d/%d", viewPager.getCurrentItem() + 1, photoList.size())); 160 | } 161 | 162 | @Override 163 | public void onAnimationCancel(Animator animation) { 164 | 165 | } 166 | 167 | @Override 168 | public void onAnimationRepeat(Animator animation) { 169 | 170 | } 171 | }); 172 | } 173 | ``` 174 | 3.2 图片缩小操作 175 | 176 | ``` 177 | /** 178 | * 计算缩小比例,开启缩小动画 179 | */ 180 | private void calculateScaleAndStartZoomOutAnim() { 181 | hiedOtherViews(); 182 | 183 | // 缩小动画参数 184 | int translationX = (firstClickItemParameterModel.locOnScreen[0] + firstClickItemParameterModel.imageWidth / 2) - (int) (mScreenRect.width() / 2); 185 | int translationY = (firstClickItemParameterModel.locOnScreen[1] + firstClickItemParameterModel.imageHeight / 2) - (int) ((mScreenRect.height() + GalleryScreenUtil.getStatusBarHeight(getContext())) / 2); 186 | float scale = getImageViewScale(firstClickItemParameterModel); 187 | // 开启缩小动画 188 | executeZoom(mScaleImageView, translationX, translationY, scale, false, new Animator.AnimatorListener() { 189 | @Override 190 | public void onAnimationStart(Animator animation) {} 191 | 192 | @Override 193 | public void onAnimationEnd(Animator animation) { 194 | mScaleImageView.setImageDrawable(null); 195 | mScaleImageView.setVisibility(GONE); 196 | setVisibility(GONE); 197 | } 198 | 199 | @Override 200 | public void onAnimationCancel(Animator animation) {} 201 | 202 | @Override 203 | public void onAnimationRepeat(Animator animation) {} 204 | }); 205 | } 206 | ``` 207 | 3.3 计算图片缩放的比例 208 | ``` 209 | private float getImageViewScale(GalleryPhotoParameterModel photoData) { 210 | float scale; 211 | float scaleX = photoData.imageWidth / mScreenRect.width(); 212 | float scaleY = photoData.photoHeight * 1.0f / mScaleImageView.getHeight(); 213 | 214 | // 横向图片 215 | if (photoData.photoWidth > photoData.photoHeight) { 216 | // 图片的宽高比 217 | float photoScale = photoData.photoWidth * 1.0f / photoData.photoHeight; 218 | // 执行动画的ImageView宽高比 219 | float animationImageScale = mScaleImageView.getWidth() * 1.0f / mScaleImageView.getHeight(); 220 | 221 | if (animationImageScale > photoScale) { 222 | // 动画ImageView宽高比大于图片宽高比的时候,需要用图片的高度除以动画ImageView高度的比例尺 223 | scale = scaleY; 224 | } 225 | else { 226 | scale = scaleX; 227 | } 228 | } 229 | // 正方形图片 230 | else if (photoData.photoWidth == photoData.photoHeight) { 231 | if (mScaleImageView.getWidth() > mScaleImageView.getHeight()) { 232 | scale = scaleY; 233 | } 234 | else { 235 | scale = scaleX; 236 | } 237 | } 238 | // 纵向图片 239 | else { 240 | scale = scaleY; 241 | } 242 | return scale; 243 | } 244 | ``` 245 | 3.4 执行动画的缩放 246 | ``` 247 | /** 248 | * 执行缩放动画 249 | * @param scaleImageView 250 | * @param translationX 251 | * @param translationY 252 | * @param scale 253 | * @param isEnlarge 254 | */ 255 | private void executeZoom(final ImageView scaleImageView, int translationX, int translationY, float scale, boolean isEnlarge, Animator.AnimatorListener listener) { 256 | float startTranslationX, startTranslationY, endTranslationX, endTranslationY; 257 | float startScale, endScale, startAlpha, endAlpha; 258 | 259 | // 放大 260 | if (isEnlarge) { 261 | startTranslationX = translationX; 262 | endTranslationX = 0; 263 | startTranslationY = translationY; 264 | endTranslationY = 0; 265 | startScale = scale; 266 | endScale = 1; 267 | startAlpha = 0f; 268 | endAlpha = 0.75f; 269 | } 270 | // 缩小 271 | else { 272 | startTranslationX = 0; 273 | endTranslationX = translationX; 274 | startTranslationY = 0; 275 | endTranslationY = translationY; 276 | startScale = 1; 277 | endScale = scale; 278 | startAlpha = 0.75f; 279 | endAlpha = 0f; 280 | } 281 | 282 | //-------缩小动画-------- 283 | AnimatorSet set = new AnimatorSet(); 284 | set.play( 285 | ObjectAnimator.ofFloat(scaleImageView, "translationX", startTranslationX, endTranslationX)) 286 | .with(ObjectAnimator.ofFloat(scaleImageView, "translationY", startTranslationY, endTranslationY)) 287 | .with(ObjectAnimator.ofFloat(scaleImageView, "scaleX", startScale, endScale)) 288 | .with(ObjectAnimator.ofFloat(scaleImageView, "scaleY", startScale, endScale)) 289 | // ---Alpha动画--- 290 | // mMaskView伴随着一个Alpha减小动画 291 | .with(ObjectAnimator.ofFloat(maskView, "alpha", startAlpha, endAlpha)); 292 | set.setDuration(animDuration); 293 | if (listener != null) { 294 | set.addListener(listener); 295 | } 296 | set.setInterpolator(new DecelerateInterpolator()); 297 | set.start(); 298 | } 299 | ``` 300 | > 改View的主要实现如上,在图片进行缩放的时候,要考虑的情况:短边适配、图片原尺寸的宽高、展示图片的ImageView的宽高比、横竖屏时屏幕的尺寸。在此非常感谢震哥的帮助、抱拳了!老铁。如有更多想法的小伙伴。请移步我的github [GalleryView地址](https://github.com/cedear/GalleryView) 301 | -------------------------------------------------------------------------------- /gallery/src/main/java/gallery/demo/com/gallery/view/GalleryView.java: -------------------------------------------------------------------------------- 1 | package gallery.demo.com.gallery.view; 2 | 3 | import android.Manifest; 4 | import android.animation.Animator; 5 | import android.animation.AnimatorSet; 6 | import android.animation.ObjectAnimator; 7 | import android.app.Activity; 8 | import android.content.Context; 9 | import android.content.res.TypedArray; 10 | import android.graphics.RectF; 11 | import android.os.Message; 12 | import android.support.annotation.ColorInt; 13 | import android.support.annotation.Nullable; 14 | import android.support.v4.view.ViewPager; 15 | import android.text.TextUtils; 16 | import android.util.AttributeSet; 17 | import android.view.KeyEvent; 18 | import android.view.View; 19 | import android.view.ViewTreeObserver; 20 | import android.view.animation.DecelerateInterpolator; 21 | import android.widget.ImageView; 22 | import android.widget.RelativeLayout; 23 | import android.widget.TextView; 24 | import android.widget.Toast; 25 | 26 | import com.bumptech.glide.Glide; 27 | import com.tbruyelle.rxpermissions2.RxPermissions; 28 | 29 | import java.util.ArrayList; 30 | import java.util.List; 31 | 32 | import gallery.demo.com.gallery.R; 33 | import gallery.demo.com.gallery.adapter.GalleryPhotoAdapter; 34 | import gallery.demo.com.gallery.model.GalleryPhotoModel; 35 | import gallery.demo.com.gallery.model.GalleryPhotoParameterModel; 36 | import gallery.demo.com.gallery.util.GalleryDownLoadCallBack; 37 | import gallery.demo.com.gallery.util.GalleryDownloadThread; 38 | import gallery.demo.com.gallery.util.GalleryScreenUtil; 39 | 40 | /** 41 | * Created by bjhl on 2018/3/26. 42 | */ 43 | 44 | public class GalleryView extends RelativeLayout { 45 | 46 | private static final int DEFAULT_ANIM_DURATION = 300; // 毫秒 47 | 48 | // 用于缩放的ImageView 49 | private ImageView mScaleImageView = null; 50 | // 阴影背景 51 | private View maskView; 52 | // 屏幕的宽高 53 | private RectF mScreenRect = null; 54 | //viewpager滑到的位置 55 | private int position; 56 | private int animDuration; 57 | private int saveTextSize; 58 | private int saveTextColor; 59 | private String saveText; 60 | private ViewPager viewPager; 61 | private TextView tvPhotoSize; 62 | private TextView tvPhotoSave; 63 | private List viewList; 64 | private List photoList; 65 | private GalleryPhotoParameterModel firstClickItemParameterModel; 66 | private GalleryPhotoAdapter adapter; 67 | private final static int WHAT_SAVE_SUCCESS = 1; 68 | private final static int WHAT_SAVE_FAILED = 2; 69 | private RxPermissions rxPermissions; 70 | 71 | 72 | private android.os.Handler handler = new android.os.Handler() { 73 | @Override 74 | public void handleMessage(Message msg) { 75 | super.handleMessage(msg); 76 | switch (msg.what) { 77 | case WHAT_SAVE_SUCCESS: 78 | Toast.makeText(getContext(), "保存成功", Toast.LENGTH_SHORT).show(); 79 | break; 80 | case WHAT_SAVE_FAILED: 81 | Toast.makeText(getContext(), "保存失败", Toast.LENGTH_SHORT).show(); 82 | break; 83 | } 84 | } 85 | }; 86 | 87 | public GalleryView(Context context) { 88 | this(context, null); 89 | } 90 | 91 | public GalleryView(Context context, @Nullable AttributeSet attrs) { 92 | this(context, attrs, 0); 93 | } 94 | 95 | public GalleryView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 96 | super(context, attrs, defStyleAttr); 97 | 98 | if (attrs != null) { 99 | TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.GalleryView); 100 | animDuration = typedArray.getInt(R.styleable.GalleryView_animDuration, DEFAULT_ANIM_DURATION); 101 | saveText = typedArray.getString(R.styleable.GalleryView_saveText); 102 | saveTextSize = typedArray.getDimensionPixelSize(R.styleable.GalleryView_saveTextSize, 0); 103 | saveTextColor = typedArray.getColor(R.styleable.GalleryView_saveTextColor, 0); 104 | } 105 | else { 106 | animDuration = DEFAULT_ANIM_DURATION; 107 | } 108 | 109 | setFocusable(true); 110 | setFocusableInTouchMode(true); 111 | initView(); 112 | // 拦截单击事件,防止其他view被点击 113 | setOnClickListener(new OnClickListener() { 114 | @Override 115 | public void onClick(View v) { 116 | 117 | } 118 | }); 119 | } 120 | 121 | @Override 122 | public boolean onKeyDown(int keyCode, KeyEvent event) { 123 | if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN && getVisibility() == VISIBLE) { 124 | calculateScaleAndStartZoomOutAnim(); 125 | return true; 126 | } 127 | return super.onKeyDown(keyCode, event); 128 | } 129 | 130 | /** 131 | * @param index 想要展示的图片的索引值 132 | * @param photoList 图片集合(URL、Drawable、Bitmap) 133 | * @param clickImageView 点击的第一个图片 134 | */ 135 | public void showPhotoGallery(int index, List photoList, ImageView clickImageView) { 136 | GalleryPhotoParameterModel photoParameter = new GalleryPhotoParameterModel(); 137 | //图片地址 138 | photoParameter.photoObj = photoList.get(index).photoSource; 139 | //图片在list中的索引 140 | photoParameter.index = index; 141 | int[] locationOnScreen = new int[2]; 142 | //图片位置参数 143 | clickImageView.getLocationOnScreen(locationOnScreen); 144 | photoParameter.locOnScreen = locationOnScreen; 145 | //图片的宽高 146 | int width = clickImageView.getDrawable().getBounds().width(); 147 | int height = clickImageView.getDrawable().getBounds().height(); 148 | photoParameter.imageWidth = clickImageView.getWidth(); 149 | photoParameter.imageHeight = clickImageView.getHeight(); 150 | photoParameter.photoHeight = height; 151 | photoParameter.photoWidth = width; 152 | //scaleType 153 | photoParameter.scaleType = clickImageView.getScaleType(); 154 | //将第一个点击的图片参数连同整个图片列表传入 155 | this.setVisibility(View.VISIBLE); 156 | post(new Runnable() { 157 | @Override 158 | public void run() { 159 | requestFocus(); 160 | } 161 | }); 162 | setGalleryPhotoList(photoList, photoParameter); 163 | } 164 | 165 | /** 166 | * 设置缩放时动画持续时间 167 | * @param duration 毫秒 168 | */ 169 | public void setAnimDuration(int duration) { 170 | animDuration = duration; 171 | } 172 | 173 | public void setSaveText(String text) { 174 | if (tvPhotoSave != null) { 175 | tvPhotoSave.setText(text); 176 | } 177 | } 178 | 179 | public void setSaveTextSize(int size, int... unit) { 180 | if (tvPhotoSave != null) { 181 | if (unit != null) { 182 | tvPhotoSave.setTextSize(unit[0], size); 183 | } 184 | else { 185 | tvPhotoSave.setTextSize(size); 186 | } 187 | } 188 | } 189 | 190 | public void setSaveTextColoc(@ColorInt int color) { 191 | if (tvPhotoSave != null) { 192 | tvPhotoSave.setTextColor(color); 193 | } 194 | } 195 | 196 | private void setGalleryPhotoList(List list, GalleryPhotoParameterModel parameterModel) { 197 | // boolean isFocus = requestFocus(); 198 | if (list != null && parameterModel != null) { 199 | if (photoList != null && photoList.size() != 0) { 200 | photoList.clear(); 201 | } else { 202 | photoList = new ArrayList<>(); 203 | } 204 | photoList.addAll(list); 205 | //获取第一个被点击的图片的具体参数 206 | firstClickItemParameterModel = parameterModel; 207 | initData(); 208 | //处理放大动画 209 | handleZoomAnimation(); 210 | } 211 | } 212 | 213 | private void handleZoomAnimation() { 214 | // 屏幕的宽高 215 | this.mScreenRect = GalleryScreenUtil.getDisplayPixes(getContext()); 216 | //将被缩放的图片放在一个单独的ImageView上进行单独的动画处理。 217 | Glide.with(getContext()).load(firstClickItemParameterModel.photoObj).into(mScaleImageView); 218 | //开启动画 219 | mScaleImageView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 220 | @Override 221 | public void onGlobalLayout() { 222 | //开始放大操作 223 | calculateScaleAndStartZoomInAnim(firstClickItemParameterModel); 224 | // 225 | mScaleImageView.getViewTreeObserver().removeGlobalOnLayoutListener(this); 226 | } 227 | }); 228 | } 229 | 230 | 231 | /** 232 | * 初始化视图 233 | */ 234 | private void initView() { 235 | View view = View.inflate(getContext(), R.layout.gallery_view_main, this); 236 | maskView = view.findViewById(R.id.gallery_view_main_mask_View); 237 | mScaleImageView = (ImageView) view.findViewById(R.id.gallery_view_main_scale_imageView); 238 | viewPager = (ViewPager) view.findViewById(R.id.gallery_view_main_viewpager); 239 | tvPhotoSize = (TextView) view.findViewById(R.id.gallery_view_main_photo_size_tv); 240 | tvPhotoSave = (TextView) view.findViewById(R.id.gallery_view_main_photo_save); 241 | 242 | if (!TextUtils.isEmpty(saveText)) { 243 | tvPhotoSave.setText(saveText); 244 | } 245 | 246 | if (saveTextSize != 0) { 247 | tvPhotoSave.setTextSize(saveTextSize); 248 | } 249 | 250 | if (saveTextColor != 0) { 251 | tvPhotoSave.setTextColor(saveTextColor); 252 | } 253 | } 254 | 255 | /** 256 | * 初始化数据,设置事件监听 257 | */ 258 | private void initData() { 259 | if (viewList != null && viewList.size() != 0) { 260 | viewList.clear(); 261 | } else { 262 | viewList = new ArrayList<>(); 263 | } 264 | for (int i = 0; i < photoList.size(); i++) { 265 | GalleryPhotoView galleryPhoto = new GalleryPhotoView(getContext(), photoList.get(i)); 266 | viewList.add(galleryPhoto); 267 | galleryPhoto.setOnClickListener(new OnClickListener() { 268 | @Override 269 | public void onClick(View view) { 270 | //隐藏当前viewpager 271 | calculateScaleAndStartZoomOutAnim(); 272 | } 273 | }); 274 | } 275 | adapter = new GalleryPhotoAdapter(viewList); 276 | viewPager.setAdapter(adapter); 277 | viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { 278 | @Override 279 | public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 280 | 281 | } 282 | 283 | @Override 284 | public void onPageSelected(int position) { 285 | GalleryView.this.position = position; 286 | tvPhotoSize.setText(viewPager.getCurrentItem() + 1 + "/" + photoList.size()); 287 | } 288 | 289 | @Override 290 | public void onPageScrollStateChanged(int state) { 291 | 292 | } 293 | }); 294 | viewPager.setCurrentItem(firstClickItemParameterModel.index); 295 | 296 | tvPhotoSave.setOnClickListener(new OnClickListener() { 297 | @Override 298 | public void onClick(View view) { 299 | final Context context = getContext(); 300 | if (context instanceof Activity) { 301 | checkPermission((Activity) context); 302 | } 303 | } 304 | }); 305 | } 306 | 307 | private void showOtherViews() { 308 | mScaleImageView.setVisibility(GONE); 309 | tvPhotoSave.setVisibility(VISIBLE); 310 | tvPhotoSize.setVisibility(VISIBLE); 311 | viewPager.setVisibility(VISIBLE); 312 | } 313 | 314 | private void hiedOtherViews() { 315 | mScaleImageView.setVisibility(VISIBLE); 316 | tvPhotoSave.setVisibility(GONE); 317 | tvPhotoSize.setVisibility(GONE); 318 | viewPager.setVisibility(GONE); 319 | } 320 | 321 | private void checkPermission(final Activity activity) { 322 | if (rxPermissions == null) { 323 | rxPermissions = new RxPermissions(activity); 324 | } 325 | if (rxPermissions.isGranted(Manifest.permission.WRITE_EXTERNAL_STORAGE)) { 326 | saveBitmapToLocation(); 327 | } else { 328 | rxPermissions 329 | .request(Manifest.permission.WRITE_EXTERNAL_STORAGE) 330 | .subscribe(granted -> { 331 | if (granted) { 332 | saveBitmapToLocation(); 333 | } else { 334 | Toast.makeText(activity, R.string.gallery_save_picture_permission_request_failed, Toast.LENGTH_SHORT).show(); 335 | } 336 | }); 337 | } 338 | } 339 | 340 | private void saveBitmapToLocation() { 341 | GalleryDownloadThread download = new GalleryDownloadThread(getContext(), photoList.get(position).photoSource, new GalleryDownLoadCallBack() { 342 | @Override 343 | public void onDownLoadSuccess() { 344 | handler.sendEmptyMessage(WHAT_SAVE_SUCCESS); 345 | } 346 | 347 | @Override 348 | public void onDownLoadFailed() { 349 | handler.sendEmptyMessage(WHAT_SAVE_FAILED); 350 | } 351 | }); 352 | new Thread(download).start(); 353 | } 354 | 355 | /** 356 | * 计算放大比例,开启放大动画 357 | * 358 | * @param photoData 359 | */ 360 | private void calculateScaleAndStartZoomInAnim(final GalleryPhotoParameterModel photoData) { 361 | mScaleImageView.setVisibility(View.VISIBLE); 362 | 363 | // 放大动画参数 364 | int translationX = (photoData.locOnScreen[0] + photoData.imageWidth / 2) - (int) (mScreenRect.width() / 2); 365 | int translationY = (photoData.locOnScreen[1] + photoData.imageHeight / 2) - (int) ((mScreenRect.height() + GalleryScreenUtil.getStatusBarHeight(getContext())) / 2); 366 | float scale = getImageViewScale(photoData); 367 | // 开启放大动画 368 | executeZoom(mScaleImageView, translationX, translationY, scale, true, new Animator.AnimatorListener() { 369 | @Override 370 | public void onAnimationStart(Animator animation) {} 371 | 372 | @Override 373 | public void onAnimationEnd(Animator animation) { 374 | showOtherViews(); 375 | tvPhotoSize.setText(String.format("%d/%d", viewPager.getCurrentItem() + 1, photoList.size())); 376 | } 377 | 378 | @Override 379 | public void onAnimationCancel(Animator animation) { 380 | 381 | } 382 | 383 | @Override 384 | public void onAnimationRepeat(Animator animation) { 385 | 386 | } 387 | }); 388 | } 389 | 390 | /** 391 | * 计算缩小比例,开启缩小动画 392 | */ 393 | private void calculateScaleAndStartZoomOutAnim() { 394 | hiedOtherViews(); 395 | 396 | // 缩小动画参数 397 | int translationX = (firstClickItemParameterModel.locOnScreen[0] + firstClickItemParameterModel.imageWidth / 2) - (int) (mScreenRect.width() / 2); 398 | int translationY = (firstClickItemParameterModel.locOnScreen[1] + firstClickItemParameterModel.imageHeight / 2) - (int) ((mScreenRect.height() + GalleryScreenUtil.getStatusBarHeight(getContext())) / 2); 399 | float scale = getImageViewScale(firstClickItemParameterModel); 400 | // 开启缩小动画 401 | executeZoom(mScaleImageView, translationX, translationY, scale, false, new Animator.AnimatorListener() { 402 | @Override 403 | public void onAnimationStart(Animator animation) {} 404 | 405 | @Override 406 | public void onAnimationEnd(Animator animation) { 407 | mScaleImageView.setImageDrawable(null); 408 | mScaleImageView.setVisibility(GONE); 409 | setVisibility(GONE); 410 | } 411 | 412 | @Override 413 | public void onAnimationCancel(Animator animation) {} 414 | 415 | @Override 416 | public void onAnimationRepeat(Animator animation) {} 417 | }); 418 | } 419 | 420 | /** 421 | * 执行缩放动画 422 | * @param scaleImageView 423 | * @param translationX 424 | * @param translationY 425 | * @param scale 426 | * @param isEnlarge 427 | */ 428 | private void executeZoom(final ImageView scaleImageView, int translationX, int translationY, float scale, boolean isEnlarge, Animator.AnimatorListener listener) { 429 | float startTranslationX, startTranslationY, endTranslationX, endTranslationY; 430 | float startScale, endScale, startAlpha, endAlpha; 431 | 432 | // 放大 433 | if (isEnlarge) { 434 | startTranslationX = translationX; 435 | endTranslationX = 0; 436 | startTranslationY = translationY; 437 | endTranslationY = 0; 438 | startScale = scale; 439 | endScale = 1; 440 | startAlpha = 0f; 441 | endAlpha = 0.75f; 442 | } 443 | // 缩小 444 | else { 445 | startTranslationX = 0; 446 | endTranslationX = translationX; 447 | startTranslationY = 0; 448 | endTranslationY = translationY; 449 | startScale = 1; 450 | endScale = scale; 451 | startAlpha = 0.75f; 452 | endAlpha = 0f; 453 | } 454 | 455 | //-------缩小动画-------- 456 | AnimatorSet set = new AnimatorSet(); 457 | set.play( 458 | ObjectAnimator.ofFloat(scaleImageView, "translationX", startTranslationX, endTranslationX)) 459 | .with(ObjectAnimator.ofFloat(scaleImageView, "translationY", startTranslationY, endTranslationY)) 460 | .with(ObjectAnimator.ofFloat(scaleImageView, "scaleX", startScale, endScale)) 461 | .with(ObjectAnimator.ofFloat(scaleImageView, "scaleY", startScale, endScale)) 462 | // ---Alpha动画--- 463 | // mMaskView伴随着一个Alpha减小动画 464 | .with(ObjectAnimator.ofFloat(maskView, "alpha", startAlpha, endAlpha)); 465 | set.setDuration(animDuration); 466 | if (listener != null) { 467 | set.addListener(listener); 468 | } 469 | set.setInterpolator(new DecelerateInterpolator()); 470 | set.start(); 471 | } 472 | 473 | private float getImageViewScale(GalleryPhotoParameterModel photoData) { 474 | float scale; 475 | float scaleX = photoData.imageWidth / mScreenRect.width(); 476 | float scaleY = photoData.photoHeight * 1.0f / mScaleImageView.getHeight(); 477 | 478 | // 横向图片 479 | if (photoData.photoWidth > photoData.photoHeight) { 480 | // 图片的宽高比 481 | float photoScale = photoData.photoWidth * 1.0f / photoData.photoHeight; 482 | // 执行动画的ImageView宽高比 483 | float animationImageScale = mScaleImageView.getWidth() * 1.0f / mScaleImageView.getHeight(); 484 | 485 | if (animationImageScale > photoScale) { 486 | // 动画ImageView宽高比大于图片宽高比的时候,需要用图片的高度除以动画ImageView高度的比例尺 487 | scale = scaleY; 488 | } 489 | else { 490 | scale = scaleX; 491 | } 492 | } 493 | // 正方形图片 494 | else if (photoData.photoWidth == photoData.photoHeight) { 495 | if (mScaleImageView.getWidth() > mScaleImageView.getHeight()) { 496 | scale = scaleY; 497 | } 498 | else { 499 | scale = scaleX; 500 | } 501 | } 502 | // 纵向图片 503 | else { 504 | scale = scaleY; 505 | } 506 | return scale; 507 | } 508 | 509 | 510 | } 511 | --------------------------------------------------------------------------------