Skip to content

Commit

Permalink
减少BitMap加载时候的不必要的消耗
Browse files Browse the repository at this point in the history
  • Loading branch information
郭文明 committed Aug 29, 2016
1 parent 3e91a13 commit 6b31365
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 29 deletions.
143 changes: 117 additions & 26 deletions weiSwift/src/main/java/com/wenming/weiswift/ui/common/FillContent.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.ImageSize;
import com.nostra13.universalimageloader.core.display.CircleBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
Expand Down Expand Up @@ -482,36 +483,126 @@ public static boolean isLongImg(File file, Bitmap bitmap) {
* @param norImg
* @param gifImg
* @param imageLabel
* @param mSingleImageSize
* @param mDoubleImgSize
* @param mThreeImgSize
*/
public static void fillImageList(final Context context, final Status status, DisplayImageOptions options, final int position, final SubsamplingScaleImageView longImg, final ImageView norImg, final GifImageView gifImg, final ImageView imageLabel) {
public static void fillImageList(final Context context, final Status status, DisplayImageOptions options, final int position, final SubsamplingScaleImageView longImg, final ImageView norImg, final GifImageView gifImg, final ImageView imageLabel, ImageSize mSingleImageSize, ImageSize mDoubleImgSize, ImageSize mThreeImgSize) {
final ArrayList<String> urllist = status.bmiddle_pic_urls;
ImageLoader.getInstance().loadImage(urllist.get(position), options, new SimpleImageLoadingListener() {
@Override
public void onLoadingStarted(String s, View view) {
setLabelForGif(urllist.get(position), imageLabel);
}
if (urllist.size() == 1) {
ImageLoader.getInstance().loadImage(urllist.get(position), mSingleImageSize, options, new SimpleImageLoadingListener() {
@Override
public void onLoadingStarted(String s, View view) {
setLabelForGif(urllist.get(position), imageLabel);
}

@Override
public void onLoadingComplete(String imageUri, View view, Bitmap bitmap) {
File file = DiskCacheUtils.findInCache(urllist.get(position), ImageLoader.getInstance().getDiskCache());
if (imageUri.endsWith(".gif")) {
gifImg.setVisibility(View.VISIBLE);
longImg.setVisibility(View.INVISIBLE);
norImg.setVisibility(View.INVISIBLE);
displayGif(file, gifImg, imageLabel);
} else if (isLongImg(file, bitmap)) {
longImg.setVisibility(View.VISIBLE);
gifImg.setVisibility(View.INVISIBLE);
norImg.setVisibility(View.INVISIBLE);
displayLongPic(file, bitmap, longImg, imageLabel);
} else {
norImg.setVisibility(View.VISIBLE);
longImg.setVisibility(View.INVISIBLE);
gifImg.setVisibility(View.INVISIBLE);
displayNorImg(file, bitmap, norImg, imageLabel);
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap bitmap) {
File file = DiskCacheUtils.findInCache(urllist.get(position), ImageLoader.getInstance().getDiskCache());
if (imageUri.endsWith(".gif")) {
gifImg.setVisibility(View.VISIBLE);
longImg.setVisibility(View.INVISIBLE);
norImg.setVisibility(View.INVISIBLE);
displayGif(file, gifImg, imageLabel);
} else if (isLongImg(file, bitmap)) {
longImg.setVisibility(View.VISIBLE);
gifImg.setVisibility(View.INVISIBLE);
norImg.setVisibility(View.INVISIBLE);
displayLongPic(file, bitmap, longImg, imageLabel);
} else {
norImg.setVisibility(View.VISIBLE);
longImg.setVisibility(View.INVISIBLE);
gifImg.setVisibility(View.INVISIBLE);
displayNorImg(file, bitmap, norImg, imageLabel);
}
}
}
});
});
}else if (urllist.size() == 2 || urllist.size() == 4){
ImageLoader.getInstance().loadImage(urllist.get(position), mDoubleImgSize, options, new SimpleImageLoadingListener() {
@Override
public void onLoadingStarted(String s, View view) {
setLabelForGif(urllist.get(position), imageLabel);
}

@Override
public void onLoadingComplete(String imageUri, View view, Bitmap bitmap) {
File file = DiskCacheUtils.findInCache(urllist.get(position), ImageLoader.getInstance().getDiskCache());
if (imageUri.endsWith(".gif")) {
gifImg.setVisibility(View.VISIBLE);
longImg.setVisibility(View.INVISIBLE);
norImg.setVisibility(View.INVISIBLE);
displayGif(file, gifImg, imageLabel);
} else if (isLongImg(file, bitmap)) {
longImg.setVisibility(View.VISIBLE);
gifImg.setVisibility(View.INVISIBLE);
norImg.setVisibility(View.INVISIBLE);
displayLongPic(file, bitmap, longImg, imageLabel);
} else {
norImg.setVisibility(View.VISIBLE);
longImg.setVisibility(View.INVISIBLE);
gifImg.setVisibility(View.INVISIBLE);
displayNorImg(file, bitmap, norImg, imageLabel);
}
}
});
}else if (urllist.size()== 3 || urllist.size() >= 5){
ImageLoader.getInstance().loadImage(urllist.get(position), mThreeImgSize, options, new SimpleImageLoadingListener() {
@Override
public void onLoadingStarted(String s, View view) {
setLabelForGif(urllist.get(position), imageLabel);
}

@Override
public void onLoadingComplete(String imageUri, View view, Bitmap bitmap) {
File file = DiskCacheUtils.findInCache(urllist.get(position), ImageLoader.getInstance().getDiskCache());
if (imageUri.endsWith(".gif")) {
gifImg.setVisibility(View.VISIBLE);
longImg.setVisibility(View.INVISIBLE);
norImg.setVisibility(View.INVISIBLE);
displayGif(file, gifImg, imageLabel);
} else if (isLongImg(file, bitmap)) {
longImg.setVisibility(View.VISIBLE);
gifImg.setVisibility(View.INVISIBLE);
norImg.setVisibility(View.INVISIBLE);
displayLongPic(file, bitmap, longImg, imageLabel);
} else {
norImg.setVisibility(View.VISIBLE);
longImg.setVisibility(View.INVISIBLE);
gifImg.setVisibility(View.INVISIBLE);
displayNorImg(file, bitmap, norImg, imageLabel);
}
}
});
}else {
ImageLoader.getInstance().loadImage(urllist.get(position), options, new SimpleImageLoadingListener() {
@Override
public void onLoadingStarted(String s, View view) {
setLabelForGif(urllist.get(position), imageLabel);
}

@Override
public void onLoadingComplete(String imageUri, View view, Bitmap bitmap) {
File file = DiskCacheUtils.findInCache(urllist.get(position), ImageLoader.getInstance().getDiskCache());
if (imageUri.endsWith(".gif")) {
gifImg.setVisibility(View.VISIBLE);
longImg.setVisibility(View.INVISIBLE);
norImg.setVisibility(View.INVISIBLE);
displayGif(file, gifImg, imageLabel);
} else if (isLongImg(file, bitmap)) {
longImg.setVisibility(View.VISIBLE);
gifImg.setVisibility(View.INVISIBLE);
norImg.setVisibility(View.INVISIBLE);
displayLongPic(file, bitmap, longImg, imageLabel);
} else {
norImg.setVisibility(View.VISIBLE);
longImg.setVisibility(View.INVISIBLE);
gifImg.setVisibility(View.INVISIBLE);
displayNorImg(file, bitmap, norImg, imageLabel);
}
}
});
}

longImg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.ImageSize;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.wenming.weiswift.R;
import com.wenming.weiswift.entity.Status;
import com.wenming.weiswift.ui.common.FillContent;
Expand All @@ -37,13 +39,17 @@ public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ViewHolder>
.cacheInMemory(true)
.cacheOnDisk(true)
.build();
;
private final ImageSize mSingleImageSize;
private final ImageSize mDoubleImgSize;
private final ImageSize mThreeImgSize;

public ImageAdapter(Status status, Context context) {
this.mStatus = status;
this.mData = status.bmiddle_pic_urls;
this.mContext = context;

mSingleImageSize = new ImageSize(ScreenUtil.getScreenWidth(context), (int) (ScreenUtil.getScreenWidth(context) * 0.7));
mDoubleImgSize = new ImageSize(ScreenUtil.getScreenWidth(context) / 2, ScreenUtil.getScreenWidth(context) / 2);
mThreeImgSize = new ImageSize(ScreenUtil.getScreenWidth(context) / 3,ScreenUtil.getScreenWidth(context) / 3);
}

@Override
Expand All @@ -57,7 +63,7 @@ public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
FillContent.fillImageList(mContext, mStatus, options, position, holder.longImg, holder.norImg, holder.gifImg, holder.imageLabel);
FillContent.fillImageList(mContext, mStatus, options, position, holder.longImg, holder.norImg, holder.gifImg, holder.imageLabel,mSingleImageSize,mDoubleImgSize,mThreeImgSize);
}

@Override
Expand Down

0 comments on commit 6b31365

Please sign in to comment.