Skip to content

Commit

Permalink
优化图片瀑布流显示效果
Browse files Browse the repository at this point in the history
  • Loading branch information
maning committed Oct 18, 2017
1 parent c0bd687 commit cc75249
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
## 更新日志:
### V1.6.7 (2017-8-23):
1.Umeng统计页面的修复
2.修复省份天气获取失败Bug
2.待定...

### V1.6.6 (2017-7-15):
Expand Down
59 changes: 59 additions & 0 deletions app/src/main/java/com/maning/gankmm/bean/PicSizeEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.maning.gankmm.bean;


/**
* Created by maning on 2017/10/18.
* 首页加载图片缓存宽高,防止出现滑动图片改变高度问题
*/

public class PicSizeEntity {

private String picUrl;
private int picWidth;
private int picHeight;

public PicSizeEntity() {
}

public PicSizeEntity(int picWidth, int picHeight) {
this.picWidth = picWidth;
this.picHeight = picHeight;
}

public String getPicUrl() {
return picUrl;
}

public void setPicUrl(String picUrl) {
this.picUrl = picUrl;
}

public int getPicWidth() {
return picWidth;
}

public void setPicWidth(int picWidth) {
this.picWidth = picWidth;
}

public int getPicHeight() {
return picHeight;
}

public void setPicHeight(int picHeight) {
this.picHeight = picHeight;
}

public boolean isNull() {
return picHeight == 0 || picWidth == 0;
}

@Override
public String toString() {
return "PicSizeEntity{" +
"picUrl='" + picUrl + '\'' +
", picWidth=" + picWidth +
", picHeight=" + picHeight +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.Nullable;
import android.support.v4.util.ArrayMap;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
Expand All @@ -24,6 +25,7 @@
import com.ldoublem.thumbUplib.ThumbUpView;
import com.maning.gankmm.R;
import com.maning.gankmm.bean.GankEntity;
import com.maning.gankmm.bean.PicSizeEntity;
import com.maning.gankmm.db.CollectDao;
import com.maning.gankmm.utils.DensityUtil;
import com.maning.gankmm.utils.IntentUtils;
Expand All @@ -37,6 +39,10 @@
import butterknife.Bind;
import butterknife.ButterKnife;

import static android.R.attr.numberPickerStyle;
import static android.R.attr.resource;
import static android.R.attr.width;

/**
* Created by maning on 16/5/17.
* 首页瀑布流图片展示
Expand All @@ -51,17 +57,18 @@ public class RecyclePicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHol
private int screenWidth;
private MyViewHolderHeader myViewHolderHeader;
private RequestOptions options;
//保存图片Size的集合
private ArrayMap<String, PicSizeEntity> picSizeEntityArrayMap = new ArrayMap<>();

public RecyclePicAdapter(Context context, List<GankEntity> commonDataResults) {
this.context = context;
this.commonDataResults = commonDataResults;
layoutInflater = LayoutInflater.from(this.context);
screenWidth = DensityUtil.getWidth(context);

options = new RequestOptions();
options.fitCenter();
options.placeholder(R.drawable.pic_gray_bg);
// options.diskCacheStrategy(DiskCacheStrategy.ALL);
options.diskCacheStrategy(DiskCacheStrategy.ALL);

}

Expand Down Expand Up @@ -91,6 +98,7 @@ public void updateHeadLines(List<GankEntity> headLines) {
notifyDataSetChanged();
}


@Override
public int getItemViewType(int position) {
if (position == 0) {
Expand Down Expand Up @@ -169,12 +177,16 @@ public void onClick(View v) {
//图片显示
String url = resultsEntity.getUrl();

if (resultsEntity.getItemHeight() > 0) {
PicSizeEntity picSizeEntity = picSizeEntityArrayMap.get(resultsEntity.getUrl());
if (picSizeEntity != null && !picSizeEntity.isNull()) {
int width = picSizeEntity.getPicWidth();
int height = picSizeEntity.getPicHeight();
//计算高宽比
int finalHeight = (screenWidth / 2) * height / width;
ViewGroup.LayoutParams layoutParams = viewHolder.rlRoot.getLayoutParams();
layoutParams.height = resultsEntity.getItemHeight();
layoutParams.height = finalHeight;
}

viewHolder.image.setImageResource(R.drawable.pic_gray_bg);
Glide.with(context)
.asBitmap()
.load(url)
Expand All @@ -189,37 +201,20 @@ public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bit

@Override
public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
int width = resource.getWidth();
int height = resource.getHeight();
//计算高宽比
int finalHeight = (screenWidth / 2) * height / width;
if (resultsEntity.getItemHeight() <= 0) {
resultsEntity.setItemHeight(finalHeight);
PicSizeEntity picSizeEntity = picSizeEntityArrayMap.get(resultsEntity.getUrl());
if (picSizeEntity == null || picSizeEntity.isNull()) {
int width = resource.getWidth();
int height = resource.getHeight();
//计算高宽比
int finalHeight = (screenWidth / 2) * height / width;
ViewGroup.LayoutParams layoutParams = viewHolder.rlRoot.getLayoutParams();
layoutParams.height = resultsEntity.getItemHeight();
layoutParams.height = finalHeight;
picSizeEntityArrayMap.put(resultsEntity.getUrl(), new PicSizeEntity(width, height));
}

viewHolder.image.setImageBitmap(resource);
return false;
}
})
.into(viewHolder.image);
// .into(new SimpleTarget<Bitmap>() {
// @Override
// public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
// int width = resource.getWidth();
// int height = resource.getHeight();
// //计算高宽比
// int finalHeight = (screenWidth / 2) * height / width;
// if (resultsEntity.getItemHeight() <= 0) {
// resultsEntity.setItemHeight(finalHeight);
// ViewGroup.LayoutParams layoutParams = viewHolder.rlRoot.getLayoutParams();
// layoutParams.height = resultsEntity.getItemHeight();
// }
//
// viewHolder.image.setImageBitmap(resource);
// }
// });

//查询是否存在收藏
boolean isCollect = new CollectDao().queryOneCollectByID(resultsEntity.get_id());
Expand Down

0 comments on commit cc75249

Please sign in to comment.