需求:视频通话界面,两个surfaceView一个显示本端的视图,另一个显示对端的视图,由于显示比例的问题总会存在一个覆盖另一个的问题,为保证用户体验,规定小的覆盖大的视图上面,且点击小的视图可切花为大图视图居中,达到两个视图切花的功能。简单写一个demo完成功能的测试需求,为了较少文章的篇幅,视图的内容用回执矩形代替(实际开发中显示的是本地照相采集的数据和对端经过opgl处理的数据)
简单的布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="@+id/remote_rl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<SurfaceView
android:id="@+id/remote_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
</RelativeLayout> android:layout_gravity="center" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/local_rl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<SurfaceView
android:id="@+id/local_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
具体的demo实现
public class MainActivity extends Activity implements View.OnClickListener {
public static final String TAG = "sssss";
//远端的视图
private SurfaceView remote_sv;
// 本地的视图
private SurfaceView local_sv;
private SurfaceHolder remote_holder;
private SurfaceHolder local_holder;
private RelativeLayout remote_rl;
private RelativeLayout local_rl;
private int screenWidth;
private int screenHeight;
private int beforRemoteweith;
private int beforLocalweith;
private int beforRemoteheigth;
private int beforLocalheigth;
private int StateAB = 0;
private int StateBA = 1;
private int mSate;
private int defaultLocalHeight=200;
private int defaultLocalwidth=400;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DisplayMetrics dm = getResources().getDisplayMetrics();
screenWidth = dm.widthPixels;
screenHeight = dm.heightPixels - 500;
remote_sv = (SurfaceView) findViewById(R.id.remote_view);
remote_rl = (RelativeLayout) findViewById(R.id.remote_rl);
local_rl = (RelativeLayout) findViewById(R.id.local_rl);
remote_sv.setOnClickListener(this);
LayoutParams params = new LayoutParams(screenWidth, screenHeight);
remote_sv.setLayoutParams(params);
remote_holder = remote_sv.getHolder();
// 对 surfaceView 进行操作
remote_holder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
Canvas c = remote_holder.lockCanvas();
// 2.开画
Paint p = new Paint();
p.setColor(Color.RED);
Rect aa = new Rect(0, 0, holder.getSurfaceFrame().width(),
holder.getSurfaceFrame().height());
c.drawRect(aa, p);
// 3. 解锁画布 更新提交屏幕显示内容
remote_holder.unlockCanvasAndPost(c);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
/**
*
Log.d(TAG,"remote_holder surfaceChanged width"+ width+"height"+height);
Canvas c = remote_holder.lockCanvas();
// 2.开画
Paint p = new Paint();
p.setColor(Color.RED);
Rect aa = new Rect(0, 0, holder.getSurfaceFrame().width(),
holder.getSurfaceFrame().height());
c.drawRect(aa, p);
// 3. 解锁画布 更新提交屏幕显示内容
remote_holder.unlockCanvasAndPost(c);
*
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!