您好,登錄后才能下訂單哦!
本篇內容主要講解“怎么用surfaceView實現切換效果”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“怎么用surfaceView實現切換效果”吧!
需求:視頻通話界面,兩個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); */} @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, 0, holder.getSurfaceFrame().width(), holder.getSurfaceFrame().height()); c.drawRect(aa, p); // 3. 解鎖畫布 更新提交屏幕顯示內容 holder.unlockCanvasAndPost(c); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, 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, 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, local_sv, 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, beforRemoteheigth, local_sv, remote_sv); zoomlocalViewint(beforLocalweith, beforLocalheigth); mSate = StateAB; } break; default: break; } }//放大遠端的視圖 private void zoomRemoteout(int weith3, int heigth3, 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, local_sv, remote_sv, remote_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 weith3, int heigth3) { RelativeLayout paretview = (RelativeLayout) local_rl.getParent(); paretview.removeView(remote_rl); paretview.removeView(local_rl); zoomOpera(local_rl, local_sv, remote_sv, remote_rl, beforLocalweith, beforLocalheigth, 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 weith3, int heigth3, SurfaceView localView, 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, remote_sv, local_sv, local_rl, beforRemoteweith, beforRemoteheigth, RelativeLayout.CENTER_IN_PARENT); }//減小本端的視圖 private void zoomlocalViewint(int weith3, int heigth3) { RelativeLayout paretview = (RelativeLayout) local_rl.getParent(); paretview.removeView(remote_rl); paretview.removeView(local_rl); zoomOpera(remote_rl, remote_sv, local_sv, local_rl, beforRemoteweith, beforRemoteheigth, RelativeLayout.ALIGN_PARENT_TOP); paretview.addView(remote_rl); paretview.addView(local_rl); local_sv.setZOrderOnTop(true); }}
到此,相信大家對“怎么用surfaceView實現切換效果”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。