安卓VideoView启动画面

5
这是我的splash.java文件,我使用了videoView作为contentView。有没有办法让视频居中显示在作为contentView的videoView中呢?或者有更好的解决方法吗?
package com.android;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.widget.VideoView;

public class Splash extends Activity
{
    VideoView vidHolder;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        try
        {
            vidHolder = new VideoView(this);
            setContentView(vidHolder);
            Uri video = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.splash);
            vidHolder.setVideoURI(video);
            vidHolder.setOnCompletionListener(new OnCompletionListener()
            {
                public void onCompletion(MediaPlayer mp) {
                    jump();
            }});
            vidHolder.start();

        } catch(Exception ex) {
            jump();
        }
    }

    private void jump()
    {
        if(isFinishing())
            return;
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }
}
1个回答

6
使用布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
  <VideoView android:id="@+id/myvideo"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_centerInParent="true"/>
</RelativeLayout>

注意:虽然您没有要求,但是您的代码来自另一个示例,该示例将显示视频之前和之后的臭名昭著的黑色闪烁。您需要在#setVideoUri之后添加vidHolder.setZOrderOnTop(true);以避免这种情况。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接