您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關怎么在Android中實現微信群聊頭像效果,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
一、自定義viewGroup,以此來實現主要的代碼邏輯
public class NineGridImageView<T> extends ViewGroup{ private int mRowCount; //行數 private int mColumnCount; //列數 private int mMaxSize = 9; //最大圖片數 private int mGap; //宮格間距 private int parentWidth;//父組件寬 private int parentHeight;//父組件高 private List<ImageView> mImageViewList = new ArrayList<>(); private List<T> mImgDataList; private NineGridImageViewAdapter<T> mAdapter; public NineGridImageView(Context context) { this(context,null); } public NineGridImageView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public NineGridImageView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.NineGridImageView); this.mGap = (int) typedArray.getDimension(R.styleable.NineGridImageView_imgGap, 8); typedArray.recycle(); } /** * 設定寬高 */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); parentWidth = measureWidth(widthMeasureSpec); parentHeight = measureHeight(heightMeasureSpec); setMeasuredDimension(parentWidth,parentHeight); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { layoutChildrenView(); } /** * 為子ImageView布局 */ private void layoutChildrenView(){ if(mImgDataList == null){ return; } int childrenCount = mImgDataList.size(); for(int i = 0; i < childrenCount; i++){ ImageView childrenView = (ImageView)getChildAt(i); if(mAdapter != null){ mAdapter.onDisplayImage(getContext(), childrenView, mImgDataList.get(i)); } int rowNum = i / mColumnCount;//當前行數 int columnNum = i % mColumnCount;//當前列數 int mImageSize = (parentWidth-(mColumnCount+1)*mGap)/mColumnCount;//圖片尺寸 int t_center = (parentHeight + mGap)/2;//中間位置以下的頂點(有宮格間距) int b_center = (parentHeight - mGap)/2;//中間位置以上的底部(有宮格間距) int l_center = (parentWidth + mGap)/2;//中間位置以右的左部(有宮格間距) int r_center = (parentWidth - mGap)/2;//中間位置以左的右部(有宮格間距) int center = (parentHeight - mImageSize)/2;//中間位置以上頂部(無宮格間距) int left = mImageSize * columnNum + mGap * (columnNum + 1); int top = mImageSize * rowNum + mGap * (rowNum + 1); int right = left + mImageSize; int bottom = top + mImageSize; /** * 不同子view情況下的不同顯示 */ if(childrenCount == 1){ childrenView.layout(left, top, right, bottom); }else if(childrenCount == 2){ childrenView.layout(left, center, right, center + mImageSize); }else if(childrenCount == 3){ if(i == 0){ childrenView.layout(center, top, center+mImageSize, bottom); }else { childrenView.layout(mGap * i +mImageSize * (i - 1), t_center, mGap * i +mImageSize * i, t_center+mImageSize); } }else if(childrenCount == 4){ childrenView.layout(left, top, right, bottom); }else if(childrenCount == 5){ if(i == 0){ childrenView.layout(r_center - mImageSize, r_center - mImageSize, r_center, r_center); }else if(i == 1){ childrenView.layout(l_center , r_center - mImageSize, l_center + mImageSize, r_center); }else{ childrenView.layout(mGap * (i - 1) + mImageSize * (i - 2),t_center,mGap * (i - 1) + mImageSize * (i - 1),t_center+mImageSize); } }else if(childrenCount == 6){ if(i < 3) { childrenView.layout(mGap * (i + 1) +mImageSize * i, b_center - mImageSize, mGap * (i + 1) + mImageSize * (i+1), b_center); }else{ childrenView.layout(mGap * (i - 2) + mImageSize * (i - 3),t_center,mGap * (i - 2) + mImageSize * (i - 2),t_center+mImageSize); } }else if(childrenCount == 7){ if(i == 0){ childrenView.layout(center,mGap,center+mImageSize,mGap+mImageSize); }else if(i > 0 && i < 4){ childrenView.layout(mGap * i +mImageSize * (i - 1),center,mGap * i +mImageSize * i,center+mImageSize); }else{ childrenView.layout(mGap * (i - 3) + mImageSize * (i - 4),t_center+mImageSize/2,mGap * (i - 3) + mImageSize * (i - 3),t_center+mImageSize/2+mImageSize); } }else if(childrenCount == 8){ if(i == 0){ childrenView.layout(r_center - mImageSize,mGap,r_center,mGap+mImageSize); }else if(i == 1){ childrenView.layout(l_center,mGap,l_center+mImageSize,mGap+mImageSize); }else if(i > 1 && i < 5){ childrenView.layout(mGap * (i - 1) +mImageSize * (i - 2), center, mGap * (i - 1) +mImageSize * (i - 1), center+mImageSize); }else{ childrenView.layout(mGap * (i - 4) + mImageSize * (i - 5), t_center+mImageSize/2, mGap * (i - 4) + mImageSize * (i - 4), t_center+mImageSize/2+mImageSize); } }else if(childrenCount == 9){ childrenView.layout(left, top, right, bottom); } } } /** * 設置圖片數據 * * @param lists 圖片數據集合 */ public void setImagesData(List lists){ if(lists == null || lists.isEmpty()){ this.setVisibility(GONE); return; }else { this.setVisibility(VISIBLE); } if(mMaxSize > 0 && lists.size() > mMaxSize){ lists = lists.subList(0, mMaxSize); } int[] gridParam = calculateGridParam(lists.size()); mRowCount = gridParam[0]; mColumnCount = gridParam[1]; if(mImgDataList == null){ int i = 0; while (i < lists.size()){ ImageView iv = getImageView(i); if(iv == null){ return; } addView(iv,generateDefaultLayoutParams()); i++; } }else { int oldViewCount = mImgDataList.size(); int newViewCount = lists.size(); if(oldViewCount > newViewCount){ removeViews(newViewCount, oldViewCount - newViewCount); }else if(oldViewCount < newViewCount){ for(int i = oldViewCount; i < newViewCount; i++){ ImageView iv = getImageView(i); if(iv == null){ return; } addView(iv, generateDefaultLayoutParams()); } } } mImgDataList = lists; requestLayout(); } /** * 獲得 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); return imageView; }else{ Log.e("NineGirdImageView", "Your must set a NineGridImageViewAdapter for NineGirdImageView"); return null; } } } /** * 設置宮格參數 * * @param imagesSize 圖片數量 * @return 宮格參數 gridParam[0] 宮格行數 gridParam[1] 宮格列數 */ protected static int[] calculateGridParam(int imagesSize){ int[] gridParam = new int[2]; if(imagesSize < 3){ gridParam[0] = 1; gridParam[1] = imagesSize; }else if(imagesSize <= 4){ gridParam[0] = 2; gridParam[1] = 2; }else{ 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; } /** * 對宮格的寬高進行重新定義 */ private int measureWidth(int measureSpec){ int result = 0; int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(measureSpec); if(specMode == MeasureSpec.EXACTLY){ result = specSize; }else{ result = 200; if(specMode == MeasureSpec.AT_MOST){ result = Math.min(result,specSize); } } return result; } private int measureHeight(int measureSpec){ int result = 0; int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(measureSpec); if(specMode == MeasureSpec.EXACTLY){ result = specSize; }else{ result = 200; if(specMode == MeasureSpec.AT_MOST){ result = Math.min(result,specSize); } } return result; } }
二、你要顯示你的網絡圖片所需要的代碼
public abstract class NineGridImageViewAdapter<T> { protected abstract void onDisplayImage(Context context, ImageView imageView, T t); protected ImageView generateImageView(Context context){ ImageView imageView = new ImageView(context); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); return imageView; } //這里可以添加你所需要的事件之類的方法 }
對了,別忘了配置間隔屬性,記得添加attrs.xml文件,加上如下代碼
<declare-styleable name="NineGridImageView"> <attr format="dimension" name="imgGap"/> </declare-styleable>
三、用法
NineGridImageViewAdapter<String> mAdapter = new NineGridImageViewAdapter<String>() { @Override protected void onDisplayImage(Context context, ImageView imageView, String s) { Picasso.with(context).load(s).placeholder(R.mipmap.ic_holding).error(R.mipmap.ic_error).into(imageView); } @Override protected ImageView generateImageView(Context context) { return super.generateImageView(context); } }; groudIcon1.setAdapter(mAdapter); groudIcon1.setImagesData(mPostList1);
上述就是小編為大家分享的怎么在Android中實現微信群聊頭像效果了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。