两个surfaceView实现切换效果

概述两个surfaceView实现切换效果 需求:视频通话界面,两个surfaceView一个显示本端的视图,另一个显示对端的视图,由于显示比例的问题总会存在一个覆盖另一个的问题,为保证用户体验,规定小的覆盖大的视图上面,且点击小的视图可切花为大图视图居中,达到两个视图切花的功能.简单写一个demo完成功能的测试需求,为了较少文章的篇幅,视图的内容用回执矩形代替(实际开发中显示的是本地照相采集的数据和对端经过opgl处理的数据) 简单的布局 <?xml version="1.0" encoding="u ...

需求:视频通话界面,两个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,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,p);  // 3. 解锁画布 更新提交屏幕显示内容  remote_holder.unlockCanvasAndPost(c);  */}  @OverrIDe  public voID surfaceDestroyed(SurfaceHolder holder) {  } });// 自动运行surfaceCreated以及surfaceChanged local_sv = (SurfaceVIEw) findVIEwByID(R.ID.local_vIEw); local_sv.setonClickListener(this); local_sv.setonClickListener(this); // sv.setZOrderOntop(false); local_sv.setZOrderOntop(true); // 这两个方法差不多,设置了就会浮现到顶部,但是,后面的看不见,要像下面设置为透明 // local_sv.setZOrderOntop(true); // local_sv.setZOrderMediaOverlay(true); local_holder = local_sv.getHolder(); remote_holder.setFormat(PixelFormat.transparent); local_holder.setFormat(PixelFormat.transparent); LayoutParams params1 = new LayoutParams(defaultLocalHeight,defaultLocalwIDth); local_sv.setLayoutParams(params1); remote_holder = remote_sv.getHolder(); local_holder.addCallback(new SurfaceHolder.Callback() {  @OverrIDe  public voID surfaceCreated(SurfaceHolder holder) {  Canvas c = holder.lockCanvas();  // 2.开画  Paint p = new Paint();  p.setcolor(color.YELLOW);  Rect aa = new Rect(0,p);  // 3. 解锁画布 更新提交屏幕显示内容  holder.unlockCanvasAndPost(c);  }  @OverrIDe  public voID surfaceChanged(SurfaceHolder holder,int height) { /**  *  Log.d(TAG,"local_holder surfaceChanged wIDth"+ wIDth+"height"+height);  Canvas c = holder.lockCanvas();  // 2.开画  Paint p = new Paint();  p.setcolor(color.YELLOW);  Rect aa = new Rect(0,holder.getSurfaceFrame().wIDth()-50,holder.getSurfaceFrame().height()-50);  c.drawRect(aa,p);  // 3. 解锁画布 更新提交屏幕显示内容  holder.unlockCanvasAndPost(c);  */}  @OverrIDe  public voID surfaceDestroyed(SurfaceHolder holder) {  } }); zoomOpera(local_rl,local_sv,remote_sv,remote_rl,defaultLocalwIDth,defaultLocalHeight,relativeLayout.CENTER_IN_PARENT); } @OverrIDe public voID onClick(VIEw vIEw) { switch (vIEw.getID()) { case R.ID.local_vIEw:  Log.d(TAG," onClick local_vIEw" + mSate);  if (mSate == StateAB) {  zoomlocalVIEwout(beforRemoteweith,beforRemoteheigth,remote_sv);  zoomremoteVIEwint(beforLocalweith,beforLocalheigth);  mSate = StateBA;  }  break; case R.ID.remote_vIEw:  Log.d(TAG," onClick emote_vIEw" + mSate);  if (mSate == StateBA) {  zoomremoteout(beforRemoteweith,remote_sv);  zoomlocalVIEwint(beforLocalweith,beforLocalheigth);  mSate = StateAB;  }  break; default:  break; } }//放大远端的视图 private voID zoomremoteout(int weith2,int heigth2,SurfaceVIEw localVIEw,SurfaceVIEw remoteVIEw) { beforLocalheigth = localVIEw.getMeasuredHeight(); beforLocalweith = localVIEw.getMeasureDWIDth(); beforRemoteheigth = remoteVIEw.getMeasuredHeight(); beforRemoteweith = remoteVIEw.getMeasureDWIDth(); Log.d(TAG,"zoomremoteout beforLocalheigth" + beforLocalheigth  + "beforLocalweith" + beforLocalweith + "beforRemoteheigth"  + beforRemoteheigth + "beforRemoteweith" + beforLocalweith); zoomOpera(local_rl,screenWIDth,beforLocalheigth,relativeLayout.CENTER_IN_PARENT); }//具体的视图 *** 作 private voID zoomOpera(VIEw sourcVIEw,SurfaceVIEw beforevIEw,SurfaceVIEw aftervIEw,VIEw detVIEw,int beforLocalweith,int beforLocalHeigth,int rule) { LayoutParams params1 = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT); Log.w(TAG,"beforLocalheigth = " + beforLocalheigth  + "; beforLocalweith = " + beforLocalweith); params1.addRule(rule,relativeLayout.TRUE); aftervIEw.setLayoutParams(params1); aftervIEw.setBackgroundResource(androID.R.color.transparent); params1 = new LayoutParams(beforLocalweith,beforLocalHeigth); params1.addRule(rule,relativeLayout.TRUE); detVIEw.setLayoutParams(params1); }//缩小远端的视图 private voID zoomremoteVIEwint(int weith2,int heigth2) { relativeLayout paretvIEw = (relativeLayout) local_rl.getParent(); paretvIEw.removeVIEw(remote_rl); paretvIEw.removeVIEw(local_rl); zoomOpera(local_rl,beforLocalweith,relativeLayout.AliGN_PARENT_top); Log.d(TAG,"paretvIEw" + paretvIEw.getChildCount()); paretvIEw.addVIEw(local_rl); paretvIEw.addVIEw(remote_rl); remote_sv.setZOrderOntop(true); }//放大本端的视图 private voID zoomlocalVIEwout(int weith2,SurfaceVIEw remoteVIEw) { beforLocalheigth = localVIEw.getMeasuredHeight(); beforLocalweith = localVIEw.getMeasureDWIDth(); beforRemoteheigth = remoteVIEw.getMeasuredHeight(); beforRemoteweith = remoteVIEw.getMeasureDWIDth(); Log.d(TAG,"zoomlocalVIEwout beforLocalheigth" + beforLocalheigth  + "beforLocalweith" + beforLocalweith + "beforRemoteheigth"  + beforRemoteheigth + "beforRemoteweith" + beforRemoteweith); zoomOpera(remote_rl,local_rl,beforRemoteweith,relativeLayout.CENTER_IN_PARENT); }//减小本端的视图 private voID zoomlocalVIEwint(int weith2,int heigth2) { relativeLayout paretvIEw = (relativeLayout) local_rl.getParent(); paretvIEw.removeVIEw(remote_rl); paretvIEw.removeVIEw(local_rl); zoomOpera(remote_rl,relativeLayout.AliGN_PARENT_top); paretvIEw.addVIEw(remote_rl); paretvIEw.addVIEw(local_rl); local_sv.setZOrderOntop(true); }}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

总结

以上是内存溢出为你收集整理的两个surfaceView实现切换效果全部内容,希望文章能够帮你解决两个surfaceView实现切换效果所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1144319.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存