Android进阶之路

来源:互联网 发布:网络服务器配置与管理 编辑:程序博客网 时间:2024/06/07 18:11

有段时间没有登手机QQ,登陆之后发现登陆的界面还是蛮炫的,比较引人入目,所以就在网上找了一下Demo,此Demo就是建立在之前的一位网友Demo的基础上进行了尝试扩展,下面的代码加以修改,均可copy直接用于开发使用。

QQ登陆效果:

QQ登陆

Our Effect :

这里写图片描述

在正式开始之前,请先认真看一下现在的提要!

前期提要:
1.本身我要使用VideoView进行实现的,显示效果之后,显示不全,所以我们要用到自定义View进行测量补全

2.Title的取消,默认有俩个Title,一个是app的,一个是手机本身的,这里的话我只取消了app本身的,在application下添加下面这行代码

android:theme="@style/Theme.AppCompat.NoActionBar"

3.使用视频的背景首先要在 res目录下-创建raw文件目录(此文件下的资源不会被压缩)-放入我们在当做背景的视频(一般为mp4格式)

4.最好在对应的Activity加入以下代码,确保不会重复的去横竖屏切换

   android:screenOrientation="portrait"

5.此模式的优缺点
优点:方式独特,引人注意,提升用户体验性
缺点:不利于瘦身app的要求,因为压缩会失贞,不压缩打包的时候又增加了app大小

个人观点:找个内存小的MP4就好,毕竟吸引我们这群用户才是关键啊,反正就1-5MB。

纯属个人观点,请直接看代码吧,简单易懂。

MainActivity :

package com.yl.videologin.videologin;import android.media.MediaPlayer;import android.net.Uri;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.widget.VideoView;public class MainActivity extends AppCompatActivity {    private VideoView mVideoView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();    }    private void initView() {        mVideoView = (VideoView) findViewById(R.id.videoview);        //视频位置        mVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video));        //开始播放前的准备        mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {            @Override            public void onPrepared(MediaPlayer mp) {                //静音,如需声音可去掉                mp.setVolume(0f, 0f);                mVideoView.start();            }        });        //循环        mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {            @Override            public void onCompletion(MediaPlayer mp) {                mVideoView.start();            }        });    }    //返回重启加载    @Override    protected void onRestart() {        initView();        super.onRestart();    }    //防止锁屏或者切出的时候,音乐在播放    @Override    protected void onStop() {        mVideoView.stopPlayback();        super.onStop();    }}

MainActivity Xml:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.yl.videologin.videologin.MainActivity">    <com.yl.videologin.videologin.CustomVideoView        android:id="@+id/videoview"        android:layout_width="match_parent"        android:layout_height="match_parent" />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:orientation="vertical">        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="horizontal"            android:paddingLeft="30dp"            android:paddingRight="30dp">            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="账户:"                android:padding="5dp"                android:textColor="#fff" />            <EditText                android:layout_width="0dp"                android:layout_height="20dp"                android:layout_weight="1"                android:background="@null"                android:textSize="14sp"                android:textColor="#f00"                android:textColorHint="#fff"                 />        </LinearLayout>        <View            android:layout_marginRight="30dp"            android:layout_marginLeft="30dp"            android:layout_width="match_parent"            android:layout_height="2dp"            android:background="#BDCCD0" />        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="horizontal"            android:paddingLeft="30dp"            android:paddingRight="30dp">            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="密码:"                android:padding="5dp"                android:textColor="#fff" />            <EditText                android:layout_width="0dp"                android:layout_height="20dp"                android:layout_weight="1"                android:background="@null"                android:textSize="14sp"                android:textColor="#f00"                android:textColorHint="#fff"                />        </LinearLayout>        <TextView            android:layout_marginTop="5dp"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="登陆"            android:background="#1A6EAC"            android:layout_marginLeft="30dp"            android:layout_marginRight="30dp"            android:padding="2dp"            android:gravity="center"            android:textColor="#fff"            />    </LinearLayout></RelativeLayout>

CustomVideoView (确保VideoView完全显示):

package com.yl.videologin.videologin;import android.content.Context;import android.media.MediaPlayer;import android.util.AttributeSet;import android.view.KeyEvent;import android.widget.VideoView;public class CustomVideoView extends VideoView {    public CustomVideoView(Context context) {        super(context);    }    public CustomVideoView(Context context, AttributeSet attrs) {        super(context, attrs);    }    public CustomVideoView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        //我们重新计算高度        int width = getDefaultSize(0, widthMeasureSpec);        int height = getDefaultSize(0, heightMeasureSpec);        setMeasuredDimension(width, height);    }    //以下俩个方法,觉得没有重写的必要,个人看法    @Override    public void setOnPreparedListener(MediaPlayer.OnPreparedListener view) {        super.setOnPreparedListener(view);    }    @Override    public boolean onKeyDown(int keyCode, KeyEvent event) {        return super.onKeyDown(keyCode, event);    }}
原创粉丝点击