版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
【移動(dòng)應(yīng)用開發(fā)技術(shù)】Android怎么實(shí)現(xiàn)IM多人員組合的群組頭像
這篇文章主要介紹了Android怎么實(shí)現(xiàn)IM多人員組合的群組頭像,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓在下帶著大家一起了解一下。說(shuō)明:此頭像類似微信群組頭像,整個(gè)頭像由組內(nèi)前N位人員的頭像組合而成,可用網(wǎng)絡(luò)或本地圖片進(jìn)行組合,最終顯示為一個(gè)頭像整體,看效果圖:一、自定義整體頭像的ViewGroup,計(jì)算并保存寬高(重寫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,對(duì)每個(gè)子頭像進(jìn)行布局):@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;
/*
對(duì)每個(gè)item的View計(jì)算left、top、right、bottom四個(gè)值
*/
...
itemV.layout(left,
top,
right,
bottom);
//真正布局子頭像位置
showImage(itemV,
mImgUrls.get(i));
//加載并顯示子頭像圖片
}
}三、加載并顯示各子頭像(使用Glide加載并顯示每個(gè)子頭像)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);
}到此多圖片組合頭像已經(jīng)完成,不過(guò)想要圈形的還需要進(jìn)行以下步奏四、裁剪整個(gè)群頭像為圓形(重寫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)
{
/*
計(jì)算各條線的x和y坐標(biāo)值
*/
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);
}五、暴露公共方法供外部調(diào)用:/**
*
設(shè)置圖片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();
}
/**
*
設(shè)置單個(gè)圖片url
*
*
@param
img
圖片url
*/
public
void
setImageUrl(String
img)
{
ArrayList
imgUrls
=
new
ArrayList<>();
imgUrls.add(img);
setImages(imgUrls);
}
/**
*
生成一個(gè)頭像布局
*/
private
ImageView
getItemView(int
position)
{
...
}六、使用:1.寫一個(gè)布局文件放自定義群組頭像控件:<?xml
version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="/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",
"70
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024年BOD自動(dòng)在線監(jiān)測(cè)儀項(xiàng)目合作計(jì)劃書
- 2024年乙苯脫氫催化劑項(xiàng)目發(fā)展計(jì)劃
- 2024年嘧菌酯合作協(xié)議書
- 2024年抗重癥肌無(wú)力藥項(xiàng)目發(fā)展計(jì)劃
- 2024年微滴灌過(guò)濾設(shè)備合作協(xié)議書
- 2024年重組葡激酶項(xiàng)目建議書
- 2024年辦公商業(yè)空間設(shè)計(jì)項(xiàng)目發(fā)展計(jì)劃
- 藥事管理與法規(guī)分類模擬題141
- 2024年福清市江鏡中心小學(xué)四年級(jí)數(shù)學(xué)第一學(xué)期期末學(xué)業(yè)水平測(cè)試模擬試題含解析
- 2024年福建省泉州市臺(tái)商投資區(qū)三年級(jí)數(shù)學(xué)第一學(xué)期期末達(dá)標(biāo)測(cè)試試題含解析
- 催化燃燒設(shè)計(jì)計(jì)算總表
- 工程勘察服務(wù)成本要素信息(2022版)
- 安慶市鑫富化工有限公司“3.27”爆炸事故案例分析
- 關(guān)于交通銀行的實(shí)習(xí)報(bào)告四篇
- 《網(wǎng)易公司戰(zhàn)略管理案例分析報(bào)告》
- 2023年通用技術(shù)集團(tuán)機(jī)床工程研究院有限公司招聘筆試題庫(kù)及答案解析
- GB/T 18263-2000人造板機(jī)械熱壓機(jī)術(shù)語(yǔ)
- GB/T 14809-2000高頻介質(zhì)加熱設(shè)備輸出功率的測(cè)定方法
- 【課件】《登高》課47張高一語(yǔ)文統(tǒng)編版必修上冊(cè)
- GB 39800.1-2020個(gè)體防護(hù)裝備配備規(guī)范第1部分:總則
- GB 12904-2008商品條碼零售商品編碼與條碼表示
評(píng)論
0/150
提交評(píng)論