【问题标题】:Video background视频背景
【发布时间】:2017-12-21 09:29:19
【问题描述】:

我正在尝试将视频作为背景放入我的应用程序中。但是,当我放视频时,它的大小,很奇怪,随着时间的推移而上升并且不是顶部。有谁知道我该如何解决这个问题?让它保持合法的比例,只是它像侧面一样被切割......类似的东西

我的代码:

Activity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout      xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/home_container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <icon.com.videobackground.VideoBackground
        android:id="@+id/introVideoView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/transparent">

        <EditText
            android:id="@+id/edit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:hint="Enter Email"
            android:padding="10dp"
            android:gravity="center"
            android:background="@null" />

        <EditText
            android:id="@+id/pass"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/edit"
            android:hint="Enter Password"
            android:padding="10dp"
            android:layout_marginTop="5dp"
            android:gravity="center"
            android:background="@null" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Login"
            android:textColor="@android:color/holo_orange_dark"
            android:padding="10dp"
            android:layout_marginTop="5dp"
            android:textSize="25sp"
            android:background="@android:color/transparent"
            android:layout_below="@+id/pass"/>

    </RelativeLayout>
</RelativeLayout>

VideoBackground.java

public class VideoBackground extends SurfaceView implements SurfaceHolder.Callback {

    private static final String TAG = "INTRO_VIDEO_CALLBACK";
    private MediaPlayer mp;

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public VideoBackground(Context context, AttributeSet attrs,
                           int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    public VideoBackground(Context context, AttributeSet attrs,
                           int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

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

    public VideoBackground(Context context) {
        super(context);
        init();
    }

    private void init() {
        mp = new MediaPlayer();
        getHolder().addCallback(this);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        AssetFileDescriptor afd = getResources().openRawResourceFd(R.raw.video_bgg); // your intro video file placed in raw folder named as intro.mp4
        try {
            mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(),
                    afd.getDeclaredLength());
            mp.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
        android.view.ViewGroup.LayoutParams lp = getLayoutParams();

        int screenHeight = getHeight();
        int screenWidth = getWidth();

        // this plays in full screen video
        lp.height = screenHeight;
        lp.width = screenWidth;

        setLayoutParams(lp);
        mp.setDisplay(getHolder());
        mp.setLooping(false);
        mp.start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
                               int height) {
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        mp.stop();
    }
}

Image this paste raw

【问题讨论】:

标签: android android-layout android-video-player


【解决方案1】:

制作单独的视频背景类并粘贴此代码

public class VideoBackground extends SurfaceView implements SurfaceHolder.Callback {

private static final String TAG = "INTRO_VIDEO_CALLBACK";
private MediaPlayer mp;

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public VideoBackground(Context context, AttributeSet attrs,
                       int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    init();
}

public VideoBackground(Context context, AttributeSet attrs,
                       int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
}

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

public VideoBackground(Context context) {
    super(context);
    init();
}

private void init() {
    mp = new MediaPlayer();
    getHolder().addCallback(this);
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    AssetFileDescriptor afd = getResources().openRawResourceFd(R.raw.sample); // your intro video file placed in raw folder named as intro.mp4
    try {
        mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(),
                afd.getDeclaredLength());
        mp.prepare();
    } catch (IOException e) {
        e.printStackTrace();
    }
    android.view.ViewGroup.LayoutParams lp = getLayoutParams();

    int screenHeight = getHeight();
    int screenWidth = getWidth();

    // this plays in full screen video
    lp.height = screenHeight;
    lp.width = screenWidth;

    setLayoutParams(lp);
    mp.setDisplay(getHolder());
    mp.setLooping(false);
    mp.start();
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
                           int height) {
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    mp.stop();
    }
   }

你的 XML 就像 activity_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout      xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/home_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<icon.com.videobackground.VideoBackground
    android:id="@+id/introVideoView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent">

    <EditText
        android:id="@+id/edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:hint="Enter Email"
        android:padding="10dp"
        android:gravity="center"
        android:background="@null" />

    <EditText
        android:id="@+id/pass"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/edit"
        android:hint="Enter Password"
        android:padding="10dp"
        android:layout_marginTop="5dp"
        android:gravity="center"
        android:background="@null" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Login"
        android:textColor="@android:color/holo_orange_dark"
        android:padding="10dp"
        android:layout_marginTop="5dp"
        android:textSize="25sp"
        android:background="@android:color/transparent"
        android:layout_below="@+id/pass"/>

</RelativeLayout>

您的活动:MainActivity

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

}

【讨论】:

  • 谢谢,我有问题,创建一个单独的类还是直接在我的 MainActivity 中使用它?还是调用这个类在我的 na 中创建在 main 中?
  • 等待我更新我的代码刚刚过去并使用这个类
  • 谢谢,我会测试并回报你,我在服务中,我一回家就测试!!非常感谢
  • 请务必花点时间实施。如果您遇到任何问题,请将您的查询放在这里...
  • 在 res 中创建一个目录并将其命名为 raw 并放置任何带有小写字母名称的视频文件。运行并执行。
猜你喜欢
  • 1970-01-01
  • 2015-01-10
  • 2021-12-13
  • 1970-01-01
  • 1970-01-01
  • 2015-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多