安卓videoview视频缩放

时间:2022-06-13 12:21:58

第一步重写videoview,新建MyVideoView,至于为啥,网上有很多解释,大概是源代码中的给注销掉了,需要重写把缩放功能打开

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

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = getDefaultSize(0, widthMeasureSpec);
        int height = getDefaultSize(0, heightMeasureSpec);
        setMeasuredDimension(width, height);
    }
}

第二步,编写xml,把MyVideoView放到LinearLayout或者Relativlayout中,以便代码中动态修改myvideoview的大小

<com.sammer.xiangaotie.util.MyVideoView  android:id="@+id/my_video_view"  android:layout_width="500px"  android:layout_height="500px"  android:layout_gravity="center"/>
第三步,MainActivity中引用,并进行缩放

1. FrameLayout下动态设置子控件居中,动态用JAVA代码要这样实现:(位置,要在其他的语句中进行设置)
FrameLayout.LayoutParams lytp = new FrameLayout.LayoutParams(80,LayoutParams.WRAP_CONTENT);
lytp.gravity = Gravity.CENTER;
btn.setLayoutParams(lytp);
2. RelativeLayout下动态设置子控件居中:
RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE); 
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE); 
btn1.setLayoutParams(lp);

3.LinearLayout下动态设置子空间大小,居中。

LinearLayout.LayoutParams rl = new LinearLayout.LayoutParams(300,300);
rl.gravity = Gravity.CENTER_HORIZONTAL;
videoView.setLayoutParams(rl);

今天遇到的其他问题

1模拟器中spinner无法正常显示。

答案:把Activity修改成AppCompatActivity成功

2AppCompatActivity下requestWindowFeature(Window.FEATURE_NO_TITLE);无效解决办法

答案:修改AndroidManifest中的application下的android:theme选项

<application  android:allowBackup="true"  android:icon="@mipmap/xiangaotielogo"  android:label="@string/app_name"  android:roundIcon="@mipmap/ic_launcher_round"  android:supportsRtl="true"  android:theme="@style/Theme.AppCompat.Light.NoActionBar">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>