Skip to content

Commit

Permalink
修复超大图(宽高超过8192)预览显示不了的问题、过滤未下载完成的图片。
Browse files Browse the repository at this point in the history
  • Loading branch information
梁任彦 committed Nov 9, 2017
1 parent aa07343 commit 5eba8f3
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 17 deletions.
2 changes: 1 addition & 1 deletion imageselector/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ android {

defaultConfig {
minSdkVersion 14
targetSdkVersion 23
targetSdkVersion 25
versionCode 1
versionName "1.0"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
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;
Expand All @@ -13,6 +14,7 @@
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.donkingliang.imageselector.entry.Image;
import com.donkingliang.imageselector.utils.ImageUtil;
import com.github.chrisbanes.photoview.PhotoView;
import com.github.chrisbanes.photoview.PhotoViewAttacher;

Expand All @@ -38,6 +40,7 @@ public ImagePagerAdapter(Context context, List<Image> imgList) {
private void createImageViews() {
for (int i = 0; i < 4; i++) {
PhotoView imageView = new PhotoView(mContext);
imageView.setAdjustViewBounds(true);
viewList.add(imageView);
}
}
Expand Down Expand Up @@ -71,21 +74,17 @@ public Object instantiateItem(ViewGroup container, final int position) {
.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);
}
if (bw > 8192 || bh > 8192) {
Bitmap bitmap = ImageUtil.zoomBitmap(resource, 8192, 8192);
setBitmap(currentView, bitmap);
} else {
setBitmap(currentView, resource);
}
} else {
currentView.setImageBitmap(null);
}
}
});
Expand All @@ -100,6 +99,25 @@ public void onClick(View v) {
return currentView;
}

private void setBitmap(PhotoView imageView, Bitmap bitmap) {
imageView.setImageBitmap(bitmap);
if (bitmap != null) {
int bw = bitmap.getWidth();
int bh = bitmap.getHeight();
int vw = imageView.getWidth();
int vh = imageView.getHeight();
if (bw != 0 && bh != 0 && vw != 0 && vh != 0) {
if (1.0f * bh / bw > 1.0f * vh / vw) {
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
float offset = (1.0f * bh * vw / bw - vh) / 2;
adjustOffset(imageView, offset);
} else {
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
}
}
}
}

public void setOnItemClickListener(OnItemClickListener l) {
mListener = l;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ public void run() {
//获取图片时间
long time = mCursor.getLong(
mCursor.getColumnIndex(MediaStore.Images.Media.DATE_ADDED));
images.add(new Image(path, time, name));
if (!".downloading".equals(path)) { //过滤未下载完成的文件
images.add(new Image(path, time, name));
}
}
mCursor.close();
}
Expand Down Expand Up @@ -90,6 +92,19 @@ private static ArrayList<Folder> splitFolder(ArrayList<Image> images) {
return folders;
}

/*
* Java文件操作 获取文件扩展名
* */
public static String getExtensionName(String filename) {
if (filename != null && filename.length() > 0) {
int dot = filename.lastIndexOf('.');
if (dot > -1 && dot < filename.length() - 1) {
return filename.substring(dot + 1);
}
}
return "";
}

/**
* 跟着图片路径,获取图片文件夹名称
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,23 @@ public static String saveImage(Bitmap bitmap, String path) {
return "";
}

public static Bitmap zoomBitmap(Bitmap bm, int reqWidth, int reqHeight) {
// 获得图片的宽高
int width = bm.getWidth();
int height = bm.getHeight();
// 计算缩放比例
float scaleWidth = ((float) reqWidth) / width;
float scaleHeight = ((float) reqHeight) / height;
float scale = Math.min(scaleWidth, scaleHeight);
// 取得想要缩放的matrix参数
Matrix matrix = new Matrix();
matrix.postScale(scale, scale);
// 得到新的图片
Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix,
true);
return newbm;
}

/**
* 根据计算的inSampleSize,得到压缩后图片
*
Expand Down Expand Up @@ -130,7 +147,7 @@ public static Bitmap rotateImageView(Bitmap bitmap, int angle) {
* @return
*/
private static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
int reqWidth, int reqHeight) {
// 源图片的宽度
int width = options.outWidth;
int height = options.outHeight;
Expand Down
8 changes: 5 additions & 3 deletions imageselector/src/main/res/layout/activity_image_select.xml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@
android:fadingEdge="none"
android:fadingEdgeLength="0dp"
android:overScrollMode="never"
android:paddingTop="3dp"
android:paddingBottom="3dp"
android:scrollbars="vertical" />

<RelativeLayout
Expand All @@ -136,9 +138,9 @@
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:layout_gravity="center_vertical">
android:paddingRight="15dp">

<TextView
android:id="@+id/tv_folder_name"
Expand All @@ -151,8 +153,8 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/tv_folder_name"
android:layout_toRightOf="@+id/tv_folder_name"
android:layout_marginLeft="2dp"
android:layout_toRightOf="@+id/tv_folder_name"
android:src="@drawable/text_indicator" />

</RelativeLayout>
Expand Down

0 comments on commit 5eba8f3

Please sign in to comment.