Skip to content

Commit

Permalink
支持长图预览、升级PhotoView、修复已知bug。
Browse files Browse the repository at this point in the history
  • Loading branch information
梁任彦 committed Oct 27, 2017
1 parent 137a33d commit 79943d5
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 25 deletions.
2 changes: 1 addition & 1 deletion imageselector/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}

//---------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ public static int getStatusBarHeight(Context context) {

/**
* 显示和隐藏状态栏
*
* @param show
*/
private void setStatusBarVisible(boolean show) {
Expand All @@ -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);
}
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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);
Expand All @@ -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<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> 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) {
Expand All @@ -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();
}
}
}

0 comments on commit 79943d5

Please sign in to comment.