diff --git a/imageselector/build.gradle b/imageselector/build.gradle index c986a01..bfe0366 100644 --- a/imageselector/build.gradle +++ b/imageselector/build.gradle @@ -26,7 +26,7 @@ dependencies { compile 'com.android.support:appcompat-v7:25.3.1' compile 'com.android.support:recyclerview-v7:25.3.1' compile 'com.github.bumptech.glide:glide:3.7.0' - compile 'com.github.chrisbanes:PhotoView:2.0.0' + compile 'com.github.chrisbanes:PhotoView:2.1.3' } //--------------------------------------------- diff --git a/imageselector/src/main/java/com/donkingliang/imageselector/PreviewActivity.java b/imageselector/src/main/java/com/donkingliang/imageselector/PreviewActivity.java index 0bdab39..77b7aa7 100644 --- a/imageselector/src/main/java/com/donkingliang/imageselector/PreviewActivity.java +++ b/imageselector/src/main/java/com/donkingliang/imageselector/PreviewActivity.java @@ -201,6 +201,7 @@ public static int getStatusBarHeight(Context context) { /** * 显示和隐藏状态栏 + * * @param show */ private void setStatusBarVisible(boolean show) { @@ -222,18 +223,22 @@ private void showBar() { rlTopBar.postDelayed(new Runnable() { @Override public void run() { - ObjectAnimator animator = ofFloat(rlTopBar, "translationY", - rlTopBar.getTranslationY(), 0).setDuration(300); - animator.addListener(new AnimatorListenerAdapter() { - @Override - public void onAnimationStart(Animator animation) { - super.onAnimationStart(animation); - rlTopBar.setVisibility(View.VISIBLE); - } - }); - animator.start(); - ofFloat(rlBottomBar, "translationY", rlBottomBar.getTranslationY(), 0) - .setDuration(300).start(); + if (rlTopBar != null) { + ObjectAnimator animator = ofFloat(rlTopBar, "translationY", + rlTopBar.getTranslationY(), 0).setDuration(300); + animator.addListener(new AnimatorListenerAdapter() { + @Override + public void onAnimationStart(Animator animation) { + super.onAnimationStart(animation); + if (rlTopBar != null) { + rlTopBar.setVisibility(View.VISIBLE); + } + } + }); + animator.start(); + ofFloat(rlBottomBar, "translationY", rlBottomBar.getTranslationY(), 0) + .setDuration(300).start(); + } } }, 100); } @@ -249,14 +254,16 @@ private void hideBar() { @Override public void onAnimationEnd(Animator animation) { super.onAnimationEnd(animation); - rlTopBar.setVisibility(View.GONE); - //添加延时,保证rlTopBar完全隐藏后再隐藏StatusBar。 - rlTopBar.postDelayed(new Runnable() { - @Override - public void run() { - setStatusBarVisible(false); - } - }, 5); + if (rlTopBar != null) { + rlTopBar.setVisibility(View.GONE); + //添加延时,保证rlTopBar完全隐藏后再隐藏StatusBar。 + rlTopBar.postDelayed(new Runnable() { + @Override + public void run() { + setStatusBarVisible(false); + } + }, 5); + } } }); animator.start(); diff --git a/imageselector/src/main/java/com/donkingliang/imageselector/adapter/ImagePagerAdapter.java b/imageselector/src/main/java/com/donkingliang/imageselector/adapter/ImagePagerAdapter.java index 8c1df9b..b3903fc 100644 --- a/imageselector/src/main/java/com/donkingliang/imageselector/adapter/ImagePagerAdapter.java +++ b/imageselector/src/main/java/com/donkingliang/imageselector/adapter/ImagePagerAdapter.java @@ -1,17 +1,24 @@ package com.donkingliang.imageselector.adapter; import android.content.Context; +import android.graphics.Bitmap; +import android.graphics.Matrix; import android.support.v4.view.PagerAdapter; -import android.util.Log; import android.view.View; import android.view.ViewGroup; +import android.widget.ImageView; import com.bumptech.glide.Glide; import com.bumptech.glide.load.engine.DiskCacheStrategy; +import com.bumptech.glide.request.animation.GlideAnimation; +import com.bumptech.glide.request.target.SimpleTarget; import com.donkingliang.imageselector.entry.Image; import com.github.chrisbanes.photoview.PhotoView; +import com.github.chrisbanes.photoview.PhotoViewAttacher; import java.io.File; +import java.lang.reflect.Field; +import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; @@ -47,8 +54,8 @@ public boolean isViewFromObject(View view, Object object) { @Override public void destroyItem(ViewGroup container, int position, Object object) { - if(object instanceof PhotoView){ - PhotoView view = (PhotoView)object; + if (object instanceof PhotoView) { + PhotoView view = (PhotoView) object; view.setImageDrawable(null); viewList.add(view); container.removeView(view); @@ -61,7 +68,27 @@ public Object instantiateItem(ViewGroup container, final int position) { final Image image = mImgList.get(position); container.addView(currentView); Glide.with(mContext).load(new File(image.getPath())) - .diskCacheStrategy(DiskCacheStrategy.NONE).into(currentView); + .asBitmap().diskCacheStrategy(DiskCacheStrategy.NONE).into(new SimpleTarget() { + @Override + public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) { + currentView.setImageBitmap(resource); + if (resource != null) { + int bw = resource.getWidth(); + int bh = resource.getHeight(); + int vw = currentView.getWidth(); + int vh = currentView.getHeight(); + if (bw != 0 && bh != 0 && vw != 0 && vh != 0) { + if (1.0f * bh / bw > 1.0f * vh / vw) { + currentView.setScaleType(ImageView.ScaleType.CENTER_CROP); + float offset = (1.0f * bh * vw / bw - vh) / 2; + adjustOffset(currentView, offset); + } else { + currentView.setScaleType(ImageView.ScaleType.FIT_CENTER); + } + } + } + } + }); currentView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { @@ -80,4 +107,19 @@ public void setOnItemClickListener(OnItemClickListener l) { public interface OnItemClickListener { void onItemClick(int position, Image image); } + + private void adjustOffset(PhotoView view, float offset) { + PhotoViewAttacher attacher = view.getAttacher(); + try { + Field field = PhotoViewAttacher.class.getDeclaredField("mBaseMatrix"); + field.setAccessible(true); + Matrix matrix = (Matrix) field.get(attacher); + matrix.postTranslate(0, offset); + Method method = PhotoViewAttacher.class.getDeclaredMethod("resetMatrix"); + method.setAccessible(true); + method.invoke(attacher); + } catch (Exception e) { + e.printStackTrace(); + } + } }