Commit 5a94723a by 段启岩

自定义布局DynamicGridLayout优化

parent cee53512
package cn.yunliyunwai.beyondclouds.view;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import java.util.List;
......@@ -26,6 +33,8 @@ public class DynamicGridLayout extends ViewGroup {
private int mCol;
private int mSpacing;
List<String> mImgUrls;
private boolean isImageInit;
public DynamicGridLayout(Context context) {
......@@ -54,7 +63,32 @@ public class DynamicGridLayout extends ViewGroup {
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
if (!isImageInit) {
initImages();
}
}
private int computePosition(int directionIndex, int imageSize) {
return (imageSize + mSpacing) * directionIndex;
}
public void setImages(List<String> imgUrls) {
removeAllViewsInLayout();
this.mImgUrls = imgUrls;
LayoutParams layoutParams = getLayoutParams();
isImageInit = false;
if (!hasImages()) {
layoutParams.height = 0;
setLayoutParams(layoutParams);
return;
}
initGridParams();
requestLayout();
}
private void initImages() {
if (!hasImages()) {
return;
}
......@@ -64,6 +98,11 @@ public class DynamicGridLayout extends ViewGroup {
// 每个图片的尺寸
int imageSize = (maxWidth - (mCol - 1) * mSpacing) / mCol;
if (getImageCount() == 1) {
imageSize = (int) (imageSize * 0.66);
}
int layoutHeight = imageSize * mRow + (mRow - 1) * mSpacing;
LayoutParams layoutParams = getLayoutParams();
......@@ -75,40 +114,51 @@ public class DynamicGridLayout extends ViewGroup {
for (int i = 0; i < mImgUrls.size(); i++) {
currentRow = i / mCol;
currentCol = i - (currentRow * mCol);
ImageView imageView = new ImageView(this.getContext());
RequestOptions options = new RequestOptions()
.centerCrop()
.placeholder(R.drawable.placeholder_square)
.error(R.drawable.placeholder_square)
.priority(Priority.HIGH)
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
Glide.with(this).load(mImgUrls.get(i)).apply(options).into(imageView);
ImageView imageView = createImageView(mImgUrls.get(i));
this.addView(imageView, new LayoutParams(imageSize, imageSize));
int left = computePosition(currentCol, imageSize);
int top = computePosition(currentRow, imageSize);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.layout(left, top, left + imageSize, top + imageSize);
}
isImageInit = true;
}
private int computePosition(int directionIndex, int imageSize) {
return (imageSize + mSpacing) * directionIndex;
}
public void setImages(List<String> imgUrls) {
removeAllViewsInLayout();
this.mImgUrls = imgUrls;
LayoutParams layoutParams = getLayoutParams();
private ImageView createImageView(String imgUrl) {
if (!hasImages()) {
layoutParams.height = 0;
setLayoutParams(layoutParams);
return;
ImageView imageView = new ImageView(this.getContext());
if (getImageCount() == 1) {
imageView.setScaleType(ImageView.ScaleType.FIT_START);
} else {
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
}
initGridParams();
requestLayout();
RequestOptions options = new RequestOptions()
.placeholder(R.drawable.placeholder_square)
.error(R.drawable.placeholder_square)
.priority(Priority.HIGH)
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
RequestBuilder builder = Glide.with(this).load(imgUrl).apply(options);
if (getImageCount() == 1) {
builder.addListener(new RequestListener() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(Object resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) {
if (resource instanceof BitmapDrawable) {
LayoutParams layoutParams = getLayoutParams();
layoutParams.height = ((BitmapDrawable) resource).getBitmap().getHeight();
setLayoutParams(layoutParams);
}
return false;
}
});
}
builder.into(imageView);
return imageView;
}
/**
......
......@@ -15,7 +15,6 @@
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
app:fastScrollEnabled="false"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment