因为图片显示的宽高是固定的 所以测量那没有做判断 用 MeasureSpec.EXACTLY
自己写的自定义view 比较一般 如果各位有其他好的想法 可以一起讨论下 不足的地方欢迎大家指正。
public class MergePictureView extends LinearLayout {
public static int MAX_WIDTH = 0;
private List<String> imagesList = new ArrayList<>();
private int pxImagePadding = DiPUtils.dip2px(getContext(), 1);// 图片间的间距
private LayoutParams onePicPara; //一张图的宽高
private LayoutParams twoPicPara; //四张图的宽高
private LayoutParams threePicPare;// 四张图的宽 一张图的高
private LayoutParams fourPicPare;//一张图的宽 四张图的高
public MergePictureView(Context context) {
super(context);
}
public MergePictureView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public MergePictureView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void setImagesList(List<String> imagesList) {
this.imagesList = imagesList;
initImageLayoutParams();
initView();
}
private void initImageLayoutParams() {
onePicPara = new LayoutParams(MAX_WIDTH, MAX_WIDTH);
twoPicPara = new LayoutParams(MAX_WIDTH / 2, MAX_WIDTH / 2);
threePicPare = new LayoutParams(MAX_WIDTH / 2, MAX_WIDTH);
fourPicPare = new LayoutParams(MAX_WIDTH, MAX_WIDTH / 2);
}
private void initView() {
this.setOrientation(HORIZONTAL);
this.removeAllViews();
ImageView imageViewOne;
ImageView imageViewTwo;
ImageView imageViewThree;
ImageView imageViewFour;
if (imagesList.size() == 1) {
imageViewOne = new ImageView(getContext());
imageViewOne.setLayoutParams(onePicPara);
imageViewOne.setScaleType(ImageView.ScaleType.CENTER_CROP);
Glide.with(getContext()).load(imagesList.get(0)).into(imageViewOne);
addView(imageViewOne);
} else if (imagesList.size() == 2) {
imageViewOne = new ImageView(getContext());
imageViewOne.setLayoutParams(threePicPare);
imageViewOne.setPadding(0, 0, pxImagePadding, 0);
imageViewOne.setScaleType(ImageView.ScaleType.CENTER_CROP);
Glide.with(getContext()).load(imagesList.get(0)).into(imageViewOne);
addView(imageViewOne);
imageViewTwo = new ImageView(getContext());
imageViewTwo.setLayoutParams(threePicPare);
imageViewTwo.setPadding(pxImagePadding, 0, 0, 0);
imageViewTwo.setScaleType(ImageView.ScaleType.CENTER_CROP);
Glide.with(getContext()).load(imagesList.get(1)).into(imageViewTwo);
addView(imageViewTwo);
} else if (imagesList.size() == 3) {
imageViewOne = new ImageView(getContext());
imageViewOne.setLayoutParams(threePicPare);
imageViewOne.setPadding(0, 0, pxImagePadding, 0);
imageViewOne.setScaleType(ImageView.ScaleType.CENTER_CROP);
Glide.with(getContext()).load(imagesList.get(0)).into(imageViewOne);
addView(imageViewOne);
LinearLayout linearLayout = new LinearLayout(getContext());
linearLayout.setOrientation(VERTICAL);
linearLayout.setLayoutParams(threePicPare);
linearLayout.setPadding(pxImagePadding, 0, 0, 0);
imageViewTwo = new ImageView(getContext());
imageViewTwo.setLayoutParams(twoPicPara);
imageViewTwo.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageViewTwo.setPadding(0, 0, 0, pxImagePadding);
Glide.with(getContext()).load(imagesList.get(1)).into(imageViewTwo);
linearLayout.addView(imageViewTwo);
imageViewThree = new ImageView(getContext());
imageViewThree.setLayoutParams(twoPicPara);
imageViewThree.setPadding(0, pxImagePadding, 0, 0);
imageViewThree.setScaleType(ImageView.ScaleType.CENTER_CROP);
Glide.with(getContext()).load(imagesList.get(2)).into(imageViewThree);
linearLayout.addView(imageViewThree);
addView(linearLayout);
} else if (imagesList.size() > 3) {
this.setOrientation(VERTICAL);
LinearLayout top = new LinearLayout(getContext());
top.setOrientation(HORIZONTAL);
top.setPadding(0, 0, 0, pxImagePadding);
top.setLayoutParams(fourPicPare);
imageViewOne = new ImageView(getContext());
imageViewOne.setLayoutParams(twoPicPara);
imageViewOne.setPadding(0, 0, pxImagePadding, 0);
imageViewOne.setScaleType(ImageView.ScaleType.CENTER_CROP);
Glide.with(getContext()).load(imagesList.get(0)).into(imageViewOne);
top.addView(imageViewOne);
imageViewTwo = new ImageView(getContext());
imageViewTwo.setLayoutParams(twoPicPara);
imageViewTwo.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageViewTwo.setPadding(pxImagePadding, 0, 0, 0);
Glide.with(getContext()).load(imagesList.get(1)).into(imageViewTwo);
top.addView(imageViewTwo);
addView(top);
LinearLayout bottom = new LinearLayout(getContext());
bottom.setOrientation(HORIZONTAL);
bottom.setLayoutParams(fourPicPare);
bottom.setPadding(0, pxImagePadding, 0, 0);
imageViewThree = new ImageView(getContext());
imageViewThree.setLayoutParams(twoPicPara);
imageViewThree.setPadding(0, 0, pxImagePadding, 0);
imageViewThree.setScaleType(ImageView.ScaleType.CENTER_CROP);
Glide.with(getContext()).load(imagesList.get(2)).into(imageViewThree);
bottom.addView(imageViewThree);
imageViewFour = new ImageView(getContext());
imageViewFour.setLayoutParams(twoPicPara);
imageViewFour.setPadding(pxImagePadding, 0, 0, 0);
imageViewFour.setScaleType(ImageView.ScaleType.CENTER_CROP);
Glide.with(getContext()).load(imagesList.get(3)).into(imageViewFour);
bottom.addView(imageViewFour);
addView(bottom);
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if(MAX_WIDTH==0){
MAX_WIDTH = measureWidth(widthMeasureSpec);
if (imagesList != null && imagesList.size() > 0) {
setImagesList(imagesList);
}
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
private int measureWidth(int measureSpec) {
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
if (specMode == MeasureSpec.EXACTLY) {
// We were told how big to be
result = specSize;
} else {
// Measure the text
// result = (int) mTextPaint.measureText(mText) + getPaddingLeft()
// + getPaddingRight();
if (specMode == MeasureSpec.AT_MOST) {
// Respect AT_MOST value if that was what is called for by
// measureSpec
result = Math.min(result, specSize);
}
}
return result;
}