int = imageWith = (ScreenUtil.getScreenWidth() - ScreenUtil.dip2px(120)) / 3
private void addImageViewToContainer(String url, final int index, final PostModel data, int totalCount) {
ImageView imageView = new ImageView(mContext);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
if (index == 0) {//第一个image不要左边距
params.leftMargin = 0;
} else {//其余的左边距为4dp
params.leftMargin = ScreenUtil.dip2px(4);
}
if (totalCount == 1) {//只有一张图时,设置图片的最大宽度和高度为200dp
imageView.setMaxHeight(ScreenUtil.dip2px(200));
imageView.setMaxWidth(ScreenUtil.dip2px(200));
//*****当图片宽高小于imageWith时,我本来想让它宽高都按imageWith显示,可是这里设置imageView.setMinimumHeight(imageWith)是没有用的。现在///解决不了这个问题,我看微信朋友圈,当图片宽高小于imageWith时,让图片以最大尺寸显示,也就是以ScreenUtil.dip2px(200)作为宽高显示,怎么实///现呢
imageView.setMinimumHeight(imageWith);
imageView.setMinimumWidth(imageWith);
LogUtil.e("imageWith", imageWith+"");
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setAdjustViewBounds(true);
imageContainer.addView(imageView, index);
ImageLoader.getInstance().displayImage(url, new ImageViewAware(imageView), imageOptions,
new ImageSize(ScreenUtil.dip2px(200), ScreenUtil.dip2px(200)), null, null);
} else {
params.height = imageWith;
params.width = imageWith;
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(params);
if (totalCount == index + 1 && data.getPostViewModel().getImageList().size() > 3) {//超过3张图时,最后一张图,添加Relate布局,和共几张图
RelativeLayout relativeLayout = new RelativeLayout(mContext);
// RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(imageWith, imageWith);
//// layoutParams.leftMargin = ScreenUtil.dip2px(4);
relativeLayout.setLayoutParams(params);
TTTextView imageCount = new TTTextView(mContext);
imageCount.setBackgroundColor(Color.BLACK);
imageCount.setAlpha(0.6f);
imageCount.setPadding(ScreenUtil.dip2px(2), ScreenUtil.dip2px(2), ScreenUtil.dip2px(2), ScreenUtil.dip2px(2));
imageCount.setGravity(Gravity.CENTER_VERTICAL);
imageCount.setTextColor(mContext.getResources().getColor(R.color.color_white));
imageCount.setTextSize(TypedValue.COMPLEX_UNIT_PX, mContext.getResources().getDimension(R.dimen.default_font_size_very_smallest));
imageCount.setText("共" + data.getPostViewModel().getImageList().size() + "张");
RelativeLayout.LayoutParams textLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
textLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
textLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
imageCount.setLayoutParams(textLayoutParams);
relativeLayout.addView(imageView);
relativeLayout.addView(imageCount);
imageContainer.addView(relativeLayout, index);
} else {
imageContainer.addView(imageView, index);
}
ImageLoader.getInstance().displayImage(url, new ImageViewAware(imageView), imageOptions,
new ImageSize(imageWith, imageWith), null, null);
}
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(mContext, PreviewActivity.class);
intent.putExtra("data", (Serializable) data.getPostViewModel().getImageList());
intent.putExtra("index", index);
mContext.startActivity(intent);
}
});
}