Skip to content

Commit

Permalink
查看大图的时候支持缓存
Browse files Browse the repository at this point in the history
  • Loading branch information
wenmingvs committed Sep 5, 2016
1 parent b8803a7 commit 50bbce0
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 70 deletions.
1 change: 1 addition & 0 deletions weiSwift/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@ dependencies {
compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'

compile 'org.greenrobot:eventbus:3.0.0'
compile 'com.daimajia.numberprogressbar:library:1.2@aar'
}
Original file line number Diff line number Diff line change
Expand Up @@ -461,16 +461,6 @@ public static void displayGif(File file, GifImageView gifImageView, ImageView im
}
}

public static boolean isLongImg(File file, Bitmap bitmap) {
//TODO file.length()的判断,需要根据OS的版本号做动态调整大小
if (file == null || file.length() == 0) {
return false;
}
if (bitmap.getHeight() > bitmap.getWidth() * 3 || file.length() >= 0.5 * 1024 * 1024) {
return true;
}
return false;
}

/**
* 填充微博列表图片
Expand Down Expand Up @@ -503,7 +493,7 @@ public void onLoadingComplete(String imageUri, View view, Bitmap bitmap) {
longImg.setVisibility(View.INVISIBLE);
norImg.setVisibility(View.INVISIBLE);
displayGif(file, gifImg, imageLabel);
} else if (isLongImg(file, bitmap)) {
} else if (ImageUtil.isLongImg(file, bitmap)) {
longImg.setVisibility(View.VISIBLE);
gifImg.setVisibility(View.INVISIBLE);
norImg.setVisibility(View.INVISIBLE);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.wenming.weiswift.ui.common;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.nostra13.universalimageloader.utils.DiskCacheUtils;
import com.wenming.weiswift.utils.SaveImgUtil;
import com.wenming.weiswift.utils.ToastUtil;

import java.io.File;

/**
* Created by wenmingvs on 2016/9/5.
*/
public class ImageUtil {


public static boolean isLongImg(File file, Bitmap bitmap) {
//TODO file.length()的判断,需要根据OS的版本号做动态调整大小
if (file == null || file.length() == 0) {
return false;
}
if (bitmap.getHeight() > bitmap.getWidth() * 3 || file.length() >= 0.5 * 1024 * 1024) {
return true;
}
return false;
}

public static void saveImg(final Context context, final String url) {
DisplayImageOptions saveOpition = new DisplayImageOptions.Builder()
.cacheOnDisk(true)
.build();
ImageLoader.getInstance().loadImage(url, saveOpition, new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
super.onLoadingComplete(imageUri, view, loadedImage);
File imgFile = DiskCacheUtils.findInCache(url, ImageLoader.getInstance().getDiskCache());
if (imgFile == null) {
ToastUtil.showShort(context, "保存文件失败,请检查SD卡是否已满!");
return;
}
SaveImgUtil.create(context).saveImage(imgFile, loadedImage);
}
});
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,14 @@

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.nostra13.universalimageloader.utils.DiskCacheUtils;
import com.wenming.weiswift.R;
import com.wenming.weiswift.utils.SaveImgUtil;
import com.wenming.weiswift.ui.common.ImageUtil;
import com.wenming.weiswift.utils.ToastUtil;

import java.io.File;

/**
* Created by xiangflight on 2016/4/22.
*/
Expand All @@ -31,10 +24,7 @@ public class ImageDetailDialog extends Dialog {
/**
* 用于加载微博列表图片的配置,进行安全压缩,尽可能的展示图片细节
*/
private static DisplayImageOptions mImageOptions = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(true)
.build();


public ImageDetailDialog(String url, Context context) {
super(context, R.style.ImageSaveDialog);
Expand Down Expand Up @@ -66,18 +56,7 @@ public void onClick(View v) {
@Override
public void onClick(View v) {
dismiss();
ImageLoader.getInstance().loadImage(mImgURL, mImageOptions, new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
super.onLoadingComplete(imageUri, view, loadedImage);
File imgFile = DiskCacheUtils.findInCache(mImgURL, ImageLoader.getInstance().getDiskCache());
if (imgFile == null) {
ToastUtil.showShort(mContext, "保存文件失败,请检查SD卡是否已满!");
return;
}
SaveImgUtil.create(mContext).saveImage(imgFile, loadedImage);
}
});
ImageUtil.saveImg(mContext, mImgURL);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.daimajia.numberprogressbar.NumberProgressBar;
import com.davemorrissey.labs.subscaleview.ImageSource;
import com.davemorrissey.labs.subscaleview.ImageViewState;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
Expand All @@ -21,6 +22,8 @@
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.nostra13.universalimageloader.utils.DiskCacheUtils;
import com.wenming.weiswift.R;
import com.wenming.weiswift.ui.common.ImageUtil;
import com.wenming.weiswift.utils.ToastUtil;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -79,46 +82,41 @@ public boolean isViewFromObject(View view, Object object) {
public Object instantiateItem(ViewGroup container, final int position) {
mView = LayoutInflater.from(container.getContext()).inflate(R.layout.home_weiboitem_imagedetails_item, null);
final RelativeLayout bgLayout = (RelativeLayout) mView.findViewById(R.id.ImageViewItemLayout);
final DonutProgress donutProgress = (DonutProgress) mView.findViewById(R.id.donut_progress);
final NumberProgressBar progressBar = (NumberProgressBar) mView.findViewById(R.id.donut_progress);

final PhotoView preImageView = (PhotoView) mView.findViewById(R.id.previewImg);
final SubsamplingScaleImageView previewLongImg = (SubsamplingScaleImageView) mView.findViewById(R.id.previewLongImg);

final SubsamplingScaleImageView longImg = (SubsamplingScaleImageView) mView.findViewById(R.id.longImg);
final GifImageView gifImageView = (GifImageView) mView.findViewById(R.id.gifView);
final PhotoView norImgView = (PhotoView) mView.findViewById(R.id.norImg);


setOnClickListener(preImageView,bgLayout, longImg, gifImageView, norImgView);
setOnLongClickListener(preImageView,bgLayout, longImg, gifImageView, norImgView, position);
setOnClickListener(preImageView,previewLongImg, bgLayout, longImg, gifImageView, norImgView);
setOnLongClickListener(preImageView,previewLongImg, bgLayout, longImg, gifImageView, norImgView, position);

ImageLoader.getInstance().loadImage(mDatas.get(position), null, options, new SimpleImageLoadingListener() {
@Override
public void onLoadingStarted(String s, View view) {

File bimiddleImg = DiskCacheUtils.findInCache(mDatas.get(position), ImageLoader.getInstance().getDiskCache());

if (bimiddleImg == null) {
donutProgress.setVisibility(View.VISIBLE);
ImageLoader.getInstance().displayImage(mDatas.get(position).replace("large", "bmiddle"), preImageView);
} else {
donutProgress.setVisibility(View.GONE);
}
progressBar.setVisibility(View.VISIBLE);
showPreviewImg(mDatas.get(position).replace("large", "bmiddle"), preImageView, previewLongImg);
}

@Override
public void onLoadingFailed(String s, View view, FailReason failReason) {
donutProgress.setVisibility(View.GONE);
preImageView.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.INVISIBLE);
showPreviewImg(mDatas.get(position).replace("large", "bmiddle"), preImageView, previewLongImg);
}

@Override
public void onLoadingComplete(String s, final View view, Bitmap bitmap) {
preImageView.setVisibility(View.GONE);
hidePreviewImg(preImageView, previewLongImg);
if (mDatas.get(position).endsWith(".gif")) {
gifImageView.setVisibility(View.VISIBLE);
longImg.setVisibility(View.INVISIBLE);
norImgView.setVisibility(View.INVISIBLE);
File file = DiskCacheUtils.findInCache(mDatas.get(position), ImageLoader.getInstance().getDiskCache());
if (file == null){
if (file == null) {
return;
}
displayGif(file, gifImageView);
Expand All @@ -127,38 +125,49 @@ public void onLoadingComplete(String s, final View view, Bitmap bitmap) {
gifImageView.setVisibility(View.INVISIBLE);
norImgView.setVisibility(View.INVISIBLE);
File file = DiskCacheUtils.findInCache(mDatas.get(position), ImageLoader.getInstance().getDiskCache());
if (file == null){
if (file == null) {
return;
}
displayLongPic(file, bitmap, longImg);
displayLongPic(file, longImg);
} else {
norImgView.setVisibility(View.VISIBLE);
gifImageView.setVisibility(View.INVISIBLE);
longImg.setVisibility(View.INVISIBLE);
displayNormalImg(bitmap, norImgView);
}
donutProgress.setProgress(100);
donutProgress.setVisibility(View.GONE);

progressBar.setProgress(100);
progressBar.post(new Runnable() {
@Override
public void run() {
progressBar.setVisibility(View.INVISIBLE);
}
});
}
}, new ImageLoadingProgressListener() {
@Override
public void onProgressUpdate(String s, View view, int current, int total) {
donutProgress.setProgress((int) 100.0f * current / total);
progressBar.setProgress((int) 100.0f * current / total);
}
});
container.addView(mView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
return mView;
}

private void setOnLongClickListener(PhotoView preImageView, final RelativeLayout bgLayout, SubsamplingScaleImageView longImg, GifImageView gifImageView, PhotoView photoView, final int position) {
private void setOnLongClickListener(PhotoView preImageView, SubsamplingScaleImageView previewLongImg, final RelativeLayout bgLayout, SubsamplingScaleImageView longImg, GifImageView gifImageView, PhotoView photoView, final int position) {
preImageView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
showPopWindow(bgLayout, position);
return false;
}
});
previewLongImg.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
showPopWindow(bgLayout, position);
return false;
}
});
longImg.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Expand All @@ -182,7 +191,7 @@ public boolean onLongClick(View v) {
});
}

private void setOnClickListener(PhotoView preImageView, RelativeLayout relativeLayout, SubsamplingScaleImageView longImg, GifImageView gifImageView, PhotoView normalImg) {
private void setOnClickListener(PhotoView preImageView, SubsamplingScaleImageView previewLongImg, RelativeLayout relativeLayout, SubsamplingScaleImageView longImg, GifImageView gifImageView, PhotoView normalImg) {
relativeLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Expand All @@ -200,6 +209,12 @@ public void onOutsidePhotoTap() {
onSingleTagListener.onTag();
}
});
previewLongImg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onSingleTagListener.onTag();
}
});
longImg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Expand Down Expand Up @@ -244,7 +259,7 @@ private void displayNormalImg(Bitmap bitmap, PhotoView photoView) {
photoView.setImageBitmap(bitmap);
}

private void displayLongPic(File file, Bitmap bitmap, SubsamplingScaleImageView longImg) {
private void displayLongPic(File file, SubsamplingScaleImageView longImg) {
longImg.setQuickScaleEnabled(true);
longImg.setZoomEnabled(true);
longImg.setPanEnabled(true);
Expand All @@ -258,5 +273,41 @@ private void showPopWindow(View parent, int position) {
SaveImageDialog.showDialog(mDatas.get(position), mContext);
}

private void showPreviewImg(final String url, final PhotoView preNorImg, final SubsamplingScaleImageView preLongImg) {
if (url.endsWith("gif")) {
return;
}
//加载图片
DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheOnDisk(true)
.cacheInMemory(true)
.build();
ImageLoader.getInstance().displayImage(url, preNorImg, options, new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
File bimiddleImg = DiskCacheUtils.findInCache(url, ImageLoader.getInstance().getDiskCache());
//存储卡空间不足
if (bimiddleImg == null) {
ToastUtil.showShort(mContext, "存储卡空间不足,显示失败");
return;
}
//如果是长图,显示长图
if (ImageUtil.isLongImg(bimiddleImg, loadedImage)) {
preNorImg.setVisibility(View.INVISIBLE);
preLongImg.setVisibility(View.VISIBLE);
displayLongPic(bimiddleImg, preLongImg);
} else {
preNorImg.setVisibility(View.VISIBLE);
preLongImg.setVisibility(View.INVISIBLE);
displayNormalImg(loadedImage, preNorImg);
}
}
});
}

private void hidePreviewImg(PhotoView norImg, SubsamplingScaleImageView longImg) {
norImg.setVisibility(View.GONE);
longImg.setVisibility(View.GONE);
}

}
29 changes: 19 additions & 10 deletions weiSwift/src/main/res/layout/home_weiboitem_imagedetails_item.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ImageViewItemLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">

<uk.co.senab.photoview.PhotoView
<uk.co.senab.photoview.PhotoView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/previewImg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:scaleType="fitCenter"/>
android:scaleType="fitCenter" />

<com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/previewLongImg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true" />


<com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView
android:id="@+id/longImg"
Expand All @@ -33,15 +42,15 @@
android:layout_centerInParent="true"
android:scaleType="fitCenter" />

<com.wenming.weiswift.ui.login.fragment.home.imagedetaillist.DonutProgress xmlns:custom="http://schemas.android.com/apk/res-auto"
<com.daimajia.numberprogressbar.NumberProgressBar
android:id="@+id/donut_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
custom:donut_finished_color="#ffffff"
custom:donut_max="100"
custom:donut_text_color="#ffffff"
custom:donut_unfinished_color="#000000" />
style="@style/NumberProgressBar_Relax_Blue"
android:layout_width="match_parent"
android:layout_height="10dp"
custom:progress_reached_bar_height="10dp"
custom:progress_text_visibility="invisible"
custom:progress_unreached_bar_height="10dp"
custom:progress_unreached_color="@android:color/transparent" />

</RelativeLayout>

0 comments on commit 50bbce0

Please sign in to comment.