登陆QQ的时候,咱们会看到在登陆界面的背景不是静态的,而是一段动画效果,刚开始以为蛮好奇的,如今咱们也来实现一下这种效果,实现起来仍是挺简单的。android
实现步骤:ide
一、自定义CustomVideoView类继承VideoView 二、实现xml布局文件 三、将视频文件放入raw目录 四、代码实现动画效果 五、静态效果图展现
实现过程:布局
一、自定义CustomVideoView类继承VideoView学习
package com.showly.bmobdemo.utils; import android.content.Context; import android.media.MediaPlayer; import android.util.AttributeSet; import android.view.KeyEvent; import android.widget.VideoView; /** * Created by Administrator */ 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 defStyle) { super(context, attrs, defStyle); } @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); } }
二、实现xml布局文件动画
<com.showly.bmobdemo.utils.CustomVideoView android:id="@+id/videoview" android:layout_width="match_parent" android:layout_height="match_parent" />
三、将视频文件放入raw目录spa
四、代码实现动画效果code
//找VideoView控件 customVideoView = (CustomVideoView)findViewById(R.id.videoview); //加载视频文件 customVideoView.setVideoURI(Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.sport)); //播放 customVideoView.start(); //循环播放 customVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mediaPlayer) { customVideoView.start(); } });
五、静态效果图展现
注:效果是视频动画,这里只截了一帧视频
到这里就完成了,源码:公众号回复 "仿QQ登陆背景动画效果"xml
如下是我的公众号(longxuanzhigu),以后发布的文章会同步到该公众号,方便交流学习Android知识及分享我的爱好文章: 继承