安卓作業----慕課移動應用開發作業23之自定義VideoView,實現動態背景的登錄界面

本篇利用自定義VideoView,實現動態背景的登錄界面。

同時這也是中國大學慕課移動終端應用開發的網課作業23

說明

參考如下博客內容

效果圖

在這裏插入圖片描述

代碼部分

1.MyVideoView.java
public class MyVideoView extends VideoView {
    public MyVideoView(Context context) {
        super(context);
    }

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

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

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public MyVideoView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(widthSpecSize, heightSpecSize);
    }
    /**
     * https://blog.csdn.net/jdsjlzx/article/details/51777320
     *
     * https://blog.csdn.net/qq_24697659/article/details/49800419
     *
     * https://www.jianshu.com/p/2d3b221a2ee7
     *
     * https://blog.csdn.net/z19980115/article/details/75046606
     * */
}

2.MainActivity.java
public class MainActivity extends AppCompatActivity {
    private MyVideoView mVideoView;
    private EditText mEditTextName,mEditTextPassword;

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

        mVideoView = findViewById(R.id.video_view);
        mEditTextName = findViewById(R.id.nameEditText);
        mEditTextPassword = findViewById(R.id.passwordEditText);
        mEditTextName.setHintTextColor(getResources().getColor(R.color.white));
        mEditTextPassword.setHintTextColor(getResources().getColor(R.color.white));

        Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.myvideo);
        mVideoView.setVideoURI(uri);

        mVideoView.start();
//
//        mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
//            @Override
//            public void onCompletion(MediaPlayer mp) {
//                mVideoView.resume();
//            }
//        });
    }
}

3.activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.example.course23.MyVideoView
        android:id="@+id/video_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <RelativeLayout
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <RelativeLayout
            android:id="@+id/viewCenter"
            android:paddingTop="20dp"
            android:layout_centerHorizontal="true"
            android:layout_width="280dp"
            android:layout_height="wrap_content">

            <EditText
                android:id="@+id/nameEditText"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:drawablePadding="10dp"
                android:hint="請輸入姓名"
                android:layout_marginLeft="10dp"

                android:textSize="16sp"></EditText>


            <EditText
                android:id="@+id/passwordEditText"
                android:layout_below="@+id/nameEditText"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:drawablePadding="10dp"
                android:hint="請輸入密碼"
                android:textSize="16sp"
                android:layout_marginLeft="10dp"></EditText>


        </RelativeLayout>
        <RelativeLayout
            android:id="@+id/login"
            android:layout_below="@id/viewCenter"
            android:layout_centerHorizontal="true"
            android:layout_width="280dp"
            android:layout_height="wrap_content"
            android:paddingBottom="10dp"
            android:layout_marginTop="10dp">
            <TextView
                android:text="快速註冊"
                android:textColor="#59EEF8"
                android:paddingLeft="10dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <TextView
                android:text="忘記密碼"
                android:paddingRight="10dp"
                android:layout_alignParentRight="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#59EEF8"/>
        </RelativeLayout>
        <RelativeLayout
            android:layout_marginTop="10dp"
            android:layout_below="@id/login"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:text="登陸"
                android:textSize="20dp"
                android:layout_centerInParent="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#59EEF8"/>
        </RelativeLayout>



    </RelativeLayout>


</RelativeLayout>
4.修改colors.xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#72141414</color>
    <color name="colorPrimaryDark">#72141414</color>
    <color name="colorAccent">#D81B60</color>
    <color name="white">#FFF</color>
</resources>
5.修改styles.xml文件
<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

        <!-- AppCompatEditText默認狀態狀態設置底線顏色 -->
        <item name="colorControlNormal">#FFFFFF</item>
        <!-- AppCompatEditText選擇的底線顏色 -->
        <item name="colorControlActivated">#c6174e</item>
    </style>

</resources>

視頻資源

在res目錄下新建raw文件夾,將自己的視頻保存爲myvideo.mp4即可
我的視頻資源來自於火螢app

總結

如果有什麼問題或改進方案,請私信聯繫我或者在評論區留言
碼字不易,若有幫助,給個關注和讚唄

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章