仿易信引导页面

目前的引导页面大多数就是ViewPager,不过已经有不少app的引导页面变为动画+viewpager,第一次见到,感受很新颖,用户体验会瞬间提高一阶。那么问题来了,这样的引导页面怎么作的呢?android

曾经一度用易信,有一次更新版本后发现易信的引导页面就是这种状况,感受很新颖。昨天下载了虾米音乐,用的也是这样的,但跟这个有区别。git

首先看效果图:
图片描述github

刚开始见到觉得后面是动态图片作背景。后来解压了app,发现里面是一段mp4。那么这样就好写了segmentfault

思路:布局为视频+viewpager

布局文件:

<?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.kevin.tech.vedioguidedemo.MainActivity">

    <com.kevin.tech.vedioguidedemo.CustomizeVideoView
        android:id="@+id/video_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="100dp">

        <android.support.v4.view.ViewPager
            android:id="@+id/view_pager"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true" />

        <LinearLayout
            android:id="@+id/indicator"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="20dp"
            android:orientation="horizontal" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="30dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingLeft="15dp"
            android:paddingRight="15dp">

            <Button
                android:id="@+id/btn_register"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@color/green_1"
                android:text="注册"
                android:textColor="@color/white"
                android:textSize="16sp" />

            <Button
                android:id="@+id/btn_login"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_weight="1"
                android:background="@drawable/shape_bg_button_transparent"
                android:text="登陆"
                android:textColor="@color/white"
                android:textSize="16sp" />
        </LinearLayout>
    </RelativeLayout>
</RelativeLayout>

这里的视频布局用的是VedioView(重写过的)。其余布局就是viewpager,button的了,比较简单。app

布局写好了,问题就简单了,直接加载视频就行了。ide

视频的加载

mVideoView = (CustomizeVideoView) findViewById(R.id.video_view);
mVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.media));//获取视频
mVideoView.start();//开始播放
mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                mVideoView.start();
            }
        });
    }

ViewPager的添加

无限轮播viewpager正好以前我已经写过了。相信不少人也都会写。有问题的能够参考我以前写的《viewpager自添加指示器,无限轮播》和 《ViewPager的自动轮播》(谢谢支持)。布局

Button处理

那么问题来了,视频是否是一直在播放呢,这样毫无疑问确定会很耗内存的。因此这里还有控制视频的中止播放。即在处理Button事件的时候添加视频中止播放并释放内存便可学习

mVideoView.stopPlayback();//视频中止播放并释放内存

我再Demo里写的视频的暂停和继续播放,由于易信的没有这个,本身只是练习。在真正写代码的时候我认为是不添加暂停和继续播放更符合要求的。动画

  • 视频暂停:ui

mVideoView.pause();
currentPosition = mVideoView.getCurrentPosition();//暂停后获取当前播放的位置
Toast.makeText(MainActivity.this, "暂停播放",    Toast.LENGTH_SHORT).show();
  • 视频继续:

mVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.media));//获取视频资源
 mVideoView.seekTo(currentPosition);//将视频移动到暂停时的播放位置
 mVideoView.start();//开始播放
 Toast.makeText(MainActivity.this, "继续播放", Toast.LENGTH_SHORT).show();

这里附上我写的Demo

点击下载一

点击下载二

但愿当帮助到各位同窗,欢迎互相学习互相交流!

相关文章
相关标签/搜索