|
Android酷炫欢迎页播放视频,仿蚂蜂窝自由行和慕课网源码下载
今天无意间看到了蚂蜂窝自由行的app,启动页很酷炫。我记得以前慕课网有个版本的app欢迎页也是播放视频的。 今天就顺手写一个,代码比较简单,高手请略过。
一.资源准备
三个比较短小的视频:视频下载
二.开始编写代码
1.在项目的res下新建一个raw文件夹,放入准备好的这三个视频
2.自定义播放视频的CustomVideoView 在这个自定义View里面提供一个播放视频的方法。用户只需要传入播放路径就可以了,并且可一循环播放。
- package cn.bluemobi.dylan.welcomevideopager;
- import android.content.Context;
- import android.media.MediaPlayer;
- import android.net.Uri;
- import android.util.AttributeSet;
- import android.view.View;
- import android.widget.VideoView;
- /**
- * 可以播放视频的View
- * Created by yuandl on 2016-11-10.
- */
- public class CustomVideoView extends VideoView {
- public CustomVideoView(Context context) {
- super(context);
- }
- public CustomVideoView(Context context, AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- }
- public CustomVideoView(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- super.onMeasure(widthMeasureSpec, heightMeasureSpec);
- setMeasuredDimension(View.MeasureSpec.getSize(widthMeasureSpec), View.MeasureSpec.getSize(heightMeasureSpec));
- }
- /**
- * 播放视频
- *
- * @param uri 播放地址
- */
- public void playVideo(Uri uri) {
- if (uri == null) {
- throw new IllegalArgumentException("Uri can not be null");
- }
- /**设置播放路径**/
- setVideoURI(uri);
- /**开始播放**/
- start();
- setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
- @Override
- public void onPrepared(MediaPlayer mp) {
- /**设置循环播放**/
- mp.setLooping(true);
- }
- });
- setOnErrorListener(new MediaPlayer.OnErrorListener() {
- @Override
- public boolean onError(MediaPlayer mp, int what, int extra) {
- return true;
- }
- });
- }
- }
复制代码 3.建立没个欢迎页面的Fragment去加载自定义视频View的视图
- package cn.bluemobi.dylan.welcomevideopager;
- import android.net.Uri;
- import android.os.Bundle;
- import android.support.annotation.Nullable;
- import android.support.v4.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- /**
- * Created by yuandl on 2016-11-10.
- */
- public class GuildFragment extends Fragment {
- private CustomVideoView customVideoView;
- @Nullable
- @Override
- public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
- customVideoView = new CustomVideoView(getContext());
- /**获取参数,根据不同的参数播放不同的视频**/
- int index = getArguments().getInt("index");
- Uri uri;
- if (index == 1) {
- uri = Uri.parse("android.resource://" + getActivity().getPackageName() + "/" + R.raw.guide_1);
- } else if (index == 2) {
- uri = Uri.parse("android.resource://" + getActivity().getPackageName() + "/" + R.raw.guide_2);
- } else {
- uri = Uri.parse("android.resource://" + getActivity().getPackageName() + "/" + R.raw.guide_3);
- }
- /**播放视频**/
- customVideoView.playVideo(uri);
- return customVideoView;
- }
- /**
- * 记得在销毁的时候让播放的视频终止
- */
- @Override
- public void onDestroy() {
- super.onDestroy();
- if (customVideoView != null) {
- customVideoView.stopPlayback();
- }
- }
- }
复制代码
4.界面布局
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <android.support.v4.view.ViewPager
- android:id="@+id/vp"
- android:layout_width="match_parent"
- android:layout_height="match_parent"></android.support.v4.view.ViewPager>
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- android:layout_centerHorizontal="true"
- android:layout_marginBottom="130dp"
- android:orientation="horizontal">
- <ImageView
- android:id="@+id/iv1"
- android:layout_width="10dp"
- android:layout_height="10dp"
- android:src="@mipmap/dot_focus" />
- <ImageView
- android:id="@+id/iv2"
- android:layout_width="10dp"
- android:layout_height="10dp"
- android:layout_marginLeft="10dp"
- android:src="@mipmap/dot_normal" />
- <ImageView
- android:id="@+id/iv3"
- android:layout_width="10dp"
- android:layout_height="10dp"
- android:layout_marginLeft="10dp"
- android:src="@mipmap/dot_normal" />
- </LinearLayout>
- <Button
- android:id="@+id/bt_start"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- android:layout_centerHorizontal="true"
- android:layout_marginBottom="50dp"
- android:background="@mipmap/bt_start"
- android:textColor="@android:color/white"
- android:visibility="gone" />
- </RelativeLayout>
复制代码 5.给界面添加Fragment
三.下载地址:
Android酷炫欢迎页播放视频,仿蚂蜂窝自由行和慕课网源码下载
http://bbs.jiandaima.com/thread-733-1-1.html
|
|