Android仿微信多人音視頻通話界面
工作中需要實(shí)現(xiàn)一個(gè)類似微信多人視頻通話功能的界面,分別使用自定義viewgroup和自定義layoutManager的方式進(jìn)行了實(shí)現(xiàn)。最終工作中采用了layoutManager,因?yàn)榭梢允褂胮ayload更新單個(gè)布局控件,效率更好。下面放出兩種具體的實(shí)現(xiàn)效果代碼。
1、使用自定義ViewGroup方式實(shí)現(xiàn)
下面是三個(gè)人通話時(shí)候的效果,其他的可以參考微信多人音視頻通話界面。

package com.dnaer.android.telephone.widgets;
import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import com.anbetter.log.MLog;
public class MultiVideoChatLayout extends ViewGroup implements CommLayoutAdapter.OnDataChangedListener {
private CommLayoutAdapter mCommLayoutAdapter;
private int mScreenWidth;
//人數(shù)為2,3,4狀態(tài)下的寬高度
private int mSizeModel1;
//人數(shù)為5,6,7,8,9狀態(tài)下的寬高度
private int mSizeModel2;
public MultiVideoChatLayout(Context context) {
this(context, null);
}
public MultiVideoChatLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MultiVideoChatLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initialize(context);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public MultiVideoChatLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
initialize(context);
}
private void initialize(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics metrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(metrics);
mScreenWidth = metrics.widthPixels;
mSizeModel1 = mScreenWidth / 2;
mSizeModel2 = mScreenWidth / 3;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//寬度默認(rèn)給屏幕的寬度,高度直接取寬度,形成一個(gè)正方形
final int width = MeasureSpec.makeMeasureSpec(mScreenWidth, MeasureSpec.EXACTLY);
final int height = MeasureSpec.makeMeasureSpec(mScreenWidth, MeasureSpec.EXACTLY);
setMeasuredDimension(width, height);
MLog.d("width: " + width + ", height:" + height);
final int childWidth = MeasureSpec.makeMeasureSpec(mScreenWidth / 3, MeasureSpec.EXACTLY);
final int childHeight = MeasureSpec.makeMeasureSpec(mScreenWidth / 3, MeasureSpec.EXACTLY);
final int childWidth2 = MeasureSpec.makeMeasureSpec(mScreenWidth / 2, MeasureSpec.EXACTLY);
final int childHeight2 = MeasureSpec.makeMeasureSpec(mScreenWidth / 2, MeasureSpec.EXACTLY);
if (getChildCount() > 4) {
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
child.measure(childWidth, childHeight);
}
} else {
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
child.measure(childWidth2, childHeight2);
}
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
if (getChildCount() <= 4) {
layoutModel1();
} else {
layoutModel2();
}
}
private void layoutModel2() {
int currentWidth = 0;
for (int i = 0; i < getChildCount(); i++) {
View item = getChildAt(i);
if (i % 3 == 0) {
currentWidth = 0;
item.layout(0, i / 3 * mSizeModel2, mSizeModel2, i / 3 * mSizeModel2 + mSizeModel2);
} else {
item.layout(currentWidth + mSizeModel2, i / 3 * mSizeModel2, currentWidth + 2 * mSizeModel2, i / 3 * mSizeModel2 + mSizeModel2);
currentWidth = currentWidth + mSizeModel2;
}
}
}
private void layoutModel1() {
if (getChildCount() == 3) {
for (int i = 0; i < getChildCount(); i++) {
View item = getChildAt(i);
MLog.d("width: " + item.getMeasuredWidth() + ", height: " + item.getMeasuredHeight() + ", mSizeModel1: " + mSizeModel1);
if (i == 0) {
item.layout(0, 0, mSizeModel1, mSizeModel1);
} else if (i == 1) {
item.layout(mSizeModel1, 0, mSizeModel1 * 2, mSizeModel1);
} else if (i == 2) {
item.layout(mSizeModel1 / 2, mSizeModel1, mSizeModel1 + mSizeModel1 / 2, mSizeModel1 * 2);
}
}
} else {
for (int i = 0; i < getChildCount(); i++) {
View item = getChildAt(i);
if (i % 2 == 0) {
item.layout(0, i / 2 * mSizeModel1, mSizeModel1, i / 2 * mSizeModel1 + mSizeModel1);
} else {
item.layout(mSizeModel1, i / 2 * mSizeModel1, 2 * mSizeModel1, i / 2 * mSizeModel1 + mSizeModel1);
}
}
}
}
public void setAdapter(CommLayoutAdapter adapter) {
mCommLayoutAdapter = adapter;
mCommLayoutAdapter.setOnDataChangedListener(this);
changedAdapter();
}
@Override
public void onChanged() {
changedAdapter();
}
private void changedAdapter() {
removeAllViews();
CommLayoutAdapter layoutAdapter = mCommLayoutAdapter;
for (int i = 0; i < layoutAdapter.getCount(); i++) {
View view = layoutAdapter.getView(this, i, layoutAdapter.getItem(i));
view.setDuplicateParentStateEnabled(true);
addView(view);
}
}
}
2、使用自定義LayoutManager方式實(shí)現(xiàn)
package org.fireking.customgridlayoutmanager
import android.content.res.Resources
import android.support.v7.widget.RecyclerView
import java.lang.IllegalArgumentException
class MultiChatLayoutManager : RecyclerView.LayoutManager() {
private var leftMargin = 0
private var rightMargin = 0
private var mScreenWidth = 0
override fun generateDefaultLayoutParams(): RecyclerView.LayoutParams {
return RecyclerView.LayoutParams(RecyclerView.LayoutParams.WRAP_CONTENT, RecyclerView.LayoutParams.WRAP_CONTENT)
}
override fun onLayoutChildren(recycler: RecyclerView.Recycler?, state: RecyclerView.State?) {
super.onLayoutChildren(recycler, state)
if (itemCount == 0) {
detachAndScrapAttachedViews(recycler!!)
return
}
if (childCount == 0 && state!!.isPreLayout) {
return
}
val params = recycler!!.getViewForPosition(0).layoutParams as RecyclerView.LayoutParams
leftMargin = params.leftMargin
rightMargin = params.rightMargin
detachAndScrapAttachedViews(recycler)
layoutItem(recycler)
}
private fun layoutItem(recycler: RecyclerView.Recycler) {
if (itemCount > 9) {
throw IllegalArgumentException("${javaClass.simpleName}最多支持9個(gè)item布局, 請(qǐng)檢查你的item個(gè)數(shù)是否正確")
}
mScreenWidth = Resources.getSystem().displayMetrics.widthPixels
val itemSize = if (itemCount > 4) {
mScreenWidth / 3
} else {
mScreenWidth / 2
}
if (itemCount <= 4) {
if (itemCount == 3) {
for (i in 0 until itemCount) {
val view = recycler.getViewForPosition(i)
addView(view) // 因?yàn)閐etach過(guò)所以重新添加
measureChildWithMargins(view, 0, 0)
when (i) {
0 -> layoutDecoratedWithMargins(view, 0, 0, itemSize, itemSize)
1 -> layoutDecoratedWithMargins(view, itemSize, 0, itemSize * 2, itemSize)
else -> layoutDecoratedWithMargins(
view,
itemSize / 2,
itemSize,
itemSize + itemSize / 2,
itemSize * 2
)
}
}
} else {
for (i in 0 until itemCount) {
val view = recycler.getViewForPosition(i)
addView(view) // 因?yàn)閐etach過(guò)所以重新添加
measureChildWithMargins(view, 0, 0)
if (i % 2 == 0) {
layoutDecoratedWithMargins(view, 0, i / 2 * itemSize, itemSize, i / 2 * itemSize + itemSize)
} else {
layoutDecoratedWithMargins(
view,
itemSize,
i / 2 * itemSize,
2 * itemSize,
i / 2 * itemSize + itemSize
)
}
}
}
} else {
var currentWidth = 0
for (i in 0 until itemCount) {
val view = recycler.getViewForPosition(i)
addView(view) // 因?yàn)閐etach過(guò)所以重新添加
measureChildWithMargins(view, 0, 0)
if (i % 3 == 0) {
currentWidth = 0
layoutDecoratedWithMargins(view, 0, i / 3 * itemSize, itemSize, i / 3 * itemSize + itemSize)
} else {
layoutDecoratedWithMargins(
view,
currentWidth + itemSize,
i / 3 * itemSize,
currentWidth + 2 * itemSize,
i / 3 * itemSize + itemSize
)
currentWidth += itemSize
}
}
}
}
//因?yàn)檫@個(gè)布局不需要有滾動(dòng),所以直接將橫豎兩個(gè)方向的滾動(dòng)全部取消了
override fun canScrollHorizontally(): Boolean {
return false
}
override fun canScrollVertically(): Boolean {
return false
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android普通應(yīng)用升級(jí)為系統(tǒng)應(yīng)用并獲取系統(tǒng)權(quán)限的操作
這篇文章主要介紹了Android普通應(yīng)用升級(jí)為系統(tǒng)應(yīng)用并獲取系統(tǒng)權(quán)限的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
Android 自定義view實(shí)現(xiàn)進(jìn)度條加載效果實(shí)例代碼
這篇文章主要介紹了Android 自定義view實(shí)現(xiàn)進(jìn)度條加載效果實(shí)例代碼,需要的朋友可以參考下2017-08-08
Android ListView 條目多樣式展示實(shí)例詳解
這篇文章主要介紹了Android ListView 條目多樣式展示的相關(guān)資料,需要的朋友可以參考下2017-04-04
Android RecyclerView滾動(dòng)定位
這篇文章主要為大家詳細(xì)介紹了Android RecyclerView滾動(dòng)定位的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
Android中實(shí)現(xiàn)下載URL地址的網(wǎng)絡(luò)資源的實(shí)例分享
這篇文章主要介紹了Android中實(shí)現(xiàn)下載URL地址的網(wǎng)絡(luò)資源的實(shí)例,其中還有一個(gè)進(jìn)行多線程下載的Java代碼示例,非常典型,需要的朋友可以參考下2016-04-04
Android中用StaticLayout實(shí)現(xiàn)文本繪制自動(dòng)換行詳解
StaticLayout是android中處理文字換行的一個(gè)工具類,StaticLayout已經(jīng)實(shí)現(xiàn)了文本繪制換行處理,下面這篇文章主要介紹了Android中用StaticLayout實(shí)現(xiàn)文本繪制自動(dòng)換行的相關(guān)資料,需要的朋友可以參考。2017-03-03
Android 創(chuàng)建/驗(yàn)證/刪除桌面快捷方式(已測(cè)試可用)
桌面快捷方式的出現(xiàn)方便了用戶操作,在某些程度上提高了用戶體驗(yàn),接下來(lái)將介紹下Android創(chuàng)建/驗(yàn)證/刪除桌面快捷方式的實(shí)現(xiàn)思路及代碼,感興趣的朋友可以了解下,或許本文可以幫助到你2013-02-02
如何利用Flutter實(shí)現(xiàn)酷狗流暢Tabbar效果
這篇文章主要給大家介紹了關(guān)于如何利用Flutter實(shí)現(xiàn)酷狗流暢Tabbar效果的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-02-02

