仿QQ等的酷炫的视频背景登录页或者引导页
看到QQ的登录页比较酷炫,于是自己在网上学习着,做了个类似的,实际上很简单。接下来附上代码吧,一遍以后自己要用到。
1.在res目录下建个目录raw,该目录下把视频放进去。
2.新建个Java 类:
package com.pactera.madp.videobc.views;
import android.content.Context;
import android.media.MediaPlayer;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.widget.VideoView;
/**
* Created by Monica on 2017/11/29.
*/
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 l) {
super.setOnPreparedListener(l);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return super.onKeyDown(keyCode, event);
}
}
3.接下来是Activity文件:
package com.pactera.madp.videobc;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.pactera.madp.videobc.views.CustomVideoView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private CustomVideoView videoview;
private Button btn_start;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
/**
* 初始化
*/
private void initView() {
btn_start = (Button) findViewById(R.id.btn_start);
btn_start.setOnClickListener(this);
videoview = (CustomVideoView) findViewById(R.id.videoview);
//设置播放加载路径
videoview.setVideoURI(Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.guide_1));
//播放
videoview.start();
//循环播放
videoview.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
videoview.start();
}
});
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btn_start:
Toast.makeText(this,"进入了主页",Toast.LENGTH_SHORT).show();
break;
}
}
}
就是这么简单。
4。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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.pactera.madp.videobc.MainActivity">
<com.pactera.madp.videobc.views.CustomVideoView
android:id="@+id/videoview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="@+id/btn_start"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="56dp"
android:text="进入" />
</RelativeLayout>
5.就这样就完成了哦。