类似Tumblr, Spotify, Keep等应用在登录界面都有要采用了背景是动画的效果。
1、布局文件:activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/home_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.huan.percy.backgroundvideotest.FullScreenVideoView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_gravity="center" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="手机/邮箱"
android:textColor="#fff"
android:textColorHint="#fff"
android:background="@null"
android:textSize="20sp" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@mipmap/fls"
android:scaleType="fitXY"/>
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:hint="密码"
android:textColorHint="#fff"
android:textColor="#fff"
android:background="@null"
android:textSize="20sp" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@mipmap/fls"
android:scaleType="fitXY"/>
<Button
android:id="@+id/login"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@mipmap/nhl"
android:layout_marginTop="30dp"
android:text="登 录"
android:textColor="#fff"
android:textSize="25sp" />
</LinearLayout>
</RelativeLayout>
2、在raw文件夹中添加播放的MP4资源
3、自定义VideoView,使得播放视频填充屏幕
/**
* Created by ZhuMing on 2017/7/12.
* 自定义 填充屏幕的VideoView
*/
public class FullScreenVideoView extends VideoView {
public FullScreenVideoView(Context context) {
super(context);
}
public FullScreenVideoView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public FullScreenVideoView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
int width = wm.getDefaultDisplay().getWidth();
int height = wm.getDefaultDisplay().getHeight();
setMeasuredDimension(width, height);
}
}
4、在需要加载动态背景的Activity的OnCreate()方法中加入相关控制代码,实现自动循环播放
public class MainActivity extends AppCompatActivity {
private MediaPlayer mp = null;
private FullScreenVideoView myVideoView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
final String videoPath = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.welcome_video).toString();
myVideoView.setVideoPath(videoPath);
myVideoView.start();
myVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
mp.setLooping(true);
}
});
myVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
myVideoView.setVideoPath(videoPath);
myVideoView.start();
}
});
}
private void initView() {
myVideoView = (FullScreenVideoView) findViewById(R.id.videoView);
}
}