您好,登錄后才能下訂單哦!
這篇文章主要介紹了Android怎么實現IM多人員組合的群組頭像,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
說明:
此頭像類似微信群組頭像,整個頭像由組內前N位人員的頭像組合而成,可用網絡或本地圖片進行組合,最終顯示為一個頭像整體,看效果圖:
一、自定義整體頭像的ViewGroup,計算并保存寬高(重寫onMeasure):
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { mWidth = getWidth(widthMeasureSpec); mHeight = getHeight(heightMeasureSpec); setMeasuredDimension(mWidth, mHeight); } private int getWidth(int measureSpec) { int width = MIN_WIDTH_AND_HEIGHT; int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(measureSpec); if (specMode == MeasureSpec.EXACTLY) { width = specSize; } else if (specMode == MeasureSpec.AT_MOST) { width = Math.min(MIN_WIDTH_AND_HEIGHT, specSize); } return width; } private int getHeight(int measureSpec) { int height = MIN_WIDTH_AND_HEIGHT; int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(measureSpec); if (specMode == MeasureSpec.EXACTLY) { height = specSize; } else if (specMode == MeasureSpec.AT_MOST) { height = Math.min(MIN_WIDTH_AND_HEIGHT, specSize); } return height; }
二、布局子頭像的View(重寫onLayout,對每個子頭像進行布局):
@Override protected void onLayout(boolean changed, int l, int t, int r, int b) { layoutChild(); } private void layoutChild() { if (mImgUrls == null || mImgUrls.isEmpty()) { return; } for (int i = 0; i < mImgSize; i++) { ImageView itemV = (ImageView) getChildAt(i); int left = 0, top = 0, right = 0, bottom = 0; /* 對每個item的View計算left、top、right、bottom四個值 */ ... itemV.layout(left, top, right, bottom); //真正布局子頭像位置 showImage(itemV, mImgUrls.get(i)); //加載并顯示子頭像圖片 } }
三、加載并顯示各子頭像(使用Glide加載并顯示每個子頭像)
private void showImage(Context context, ImageView iv, String url) { if (TextUtils.isEmpty(url)) { Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), R.mipmap.user_default_icon); iv.setImageBitmap(bmp); return; } Glide.with(context).load(url) .diskCacheStrategy(DiskCacheStrategy.ALL) .dontAnimate() .placeholder(R.mipmap.user_default_icon) .error(R.mipmap.user_default_icon) .into(iv); }
到此多圖片組合頭像已經完成,不過想要圈形的還需要進行以下步奏
四、裁剪整個群頭像為圓形(重寫dispatchDraw):
@Override protected void dispatchDraw(Canvas canvas) { Path path = new Path(); path.addCircle(mWidth / 2, mHeight / 2, mWidth / 2, Path.Direction.CW); canvas.clipPath(path); canvas.drawColor(Color.TRANSPARENT); super.dispatchDraw(canvas); drawGroupView(canvas); } /** * 繪制各頭像間隔線 * @param canvas */ private void drawGroupView(Canvas canvas) { /* 計算各條線的x和y坐標值 */ float[] point1 = new float[2], point2 = new float[2]; ... drawLine(canvas, point1, point2); } /** * 繪制直線 */ private void drawLine(Canvas canvas, float[] point1, float[] point2) { mPaint.reset(); mPaint.setAntiAlias(true); mPaint.setStrokeWidth(mInterval); mPaint.setColor(Color.WHITE); mPaint.setStyle(Paint.Style.STROKE); canvas.drawLine(point1[0], point1[1], point2[0], point2[1], mPaint); }
五、暴露公共方法供外部調用:
/** * 設置圖片url集合 * * @param imgs 圖片url集合 */ public void setImages(List<String> imgs) { if (imgs == null || imgs.isEmpty()) { return; } if (imgs.size() > MAX_SIZE) { imgs = imgs.subList(0, MAX_SIZE); } removeAllViews(); mImgUrls = imgs; mImgSize = imgs.size(); for (int i = 0; i < mImgSize; i++) { View v = getItemView(i); if (v == null) { return; } addView(v, generateDefaultLayoutParams()); } requestLayout(); } /** * 設置單個圖片url * * @param img 圖片url */ public void setImageUrl(String img) { ArrayList imgUrls = new ArrayList<>(); imgUrls.add(img); setImages(imgUrls); } /** * 生成一個頭像布局 */ private ImageView getItemView(int position) { ... }
六、使用:
1.寫一個布局文件放自定義群組頭像控件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#f2f2f2" android:orientation="vertical"> <com.yyh.im.ui.widget.HeadView android:id="@+id/cv_head" android:layout_width="150dp" android:layout_height="150dp"/> </LinearLayout>
2.代碼中群組頭像控件顯示圖片:
@BindView(R2.id.cv_head) public HeadView mHeadCv; private String[] IMG_URL_LIST = { "70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=416954025,2289731303&fm=27&gp=0.jpg", "70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=704997830,3098922597&fm=27&gp=0.jpg", "70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1375449509,557259337&fm=27&gp=0.jpg", "70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2825392570,1862752263&fm=27&gp=0.jpg", "70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3252489351,440833245&fm=27&gp=0.jpg", "70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3586311245,3082636880&fm=27&gp=0.jpg" }; private void showImage(){ ArrayList<String> list = new ArrayList<>(); for (int i = 0; i < 6; i++) { list.add(IMG_URL_LIST[i]); } mHeadCv.setImageUrls(list); }
感謝你能夠認真閱讀完這篇文章,希望小編分享的“Android怎么實現IM多人員組合的群組頭像”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。