Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于 使用 Glide 用了 placeholder 死循环解决方案 #51

Open
IHansan opened this issue Oct 16, 2019 · 2 comments
Open

关于 使用 Glide 用了 placeholder 死循环解决方案 #51

IHansan opened this issue Oct 16, 2019 · 2 comments

Comments

@IHansan
Copy link

IHansan commented Oct 16, 2019

@OverRide
protected void onLayout(boolean changed, int l, int t, int r, int b) {
Log.i("loadSquareSmallWallpaperData", "changed=" + changed + ",isonDetachedFromWindow=" + isDetachedFromWindow);
if (changed || isReDraw) {
isReDraw = false;
layoutChildrenView();
}
}

boolean isDetachedFromWindow = false;
boolean isReDraw = false;

@Override
protected void onDetachedFromWindow() {
    super.onDetachedFromWindow();
    isDetachedFromWindow = true;
    Log.i("loadSquareSmallWallpaperData", "onDetachedFromWindow");
}

@Override
protected void onAttachedToWindow() {
    super.onAttachedToWindow();
    if (isDetachedFromWindow) {
        isDetachedFromWindow = false;
        isReDraw = true;

    }
    Log.i("loadSquareSmallWallpaperData", "onAttachedToWindow");
}
@IHansan
Copy link
Author

IHansan commented Oct 17, 2019

经过测试上面依旧有问题 需要改成
@OverRide
protected void onLayout(boolean changed, int l, int t, int r, int b) {
if (changed) {
layoutChildrenView();
} else {
if (isDetachedFromWindow) {
isDetachedFromWindow = false;
layoutChildrenView();
}
}
}

boolean isDetachedFromWindow = false;
@Override
protected void onDetachedFromWindow() {
    super.onDetachedFromWindow();
    isDetachedFromWindow = true;
}

继续优化 GridImageView 注释掉 破坏了 recyclerView 缓存机制
/*
@OverRide
protected void onAttachedToWindow() {
super.onAttachedToWindow();
setImageDrawable(null);
}*/
然后再在 NineGridImageView 上的 setImagesData 方法第一行加上
if (lists == mImgDataList) {
Log.i("NineGridImageView", "退货///////////");
return;
}

@IHansan
Copy link
Author

IHansan commented Oct 17, 2019

最后附上两个类 单图模式已经改为适应图片尺寸 需要设置图片宽高
`package com.jaeger.ninegridimageview;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

/**

  • Created by Jaeger on 16/2/24.

  • Email: [email protected]

  • GitHub: https://github.com/laobie
    */
    public class NineGridImageView extends ViewGroup {
    public final static int STYLE_GRID = 0; // 宫格布局
    public final static int STYLE_FILL = 1; // 全填充布局
    ///////////////////////////////////////////////////////////////////////////
    // 跨行跨列的类型
    ///////////////////////////////////////////////////////////////////////////
    public final static int NOSPAN = 0; // 不跨行也不跨列
    public final static int TOPCOLSPAN = 2; // 首行跨列
    public final static int BOTTOMCOLSPAN = 3; // 末行跨列
    public final static int LEFTROWSPAN = 4; // 首列跨行

    private int mRowCount; // 行数
    private int mColumnCount; // 列数

    private int mMaxSize; // 最大图片数
    private int mShowStyle; // 显示风格
    private int mGap; // 宫格间距
    private int mGridSize; // 宫格大小,即图片大小
    private int mSpanType; // 跨行跨列的类型
    private int mSingleImgWidth;
    private int mSingleImgHeight;
    private int boundarySize;

    private List mImageViewList = new ArrayList<>();
    private List mImgDataList;
    private boolean needReDraw = false;
    private NineGridImageViewAdapter mAdapter;
    private ItemImageClickListener mItemImageClickListener;
    private ItemImageLongClickListener mItemImageLongClickListener;

    public NineGridImageView(Context context) {
    this(context, null);
    }

    public NineGridImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
    TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.NineGridImageView);
    this.mGap = (int) typedArray.getDimension(R.styleable.NineGridImageView_imgGap, 0);
    this.mShowStyle = typedArray.getInt(R.styleable.NineGridImageView_showStyle, STYLE_GRID);
    this.mMaxSize = typedArray.getInt(R.styleable.NineGridImageView_maxSize, 9);
    typedArray.recycle();
    }

    @OverRide
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

     super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    
     int width = MeasureSpec.getSize(widthMeasureSpec);
     int height = MeasureSpec.getSize(heightMeasureSpec);
    
    
     int totalWidth = width - getPaddingLeft() - getPaddingRight();
     if (mImgDataList != null && mImgDataList.size() > 0) {
         if (mImgDataList.size() == 1) {
             boundarySize = (int) (totalWidth * 0.8f);
             if (mSingleImgWidth > mSingleImgHeight * 1.2 && mSingleImgWidth > boundarySize) {
                 float ratio = (float) totalWidth / (float) mSingleImgWidth;
                 height = (int) (mSingleImgHeight * ratio) + getPaddingTop() + getPaddingBottom();
             } else {
                 if (mSingleImgWidth > mSingleImgHeight) {
                     if (mSingleImgWidth > boundarySize) {
                         float ratio = (float) boundarySize / (float) mSingleImgWidth;
                         width = boundarySize + getPaddingLeft() + getPaddingRight();
                         height = (int) (mSingleImgHeight * ratio) + getPaddingTop() + getPaddingBottom();
                     } else {
                         width = mSingleImgWidth + getPaddingLeft() + getPaddingRight();
                         height = mSingleImgHeight + getPaddingTop() + getPaddingBottom();
                     }
                 } else {
                     width = (int) (totalWidth * 0.65f) + getPaddingLeft() + getPaddingRight();
                     float ratio = width / (float) mSingleImgWidth;
                     height = (int) (mSingleImgHeight * ratio * 0.8f) + getPaddingTop() + getPaddingBottom();
                     if (height > totalWidth * 0.8) {
                         height = (int) (totalWidth * 0.8);
                     }
                 }
             }
         } else {
             mImageViewList.get(0).setScaleType(ImageView.ScaleType.CENTER_CROP);
             mGridSize = (totalWidth - mGap * (mColumnCount - 1)) / mColumnCount;
             height = mGridSize * mRowCount + mGap * (mRowCount - 1) + getPaddingTop() + getPaddingBottom();
         }
     }
     setMeasuredDimension(width, height);
    

    }

    @OverRide
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
    if (changed) {
    layoutChildrenView();
    } else {
    if (isDetachedFromWindow) {
    isDetachedFromWindow = false;
    layoutChildrenView();
    }
    }
    }

    boolean isDetachedFromWindow = false;
    @OverRide
    protected void onDetachedFromWindow() {
    super.onDetachedFromWindow();
    isDetachedFromWindow = true;
    }

    /**

    • 根据照片数量和span类型来对子视图进行动态排版布局
      */
      private void layoutChildrenView() {
      if (mImgDataList == null) return;
      int showChildrenCount = getNeedShowCount(mImgDataList.size());
      if (showChildrenCount == 1) {
      layoutForSingleChildrenView();
      return;
      }
      //对不跨行不跨列的进行排版布局,单张或者2张默认进行普通排版
      if (mSpanType == NOSPAN) {
      layoutForNoSpanChildrenView(showChildrenCount);
      return;
      }
      switch (showChildrenCount) {
      case 3:
      layoutForThreeChildrenView(showChildrenCount);
      break;
      case 4:
      layoutForFourChildrenView(showChildrenCount);
      break;
      case 5:
      layoutForFiveChildrenView(showChildrenCount);
      break;
      case 6:
      layoutForSixChildrenView(showChildrenCount);
      break;

       default:
           layoutForNoSpanChildrenView(showChildrenCount);
           break;
      

      }
      }

    private void layoutForSingleChildrenView() {
    ImageView childrenView = (ImageView) getChildAt(0);
    int left, top, right, bottom;
    left = getPaddingLeft();
    top = getPaddingTop();
    right = getWidth() - getPaddingRight() - getPaddingLeft();
    bottom = getHeight() - getPaddingBottom() - getPaddingTop();
    childrenView.layout(left, top, right, bottom);
    if (mAdapter != null) {
    mAdapter.onDisplayImage(getContext(), childrenView, mImgDataList.get(0));
    }

    }

    private void layoutForNoSpanChildrenView(int childrenCount) {

     if (childrenCount <= 0) return;
     int row, column, left, top, right, bottom;
     for (int i = 0; i < childrenCount; i++) {
         ImageView childrenView = (ImageView) getChildAt(i);
         row = i / mColumnCount;
         column = i % mColumnCount;
         left = (mGridSize + mGap) * column + getPaddingLeft();
         top = (mGridSize + mGap) * row + getPaddingTop();
         right = left + mGridSize;
         bottom = top + mGridSize;
         childrenView.layout(left, top, right, bottom);
         if (mAdapter != null) {
             mAdapter.onDisplayImage(getContext(), childrenView, mImgDataList.get(i));
         }
     }
    

    }

    private void layoutForThreeChildrenView(int childrenCount) {
    int left, top, right, bottom;
    for (int i = 0; i < childrenCount; i++) {
    ImageView childrenView = (ImageView) getChildAt(i);
    switch (mSpanType) {
    case TOPCOLSPAN: //2行2列,首行跨列
    if (i == 0) {
    left = getPaddingLeft();
    top = getPaddingTop();
    right = left + mGridSize * 2 + mGap;
    bottom = top + mGridSize;
    } else if (i == 1) {
    left = getPaddingLeft();
    top = getPaddingTop() + mGridSize + mGap;
    right = left + mGridSize;
    bottom = top + mGridSize;
    } else {
    left = getPaddingLeft() + mGridSize + mGap;
    top = getPaddingTop() + mGridSize + mGap;
    right = left + mGridSize;
    bottom = top + mGridSize;
    }
    childrenView.layout(left, top, right, bottom);
    break;
    case BOTTOMCOLSPAN: //2行2列,末行跨列
    if (i == 0) {
    left = getPaddingLeft();
    top = getPaddingTop();
    right = left + mGridSize;
    bottom = top + mGridSize;
    } else if (i == 1) {
    left = getPaddingLeft() + mGridSize + mGap;
    top = getPaddingTop();
    right = left + mGridSize;
    bottom = top + mGridSize;
    } else {
    left = getPaddingLeft();
    top = getPaddingTop() + mGridSize + mGap;
    right = left + mGridSize * 2 + mGap;
    bottom = top + mGridSize;
    }
    childrenView.layout(left, top, right, bottom);
    break;
    case LEFTROWSPAN: //2行2列,首列跨行
    if (i == 0) {
    left = getPaddingLeft();
    top = getPaddingTop();
    right = left + mGridSize;
    bottom = top + mGridSize * 2 + mGap;
    } else if (i == 1) {
    left = getPaddingLeft() + mGridSize + mGap;
    top = getPaddingTop();
    right = left + mGridSize;
    bottom = top + mGridSize;
    } else {
    left = getPaddingLeft() + mGridSize + mGap;
    top = getPaddingTop() + mGridSize + mGap;
    right = left + mGridSize;
    bottom = top + mGridSize;
    }
    childrenView.layout(left, top, right, bottom);
    break;
    default:
    break;
    }
    if (mAdapter != null) {
    mAdapter.onDisplayImage(getContext(), childrenView, mImgDataList.get(i));
    }
    }
    }

    private void layoutForFourChildrenView(int childrenCount) {
    int left, top, right, bottom;
    for (int i = 0; i < childrenCount; i++) {
    ImageView childrenView = (ImageView) getChildAt(i);
    switch (mSpanType) {
    case TOPCOLSPAN: //3行3列,首行跨2行3列
    if (i == 0) {
    left = getPaddingLeft();
    top = getPaddingTop();
    right = left + mGridSize * 3 + mGap * 2;
    bottom = top + mGridSize * 2 + mGap;
    } else if (i == 1) {
    left = getPaddingLeft();
    top = getPaddingTop() + mGridSize * 2 + mGap * 2;
    right = left + mGridSize;
    bottom = top + mGridSize;
    } else if (i == 2) {
    left = getPaddingLeft() + mGridSize + mGap;
    top = getPaddingTop() + mGridSize * 2 + mGap * 2;
    right = left + mGridSize;
    bottom = top + mGridSize;
    } else {
    left = getPaddingLeft() + mGridSize * 2 + mGap * 2;
    top = getPaddingTop() + mGridSize * 2 + mGap * 2;
    right = left + mGridSize;
    bottom = top + mGridSize;
    }
    childrenView.layout(left, top, right, bottom);
    break;
    case BOTTOMCOLSPAN: //3行3列,末行跨2行3列
    if (i == 0) {
    left = getPaddingLeft();
    top = getPaddingTop();
    right = left + mGridSize;
    bottom = top + mGridSize;
    } else if (i == 1) {
    left = getPaddingLeft() + mGridSize + mGap;
    top = getPaddingTop();
    right = left + mGridSize;
    bottom = top + mGridSize;
    } else if (i == 2) {
    left = getPaddingLeft() + mGridSize * 2 + mGap * 2;
    top = getPaddingTop();
    right = left + mGridSize;
    bottom = top + mGridSize;
    } else {
    left = getPaddingLeft();
    top = getPaddingTop() + mGridSize + mGap;
    right = left + mGridSize * 3 + mGap * 2;
    bottom = top + mGridSize * 2 + mGap;
    }
    childrenView.layout(left, top, right, bottom);
    break;
    case LEFTROWSPAN: //3行3列,首列跨3行2列
    if (i == 0) {
    left = getPaddingLeft();
    top = getPaddingTop();
    right = left + mGridSize * 2 + mGap;
    bottom = top + mGridSize * 3 + mGap * 2;
    } else if (i == 1) {
    left = getPaddingLeft() + mGridSize * 2 + mGap * 2;
    top = getPaddingTop();
    right = left + mGridSize;
    bottom = top + mGridSize;
    } else if (i == 2) {
    left = getPaddingLeft() + mGridSize * 2 + mGap * 2;
    top = getPaddingTop() + mGridSize + mGap;
    right = left + mGridSize;
    bottom = top + mGridSize;
    } else {
    left = getPaddingLeft() + mGridSize * 2 + mGap * 2;
    top = getPaddingTop() + mGridSize * 2 + mGap * 2;
    right = left + mGridSize;
    bottom = top + mGridSize;
    }
    childrenView.layout(left, top, right, bottom);
    break;
    default:
    break;
    }
    if (mAdapter != null) {
    mAdapter.onDisplayImage(getContext(), childrenView, mImgDataList.get(i));
    }
    }
    }

    private void layoutForFiveChildrenView(int childrenCount) {
    int left, top, right, bottom;
    for (int i = 0; i < childrenCount; i++) {
    ImageView childrenView = (ImageView) getChildAt(i);
    switch (mSpanType) {
    case TOPCOLSPAN: //3行3列,首行跨2行,2列跨3列
    if (i == 0) {
    left = getPaddingLeft();
    top = getPaddingTop();
    right = left + (mGridSize * 3 + mGap) / 2;
    bottom = top + mGridSize * 2 + mGap;
    } else if (i == 1) {
    left = getPaddingLeft() + (mGridSize * 3 + mGap) / 2 + mGap;
    top = getPaddingTop();
    right = left + (mGridSize * 3 + mGap) / 2;
    bottom = top + mGridSize * 2 + mGap;
    } else if (i == 2) {
    left = getPaddingLeft();
    top = getPaddingTop() + mGridSize * 2 + mGap * 2;
    right = left + mGridSize;
    bottom = top + mGridSize;
    } else if (i == 3) {
    left = getPaddingLeft() + mGridSize + mGap;
    top = getPaddingTop() + mGridSize * 2 + mGap * 2;
    right = left + mGridSize;
    bottom = top + mGridSize;
    } else {
    left = getPaddingLeft() + mGridSize * 2 + mGap * 2;
    top = getPaddingTop() + mGridSize * 2 + mGap * 2;
    right = left + mGridSize;
    bottom = top + mGridSize;
    }
    childrenView.layout(left, top, right, bottom);
    break;
    case BOTTOMCOLSPAN: //3行3列,末行跨2行,2列跨3列
    if (i == 0) {
    left = getPaddingLeft();
    top = getPaddingTop();
    right = left + mGridSize;
    bottom = top + mGridSize;
    } else if (i == 1) {
    left = getPaddingLeft() + mGridSize + mGap;
    top = getPaddingTop();
    right = left + mGridSize;
    bottom = top + mGridSize;
    } else if (i == 2) {
    left = getPaddingLeft() + mGridSize * 2 + mGap * 2;
    top = getPaddingTop();
    right = left + mGridSize;
    bottom = top + mGridSize;
    } else if (i == 3) {
    left = getPaddingLeft();
    top = getPaddingTop() + mGridSize + mGap;
    right = left + (mGridSize * 3 + mGap) / 2;
    bottom = top + mGridSize * 2 + mGap;
    } else {
    left = getPaddingLeft() + (mGridSize * 3 + mGap) / 2 + mGap;
    top = getPaddingTop() + mGridSize + mGap;
    right = left + (mGridSize * 3 + mGap) / 2;
    bottom = top + mGridSize * 2 + mGap;
    }
    childrenView.layout(left, top, right, bottom);
    break;
    case LEFTROWSPAN: //3行3列,2行跨3行,1列
    if (i == 0) {
    left = getPaddingLeft();
    top = getPaddingTop();
    right = left + mGridSize * 2 + mGap;
    bottom = top + (mGridSize * 3 + mGap) / 2;
    } else if (i == 1) {
    left = getPaddingLeft();
    top = getPaddingTop() + (mGridSize * 3 + mGap) / 2 + mGap;
    right = left + mGridSize * 2 + mGap;
    bottom = top + (mGridSize * 3 + mGap) / 2;
    } else if (i == 2) {
    left = getPaddingLeft() + mGridSize * 2 + mGap * 2;
    top = getPaddingTop();
    right = left + mGridSize;
    bottom = top + mGridSize;
    } else if (i == 3) {
    left = getPaddingLeft() + mGridSize * 2 + mGap * 2;
    top = getPaddingTop() + mGridSize + mGap;
    right = left + mGridSize;
    bottom = top + mGridSize;
    } else {
    left = getPaddingLeft() + mGridSize * 2 + mGap * 2;
    top = getPaddingTop() + mGridSize * 2 + mGap * 2;
    right = left + mGridSize;
    bottom = top + mGridSize;
    }
    childrenView.layout(left, top, right, bottom);
    break;
    default:
    break;
    }
    if (mAdapter != null) {
    mAdapter.onDisplayImage(getContext(), childrenView, mImgDataList.get(i));
    }
    }
    }

    private void layoutForSixChildrenView(int childrenCount) {
    int left, top, right, bottom;
    for (int i = 0; i < childrenCount; i++) {
    ImageView childrenView = (ImageView) getChildAt(i);
    switch (mSpanType) {
    case TOPCOLSPAN: //3行3列,第一张跨2行2列
    if (i == 0) {
    left = getPaddingLeft();
    top = getPaddingTop();
    right = left + mGridSize * 2 + mGap;
    bottom = top + mGridSize * 2 + mGap;
    } else if (i == 1) {
    left = getPaddingLeft() + mGridSize * 2 + mGap * 2;
    top = getPaddingTop();
    right = left + mGridSize;
    bottom = top + mGridSize;
    } else if (i == 2) {
    left = getPaddingLeft() + mGridSize * 2 + mGap * 2;
    top = getPaddingTop() + mGridSize + mGap;
    right = left + mGridSize;
    bottom = top + mGridSize;
    } else if (i == 3) {
    left = getPaddingLeft();
    top = getPaddingTop() + mGridSize * 2 + mGap * 2;
    right = left + mGridSize;
    bottom = top + mGridSize;
    } else if (i == 4) {
    left = getPaddingLeft() + mGridSize + mGap;
    top = getPaddingTop() + mGridSize * 2 + mGap * 2;
    right = left + mGridSize;
    bottom = top + mGridSize;
    } else {
    left = getPaddingLeft() + mGridSize * 2 + mGap * 2;
    top = getPaddingTop() + mGridSize * 2 + mGap * 2;
    right = left + mGridSize;
    bottom = top + mGridSize;
    }
    childrenView.layout(left, top, right, bottom);
    break;
    case BOTTOMCOLSPAN: //3行3列,第4张跨2行2列
    if (i == 0) {
    left = getPaddingLeft();
    top = getPaddingTop();
    right = left + mGridSize;
    bottom = top + mGridSize;
    } else if (i == 1) {
    left = getPaddingLeft() + mGridSize + mGap;
    top = getPaddingTop();
    right = left + mGridSize;
    bottom = top + mGridSize;
    } else if (i == 2) {
    left = getPaddingLeft() + mGridSize * 2 + mGap * 2;
    top = getPaddingTop();
    right = left + mGridSize;
    bottom = top + mGridSize;
    } else if (i == 3) {
    left = getPaddingLeft();
    top = getPaddingTop() + mGridSize + mGap;
    right = left + mGridSize * 2 + mGap;
    bottom = top + mGridSize * 2 + mGap;
    } else if (i == 4) {
    left = getPaddingLeft() + mGridSize * 2 + mGap * 2;
    top = getPaddingTop() + mGridSize + mGap;
    right = left + mGridSize;
    bottom = top + mGridSize;
    } else {
    left = getPaddingLeft() + mGridSize * 2 + mGap * 2;
    top = getPaddingTop() + mGridSize * 2 + mGap * 2;
    right = left + mGridSize;
    bottom = top + mGridSize;
    }
    childrenView.layout(left, top, right, bottom);
    break;
    case LEFTROWSPAN: //3行3列,第2张跨2行2列
    if (i == 0) {
    left = getPaddingLeft();
    top = getPaddingTop();
    right = left + mGridSize;
    bottom = top + mGridSize;
    } else if (i == 1) {
    left = getPaddingLeft() + mGridSize + mGap;
    top = getPaddingTop();
    right = left + mGridSize * 2 + mGap;
    bottom = top + mGridSize * 2 + mGap;
    } else if (i == 2) {
    left = getPaddingLeft();
    top = getPaddingTop() + mGridSize + mGap;
    right = left + mGridSize;
    bottom = top + mGridSize;
    } else if (i == 3) {
    left = getPaddingLeft();
    top = getPaddingTop() + mGridSize * 2 + mGap * 2;
    right = left + mGridSize;
    bottom = top + mGridSize;
    } else if (i == 4) {
    left = getPaddingLeft() + mGridSize + mGap;
    top = getPaddingTop() + mGridSize * 2 + mGap * 2;
    right = left + mGridSize;
    bottom = top + mGridSize;
    } else {
    left = getPaddingLeft() + mGridSize * 2 + mGap * 2;
    top = getPaddingTop() + mGridSize * 2 + mGap * 2;
    right = left + mGridSize;
    bottom = top + mGridSize;
    }
    childrenView.layout(left, top, right, bottom);
    break;
    default:
    break;
    }
    if (mAdapter != null) {
    mAdapter.onDisplayImage(getContext(), childrenView, mImgDataList.get(i));
    }
    }
    }

    /**

    • 根据跨行跨列的类型,以及图片数量,来确定单元格的行数和列数
    • @param imagesSize 图片数量
    • @param gridParam 单元格的行数和列数
      */
      private void generatUnitRowAndColumnForSpanType(int imagesSize, int[] gridParam) {
      if (imagesSize <= 2) {
      gridParam[0] = 1;
      gridParam[1] = imagesSize;
      } else if (imagesSize == 3) {
      switch (mSpanType) {
      case TOPCOLSPAN: //2行2列,首行跨列
      case BOTTOMCOLSPAN: //2行2列,末行跨列
      case LEFTROWSPAN: //2行2列,首列跨行
      gridParam[0] = 2;
      gridParam[1] = 2;
      break;
      case NOSPAN: //1行3列
      default:
      gridParam[0] = 1;
      gridParam[1] = 3;
      break;
      }
      } else if (imagesSize <= 6) {
      switch (mSpanType) {
      case TOPCOLSPAN: //3行3列,首行跨列
      case BOTTOMCOLSPAN: //3行3列,末行跨列
      case LEFTROWSPAN: //3行3列,首列跨行
      gridParam[0] = 3;
      gridParam[1] = 3;
      break;
      case NOSPAN: //2行
      default:
      gridParam[0] = 2;
      gridParam[1] = imagesSize / 2 + imagesSize % 2;
      break;
      }
      } else {
      gridParam[0] = imagesSize / 3 + (imagesSize % 3 == 0 ? 0 : 1);
      gridParam[1] = 3;
      }
      }

    public void setImagesData(List lists) {
    setImagesData(lists, NOSPAN);
    }

    /**

    • 设置图片数据

    • @param lists 图片数据集合

    • @param spanType 跨行跨列排版类型
      */
      public void setImagesData(List lists, int spanType) {
      if (lists == mImgDataList) {
      Log.i("NineGridImageView", "退货///////////");
      return;
      }
      if (lists == null || lists.isEmpty()) {
      this.setVisibility(GONE);
      return;
      } else {
      this.setVisibility(VISIBLE);
      }

      this.mSpanType = spanType;
      int newShowCount = getNeedShowCount(lists.size());

      int[] gridParam = calculateGridParam(newShowCount, mShowStyle);
      mRowCount = gridParam[0];
      mColumnCount = gridParam[1];
      if (mImgDataList == null) {
      int i = 0;
      while (i < newShowCount) {
      ImageView iv = getImageView(i);
      if (iv == null) {
      return;
      }
      addView(iv, generateDefaultLayoutParams());
      i++;
      }
      } else {
      int oldShowCount = getNeedShowCount(mImgDataList.size());
      if (oldShowCount > newShowCount) {
      removeViews(newShowCount, oldShowCount - newShowCount);
      } else if (oldShowCount < newShowCount) {
      for (int i = oldShowCount; i < newShowCount; i++) {
      ImageView iv = getImageView(i);
      if (iv == null) {
      return;
      }
      addView(iv, generateDefaultLayoutParams());
      }
      }
      }
      if (mImgDataList == null || mImgDataList != lists) {
      needReDraw = true;
      } else {
      needReDraw = false;
      }
      mImgDataList = lists;
      requestLayout();
      }

    private int getNeedShowCount(int size) {
    if (mMaxSize > 0 && size > mMaxSize) {
    return mMaxSize;
    } else {
    return size;
    }
    }

    /**

    • 获得 ImageView
    • 保证了 ImageView 的重用
    • @param position 位置
      */
      private ImageView getImageView(final int position) {
      if (position < mImageViewList.size()) {
      return mImageViewList.get(position);
      } else {
      if (mAdapter != null) {
      ImageView imageView = mAdapter.generateImageView(getContext());
      mImageViewList.add(imageView);
      imageView.setOnClickListener(new OnClickListener() {
      @OverRide
      public void onClick(View v) {
      mAdapter.onItemImageClick(getContext(), (ImageView) v, position, mImgDataList);
      if (mItemImageClickListener != null) {
      mItemImageClickListener.onItemImageClick(getContext(), (ImageView) v, position, mImgDataList);
      }
      }
      });
      imageView.setOnLongClickListener(new OnLongClickListener() {
      @OverRide
      public boolean onLongClick(View v) {
      boolean consumedEvent = mAdapter.onItemImageLongClick(getContext(), (ImageView) v, position, mImgDataList);
      if (mItemImageLongClickListener != null) {
      consumedEvent = mItemImageLongClickListener.onItemImageLongClick(getContext(), (ImageView) v, position, mImgDataList) || consumedEvent;
      }
      return consumedEvent;
      }
      });
      return imageView;
      } else {
      Log.e("NineGirdImageView", "Your must set a NineGridImageViewAdapter for NineGirdImageView");
      return null;
      }
      }
      }

    /**

    • 设置 宫格参数
    • @param imagesSize 图片数量
    • @param showStyle 显示风格
    • @return 宫格参数 gridParam[0] 宫格行数 gridParam[1] 宫格列数
      */
      protected int[] calculateGridParam(int imagesSize, int showStyle) {
      int[] gridParam = new int[2];
      switch (showStyle) {
      case STYLE_FILL:
      generatUnitRowAndColumnForSpanType(imagesSize, gridParam);
      break;
      default:
      case STYLE_GRID:
      gridParam[0] = imagesSize / 3 + (imagesSize % 3 == 0 ? 0 : 1);
      gridParam[1] = 3;
      }
      return gridParam;
      }

    /**

    • 设置适配器
    • @param adapter 适配器
      */
      public void setAdapter(NineGridImageViewAdapter adapter) {
      mAdapter = adapter;
      }

    /**

    • 设置宫格间距
    • @param gap 宫格间距 px
      */
      public void setGap(int gap) {
      mGap = gap;
      }

    /**

    • 设置显示风格
    • @param showStyle 显示风格
      */
      public void setShowStyle(int showStyle) {
      mShowStyle = showStyle;
      }

    public void setSingleImgSize(int mSingleImgWidth, int mSingleImgHeight) {
    this.mSingleImgWidth = mSingleImgWidth;
    this.mSingleImgHeight = mSingleImgHeight;
    }

    /**

    • 设置最大图片数
    • @param maxSize 最大图片数
      */
      public void setMaxSize(int maxSize) {
      mMaxSize = maxSize;
      }

    public void setItemImageClickListener(ItemImageClickListener itemImageViewClickListener) {
    mItemImageClickListener = itemImageViewClickListener;
    }

    public void setItemImageLongClickListener(ItemImageLongClickListener itemImageViewLongClickListener) {
    mItemImageLongClickListener = itemImageViewLongClickListener;
    }
    } package com.jaeger.ninegridimageview;

import android.content.Context;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;

/**

  • Created by Jaeger on 16/2/24.

  • Email: [email protected]

  • GitHub: https://github.com/laobie
    */
    public class GridImageView extends ImageView {

    public GridImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
    }

    public GridImageView(Context context) {
    super(context);
    }

    @OverRide
    public boolean onTouchEvent(MotionEvent event) {

     switch (event.getAction()) {
         case MotionEvent.ACTION_DOWN:
             Drawable drawable = getDrawable();
             if (drawable != null) {
                 drawable.mutate().setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
             }
             break;
         case MotionEvent.ACTION_MOVE:
             break;
         case MotionEvent.ACTION_CANCEL:
         case MotionEvent.ACTION_UP:
             Drawable drawableUp = getDrawable();
             if (drawableUp != null) {
                 drawableUp.mutate().clearColorFilter();
             }
             break;
     }
    
     return super.onTouchEvent(event);
    

    }
    /*
    @OverRide
    protected void onAttachedToWindow() {
    super.onAttachedToWindow();
    setImageDrawable(null);
    }*/
    }`

最后Glide圆角方案:
`public static void loadSquareSmallWallpaperData(Context context, ImageView imageView, String path) {
try {
if (Util.isOnMainThread()) {
RequestOptions options = new RequestOptions()
.placeholder(transformDrawable(context,R.color.color_white_f2,imageView.getWidth(),imageView.getHeight(),new GlideRoundTransform(6)))
.transform(new GlideRoundTransform(6))
.diskCacheStrategy(DiskCacheStrategy.ALL);
String corpStr;
if (Math.max(imageView.getWidth(), imageView.getHeight()) > 320) {
corpStr = "?imageView2/2/w/640/h/640";
} else {
corpStr = "?imageView2/2/w/320/h/320";
}
Glide.with(context)
.asBitmap()
.load(path + corpStr)
.transition(new BitmapTransitionOptions().crossFade(350))
.apply(options)
.into(imageView);
}
} catch (Exception e) {
e.printStackTrace();
}
}

private static BitmapDrawable transformDrawable(Context context,
                                                int resourceId, int w, int h,
                                                Transformation<Bitmap> transformation) {
    Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565);
    Canvas canvas = new Canvas(bitmap);
    canvas.drawColor(ContextCompat.getColor(context, resourceId));
    //make
    Resource<Bitmap> original = BitmapResource.obtain(bitmap, Glide.get(context).getBitmapPool());
    Resource<Bitmap> rounded = transformation.transform(context, original, bitmap.getWidth(), bitmap.getHeight());

    if (original != rounded) {
        original.recycle();
    }
    return new BitmapDrawable(context.getResources(), rounded.get());
}`

`import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;

import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.bumptech.glide.load.resource.bitmap.TransformationUtils;

import java.security.MessageDigest;

public class GlideRoundTransform extends BitmapTransformation {
private static final int VERSION = 1;
private static float radius = 0f;
private static final String ID = "{你的包名}.common.GlideRoundTransform." + VERSION;
private static final byte[] ID_BYTES = ID.getBytes(CHARSET);

public GlideRoundTransform(int radius) {
    this.radius = Resources.getSystem().getDisplayMetrics().density * radius;
}


@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
    Bitmap bitmap = TransformationUtils.centerCrop(pool, toTransform, outWidth, outHeight);
    return roundCrop(pool, bitmap);
}


private static Bitmap roundCrop(BitmapPool pool, Bitmap source) {
    if (source == null) return null;
    Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
    if (result == null) {
        result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
    }

    Canvas canvas = new Canvas(result);
    Paint paint = new Paint();
    paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
    paint.setAntiAlias(true);
    RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
    canvas.drawRoundRect(rectF, radius, radius, paint);
    return result;
}

public String getId() {
    return getClass().getName() + Math.round(radius);
}

@Override
public void updateDiskCacheKey(MessageDigest messageDigest) {
    messageDigest.update(ID_BYTES);
}

}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant