视频背景

问题描述 投票:1回答:1

我正在尝试将视频作为背景添加到我的应用程序中。然而,当我放视频时,它的大小,非常奇怪,随着时间的推移而上升,并不是最重要的。有谁知道如何解决这个问题?把它留在一个合法的比例,只是它被切割成两侧......这类似的东西

我的代码:

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>

video background.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个回答
0
投票

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

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);
}

}

© www.soinside.com 2019 - 2024. All rights reserved.