0

VideoView親コンテナの幅に合わせてサイズを設定してから、4:3のアスペクト比を維持するように高さを設定しようとしています。VideoViewクラスを拡張してオーバーライドすることを推奨するいくつかの回答を見てきましたonMeasureが、取得しているパラメーターやその使用方法がわかりません。

package com.example;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.VideoView;

public class MyVideoView extends VideoView {

    public MyVideoView(Context context) {
        super(context);
    }

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

    public MyVideoView (Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {
        Log.i("MyVideoView", "width="+widthMeasureSpec);
        Log.i("MyVideoView", "height="+heightMeasureSpec);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

結果(Nexus 7タブレットの場合):

02-13 21:33:42.515: I/MyVideoView(12667): width=1073742463
02-13 21:33:42.515: I/MyVideoView(12667): height=1073742303

私は次のレイアウトを実現しようとしています:

タブレット(ポートレート):

  • VideoViewの幅-全画面またはほぼ全画面。
  • VideoViewの高さ-幅を指定して4:3のアスペクト比を維持します
  • ListView-VideoViewの下に表示され、再生するビデオを選択します。

タブレット(風景):

  • ListView-画面の左側に表示され、再生するビデオを選択するために使用されます。
  • VideoView-画面の右側に表示され、4:3のアスペクト比を維持するために、残りの幅と設定された高さで埋める必要があります。
4

1 に答える 1

1

これを試して:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int width = getDefaultSize(mVideoWidth, widthMeasureSpec);
    int height = getDefaultSize(mVideoHeight, heightMeasureSpec);

            /**Adjust according to your desired ratio*/
    if (mVideoWidth > 0 && mVideoHeight > 0) {
        if (mVideoWidth * height > width * mVideoHeight) {
            // Log.i("@@@", "image too tall, correcting");
            height = (width * mVideoHeight / mVideoWidth);
        } else if (mVideoWidth * height < width * mVideoHeight) {
            // Log.i("@@@", "image too wide, correcting");
            width = (height * mVideoWidth / mVideoHeight);
        } else {
            // Log.i("@@@", "aspect ratio is correct: " +
            // width+"/"+height+"="+
            // mVideoWidth+"/"+mVideoHeight);
        }
    }

    setMeasuredDimension(width, height);

}

ここで、mVideoWidthとmVideoHeightは、ビデオの現在のサイズです。お役に立てば幸いです。:)

于 2013-02-14T08:06:38.167 に答える