Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android VideoView 視頻旋轉的嘗試與解法

編輯:關於 Android 程式設計

在 Android 中要讓 VideoView 裡的影片旋轉,一直是個相當棘手的需求。本文記錄了嘗試讓「某個小區域中的 VideoView 影片旋轉 90 度」時走過的各種方法與最終可行的方案。

嘗試一:直接旋轉 View 屬性

直接修改 View 的 orientation 屬性——但 FrameLayout 並沒有此屬性,LinearLayout 雖有,實際效果卻不理想:版面中所有元素都轉了,唯獨影片沒有跟著旋轉 90 度。

嘗試二:使用 animation 動畫

改用補間動畫旋轉 VideoView,結果是「容器」轉了,但其中的影片畫面依然紋絲不動。

嘗試三:旋轉整個螢幕

在 AndroidManifest.xml 的 activity 屬性中加上:

android:configChanges="orientation"
android:screenOrientation="sensor"

並在程式碼中覆寫:

public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
}

注意:程式中不可出現 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 這類強制方向的寫法,否則此方法會失效。實測當手機由橫轉豎時,整個畫面(含影片)都會跟著旋轉,效果不錯。但缺點是它會連同其他控制項一起旋轉,無法只旋轉影片。

嘗試四:自訂 VideoView 並搭配 resize

若需求是「全螢幕播放影片」,只需重寫 VideoView 讓它在螢幕旋轉時自動調整尺寸,再搭配方案三即可:

public class VideoViewCustom extends VideoView {

    private int mForceHeight = 0;
    private int mForceWidth = 0;

    public VideoViewCustom(Context context) { super(context); }
    public VideoViewCustom(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public VideoViewCustom(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void setDimensions(int w, int h) {
        this.mForceHeight = h;
        this.mForceWidth = w;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(mForceWidth, mForceHeight);
    }
}

搭配方向變化的回呼:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                             WindowManager.LayoutParams.FLAG_FULLSCREEN);
        questionVideo.setDimensions(displayHeight, displayWidth);
        questionVideo.getHolder().setFixedSize(displayHeight, displayWidth);
    } else {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,
                             WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        questionVideo.setDimensions(displayWidth, smallHeight);
        questionVideo.getHolder().setFixedSize(displayWidth, smallHeight);
    }
}

記得在 XML 中使用「套件名稱.自訂類別名稱」的形式,才能正確把標籤綁定到程式碼中的自訂 class。

嘗試五:android.media.effect

這個 API 大約在 API Level 16 才提供(Level 10 即 2.3.3 無法使用)。查閱官方文件後發現,它主要還是針對 image 生效,並未明確提及對 video 的支援。

嘗試六:自訂 RotateLayout 旋轉整個版面

若可以接受把某個 Layout 整體旋轉,可撰寫一個自訂的 RotateLayout:

public class RotateLayout extends FrameLayout {
    private Matrix mForward = new Matrix();
    private Matrix mReverse = new Matrix();
    private float[] mTemp = new float[2];

    public RotateLayout(Context context) { super(context); }
    public RotateLayout(Context context, AttributeSet attrs) { super(context, attrs); }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        canvas.rotate(270, getWidth() / 2, getHeight() / 2);
        mForward = canvas.getMatrix();
        mForward.invert(mReverse);
        canvas.save();
        canvas.setMatrix(mForward); // 用於正確定位觸摸事件
        super.dispatchDraw(canvas);
        canvas.restore();
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        final float[] temp = mTemp;
        temp[0] = event.getX();
        temp[1] = event.getY();
        mReverse.mapPoints(temp);       // 座標反向映射
        event.setLocation(temp[0], temp[1]);
        return super.dispatchTouchEvent(event);
    }
}

這個做法在 dispatchDraw 中旋轉 Canvas,並透過矩陣的正反轉換正確處理觸摸事件座標,讓旋轉後的版面仍能正常互動。

總結

若需求是「只旋轉影片、其他控制項不動,且手機不旋轉」,坦白說在舊版 Android 上相當難以完美實現;較務實的折衷是旋轉整個螢幕(方案三),或改用自訂 RotateLayout 搭配觸摸座標映射(方案六)。選擇何種方案,取決於你能接受多少副作用。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved