Commit 5a94723a by 段启岩

自定义布局DynamicGridLayout优化

parent cee53512
package cn.yunliyunwai.beyondclouds.view; package cn.yunliyunwai.beyondclouds.view;
import android.content.Context; import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build; import android.os.Build;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ImageView; import android.widget.ImageView;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi; import androidx.annotation.RequiresApi;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority; 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.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.RequestOptions;
import com.bumptech.glide.request.target.Target;
import java.util.List; import java.util.List;
...@@ -26,6 +33,8 @@ public class DynamicGridLayout extends ViewGroup { ...@@ -26,6 +33,8 @@ public class DynamicGridLayout extends ViewGroup {
private int mCol; private int mCol;
private int mSpacing; private int mSpacing;
List<String> mImgUrls; List<String> mImgUrls;
private boolean isImageInit;
public DynamicGridLayout(Context context) { public DynamicGridLayout(Context context) {
...@@ -54,7 +63,32 @@ public class DynamicGridLayout extends ViewGroup { ...@@ -54,7 +63,32 @@ public class DynamicGridLayout extends ViewGroup {
@Override @Override
protected void onLayout(boolean changed, int l, int t, int r, int b) { 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()) { if (!hasImages()) {
return; return;
} }
...@@ -64,6 +98,11 @@ public class DynamicGridLayout extends ViewGroup { ...@@ -64,6 +98,11 @@ public class DynamicGridLayout extends ViewGroup {
// 每个图片的尺寸 // 每个图片的尺寸
int imageSize = (maxWidth - (mCol - 1) * mSpacing) / mCol; int imageSize = (maxWidth - (mCol - 1) * mSpacing) / mCol;
if (getImageCount() == 1) {
imageSize = (int) (imageSize * 0.66);
}
int layoutHeight = imageSize * mRow + (mRow - 1) * mSpacing; int layoutHeight = imageSize * mRow + (mRow - 1) * mSpacing;
LayoutParams layoutParams = getLayoutParams(); LayoutParams layoutParams = getLayoutParams();
...@@ -75,40 +114,51 @@ public class DynamicGridLayout extends ViewGroup { ...@@ -75,40 +114,51 @@ public class DynamicGridLayout extends ViewGroup {
for (int i = 0; i < mImgUrls.size(); i++) { for (int i = 0; i < mImgUrls.size(); i++) {
currentRow = i / mCol; currentRow = i / mCol;
currentCol = i - (currentRow * mCol); currentCol = i - (currentRow * mCol);
ImageView imageView = new ImageView(this.getContext()); ImageView imageView = createImageView(mImgUrls.get(i));
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);
this.addView(imageView, new LayoutParams(imageSize, imageSize)); this.addView(imageView, new LayoutParams(imageSize, imageSize));
int left = computePosition(currentCol, imageSize); int left = computePosition(currentCol, imageSize);
int top = computePosition(currentRow, imageSize); int top = computePosition(currentRow, imageSize);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.layout(left, top, left + imageSize, top + imageSize); imageView.layout(left, top, left + imageSize, top + imageSize);
} }
isImageInit = true;
} }
private int computePosition(int directionIndex, int imageSize) { private ImageView createImageView(String imgUrl) {
return (imageSize + mSpacing) * directionIndex;
}
public void setImages(List<String> imgUrls) {
removeAllViewsInLayout();
this.mImgUrls = imgUrls;
LayoutParams layoutParams = getLayoutParams();
if (!hasImages()) { ImageView imageView = new ImageView(this.getContext());
layoutParams.height = 0; if (getImageCount() == 1) {
setLayoutParams(layoutParams); imageView.setScaleType(ImageView.ScaleType.FIT_START);
return; } else {
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
} }
RequestOptions options = new RequestOptions()
initGridParams(); .placeholder(R.drawable.placeholder_square)
requestLayout(); .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 @@ ...@@ -15,7 +15,6 @@
<androidx.recyclerview.widget.RecyclerView <androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view" android:id="@+id/recycler_view"
app:fastScrollEnabled="false"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:scrollbars="vertical" /> 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