2013-10-17 81 views
4

我有一個RelativeLayout的Android VideoView crop_center

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_gravity="center" 
    android:foregroundGravity="center" 
    android:gravity="center" 
    android:orientation="horizontal" > 

    <VideoView 
     android:id="@+id/videoViewPanel" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_gravity="center" 
     android:layout_centerInParent="true"/> 

</RelativeLayout> 

而我需要的是顯示視頻全屏裁剪。如果我可以比較ImageView,我需要將其顯示爲crop_center。

我該如何讓VideoView不會自動調整視頻大小以適合中心,但裁剪中心?

回答

2

解決方法是使用TextureView而不是VideoViewSurfaceView)。
TextureView不會對內容進行任何操作以適應屏幕。
這裏是解決方案的代碼示例:

//store the SurfaceTexture to set surface for MediaPlayer 
mTextureView.setSurfaceTextureListener(new SurfaceTextureListener() { 
@Override 
    public void onSurfaceTextureAvailable(SurfaceTexture surface, 
      int width, int height) { 
     FullScreenActivity.this.mSurface = surface; 

    } 

....

Surface s = new Surface(mSurface); 
mPlayer = mp; 
mp.setSurface(s); 

scaleVideo(mp);//<-- this function scales video to run cropped 

....

private void scaleVideo(MediaPlayer mPlayer) { 

     LayoutParams videoParams = (LayoutParams) mTextureView 
       .getLayoutParams(); 
     DisplayMetrics dm = new DisplayMetrics(); 
     FullScreenActivity.this.getWindowManager().getDefaultDisplay() 
       .getMetrics(dm); 

     final int height = dm.heightPixels; 
     final int width = dm.widthPixels; 
     int videoHeight = mPlayer.getVideoHeight(); 
     int videoWidth = mPlayer.getVideoWidth(); 
     double hRatio = 1; 

     hRatio = (height * 1.0/videoHeight)/(width * 1.0/videoWidth); 
     videoParams.x = (int) (hRatio <= 1 ? 0 : Math.round((-(hRatio - 1)/2) 
       * width)); 
     videoParams.y = (int) (hRatio >= 1 ? 0 : Math 
       .round((((-1/hRatio) + 1)/2) * height)); 
     videoParams.width = width - videoParams.x - videoParams.x; 
     videoParams.height = height - videoParams.y - videoParams.y; 
     Log.e(TAG, "x:" + videoParams.x + " y:" + videoParams.y); 
     mTextureView.setScaleX(1.00001f);//<-- this line enables smoothing of the picture in TextureView. 
     mTextureView.requestLayout(); 
     mTextureView.invalidate(); 

    } 
+2

下面是一個使用textureView並具有centerCrop添加庫: https://github.com/dmytrodanylyk/android-video-crop – Jordy

0

若要裁剪中心在全屏你仍然可以使用VideoView。設置VideoView的寬度和高度以匹配RelativeLayout中的父級,並將其調整爲大於屏幕並設置其位置。

<?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" 
android:id="@+id/rootLayout" 
tools:context="com.example.Activity"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:layout_height="match_parent"> 

     <VideoView 
      android:id="@+id/video_view" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_centerInParent="true" 
      android:layout_centerVertical="true" /> 

    </RelativeLayout> 
</RelativeLayout> 

然後在的onCreate:

RelativeLayout rootView=(RelativeLayout) findViewById(R.id.rootLayout); 
    Display display=getWindowManager().getDefaultDisplay(); 
    Point size=new Point(); 
    display.getSize(size); 

    FrameLayout.LayoutParams rootViewParams = (FrameLayout.LayoutParams) rootView.getLayoutParams(); 
    int videoWidth=864; 
    int videoHeight=1280; 

    if ((float)videoWidth/(float)videoHeight<(float)size.x/(float)size.y) { 
     rootViewParams.width=size.x; 
     rootViewParams.height=videoHeight*size.x/videoWidth; 
     rootView.setX(0); 
     rootView.setY((rootViewParams.height-size.y)/2*-1); 
    } else { 
     rootViewParams.width=videoWidth*size.y/videoHeight; 
     rootViewParams.height=size.y; 
     rootView.setX((rootViewParams.width-size.x)/2*-1); 
     rootView.setY(0); 
    } 
    rootView.setLayoutParams(rootViewParams); 


    final VideoView mVideoView=(VideoView)findViewById(R.id.video_view); 
    mVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.splash)); 
    mVideoView.requestFocus(); 

    mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { 
     @Override 
     public void onPrepared(MediaPlayer mediaPlayer) { 
      mVideoView.start(); 
     } 
    }); 
+0

我我嘗試過類似的解決方案,因爲它在4年前被問及一個問題時很明顯,當時VideoView在其內部強制安裝視頻,禁止裁剪。現在不知道它是如何工作的,因爲4年過去了,並且SDK中必須改變一些東西。 –

+0

好吧,我想到了,但我花了一些時間尋找解決方案,這個問題很清楚,並在谷歌的第一個位置出現,所以我決定回答。這適用於我在api級別25。 – MazarD

+0

你確定它應該** **,而不是**適合中心**嗎? –