前言
最近也不知道写一点什么好,然后发现一个有趣的就是QQ的视频背景登录,然后想一下还是多简单的就能实现,于是自己也动手写了一写,然后在这里记录一下。
名言
面对人生旅途中的挫折与磨难,我们不仅要有勇气,更要有坚强的信念。
先来看看我们的效果吧,可能GIF有点不给力
那啥画质。好了我们还是来看下是怎么实现的吧。
其实Android的原生里面就有播放视频的控件VideoView。但是我们这个地方需要重新写一下VideoView。所以我们自定义一个MyVideo来继承VideoView。然后在onMeasure里面重新计算控件的高度和宽度,因为我们要做到全屏嘛。
public class MyVideo extends VideoView {
public MyVideo(Context context) {
super(context);
}
public MyVideo(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyVideo(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//从新写入高度
int width = getDefaultSize(0,widthMeasureSpec);
int height = getDefaultSize(0,heightMeasureSpec);
//设置测量尺寸,将高和宽放进去
setMeasuredDimension(width, height);
}
}
基本操作就不解释了。
然后这个写完了就是一个布局的问题了。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<com.example.beiduo.testvideo.MyVideo
android:id="@+id/myvideo"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:orientation="vertical">
<EditText
android:textColor="#ffffff"
android:layout_width="match_parent"
android:layout_height="54dp"
android:background="@null"
android:gravity="center_vertical"
android:hint="账号"
android:textColorHint="#ffffff"
android:textSize="16sp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#50ffffff"></View>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:orientation="vertical">
<EditText
android:textColor="#ffffff"
android:layout_width="match_parent"
android:layout_height="54dp"
android:background="@null"
android:gravity="center_vertical"
android:hint="密码"
android:textColorHint="#ffffff"
android:textSize="16sp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#50ffffff"></View>
</LinearLayout>
<Button
android:background="@drawable/btn_style"
android:layout_width="match_parent"
android:layout_height="44dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="30dp"
android:text="Login"
android:textColor="#ffffff"
android:textSize="16sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="35dp"
android:layout_marginRight="35dp"
android:layout_marginTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="忘记密码"
android:textColor="#ffffff"
android:textSize="12sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="注册用户"
android:textColor="#ffffff"
android:textSize="12sp" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
布局的时候注意自定义控件的包名。
这里有个按钮的样式我也贴出来
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- 填充的颜色 -->
<solid android:color="#70e9a0a0" />
<!-- 设置按钮的四个角为弧形 -->
<corners android:radius="6dip" />
</shape>
颜色的前2位是透明度,00是完全透明—–100是完全不透明
记下来就看看我们的主Activity是怎么写的
public class MainActivity extends AppCompatActivity {
private MyVideo myVideo = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myVideo = (MyVideo) findViewById(R.id.myvideo);
initView();
}
public void initView(){
//播放路径
myVideo.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.myviewtest));
//播放
myVideo.start();
//循环播放
myVideo.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
myVideo.start();
}
});
}
@Override
protected void onRestart() {
//返回重新加载
initView();
super.onRestart();
}
@Override
protected void onStop() {
//防止锁屏或者弹出的时候,音乐在播放
myVideo.stopPlayback();
super.onStop();
}
}
我这里是把一个MP4 的文件放在raw这个文件夹下面,所以获取路径的方法就是这个。
说到这里就完了。感谢~~~!