Android 视频背景,视频背景登录

前言

最近也不知道写一点什么好,然后发现一个有趣的就是QQ的视频背景登录,然后想一下还是多简单的就能实现,于是自己也动手写了一写,然后在这里记录一下。

名言
面对人生旅途中的挫折与磨难,我们不仅要有勇气,更要有坚强的信念。


先来看看我们的效果吧,可能GIF有点不给力
这里写图片描述

这里写图片描述
那啥画质。好了我们还是来看下是怎么实现的吧。


其实Android的原生里面就有播放视频的控件VideoView。但是我们这个地方需要重新写一下VideoView。所以我们自定义一个MyVideo来继承VideoView。然后在onMeasure里面重新计算控件的高度和宽度,因为我们要做到全屏嘛。

public class MyVideo extends VideoView {
    public MyVideo(Context context) {
        super(context);
    }

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

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

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //从新写入高度
        int width = getDefaultSize(0,widthMeasureSpec);
        int height = getDefaultSize(0,heightMeasureSpec);
        //设置测量尺寸,将高和宽放进去
        setMeasuredDimension(width, height); 
    }
}

基本操作就不解释了。
然后这个写完了就是一个布局的问题了。


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <com.example.beiduo.testvideo.MyVideo
        android:id="@+id/myvideo"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="3">

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="5"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="55dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:orientation="vertical">

                <EditText
                    android:textColor="#ffffff"
                    android:layout_width="match_parent"
                    android:layout_height="54dp"
                    android:background="@null"
                    android:gravity="center_vertical"
                    android:hint="账号"
                    android:textColorHint="#ffffff"
                    android:textSize="16sp" />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="#50ffffff"></View>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="55dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:orientation="vertical">

                <EditText
                    android:textColor="#ffffff"
                    android:layout_width="match_parent"
                    android:layout_height="54dp"
                    android:background="@null"
                    android:gravity="center_vertical"
                    android:hint="密码"
                    android:textColorHint="#ffffff"
                    android:textSize="16sp" />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="#50ffffff"></View>
            </LinearLayout>

            <Button
                android:background="@drawable/btn_style"
                android:layout_width="match_parent"
                android:layout_height="44dp"
                android:layout_marginLeft="30dp"
                android:layout_marginRight="30dp"
                android:layout_marginTop="30dp"
                android:text="Login"
                android:textColor="#ffffff"
                android:textSize="16sp" />

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="35dp"
                android:layout_marginRight="35dp"
                android:layout_marginTop="10dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:text="忘记密码"
                    android:textColor="#ffffff"
                    android:textSize="12sp" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:text="注册用户"
                    android:textColor="#ffffff"
                    android:textSize="12sp" />
            </RelativeLayout>


        </LinearLayout>

    </LinearLayout>


</RelativeLayout>

布局的时候注意自定义控件的包名。

这里有个按钮的样式我也贴出来

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!-- 填充的颜色 -->
    <solid android:color="#70e9a0a0" />
    <!-- 设置按钮的四个角为弧形 -->
    <corners android:radius="6dip" />
</shape>

颜色的前2位是透明度,00是完全透明—–100是完全不透明

记下来就看看我们的主Activity是怎么写的

public class MainActivity extends AppCompatActivity {
    private MyVideo myVideo = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myVideo = (MyVideo) findViewById(R.id.myvideo);
        initView();
    }

    public void initView(){
        //播放路径
        myVideo.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.myviewtest));
        //播放
        myVideo.start();
        //循环播放
        myVideo.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                myVideo.start();
            }
        });
    }
    @Override
    protected void onRestart() {
        //返回重新加载
        initView();
        super.onRestart();
    }


    @Override
    protected void onStop() {
        //防止锁屏或者弹出的时候,音乐在播放
        myVideo.stopPlayback();
        super.onStop();
    }
}

我这里是把一个MP4 的文件放在raw这个文件夹下面,所以获取路径的方法就是这个。
说到这里就完了。感谢~~~!

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
您可以使用VideoView来将视频作为Android应用程序的背景。下面是一些基本的代码示例: 1. 在XML布局文件中添加VideoView组件 ``` <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/root" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <VideoView android:id="@+id/videoView" android:layout_width="match_parent" android:layout_height="match_parent" /> <!-- 在这里添加其他组件 --> </RelativeLayout> ``` 2. 在Activity中设置视频文件和循环播放 ``` public class MainActivity extends AppCompatActivity { private VideoView videoView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); videoView = findViewById(R.id.videoView); videoView.setVideoPath("android.resource://" + getPackageName() + "/" + R.raw.background_video); videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mediaPlayer) { mediaPlayer.setLooping(true); } }); videoView.start(); } } ``` 3. 将视频文件放入res/raw文件夹中 在这个示例中,我们将视频文件命名为background_video.mp4并将其放入res/raw文件夹中。您可以将其替换为您自己的视频文件,并相应地更改setVideoPath()方法。 希望这能帮助您将视频作为Android应用程序的背景
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值