今天给将项目中实现的一个小功能分享下,我觉得挺有意思的,比如写个登录界面背景图片用什么样子,那个图片,色彩,风格什么的都比较那把握,今天教大家一招,直接将背景图片给成弄一个视频,那样看起来既好看又有逼格;
先看效果图
由于没有办法上传视频,大家也就脑补下吧,下面是在播放视频-_-
怎么实现的呢?其实很简单的:
布局文件
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:background="@drawable/pjw"
android:layout_height="match_parent">
<com.example.com.control.Views.fullScreen
android:id="@+id/videoview"
android:layout_width="match_parent"
android:layout_gravity="center"
android:layout_height="match_parent"
android:focusable="false"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:drawableLeft="@drawable/shou"
android:layout_width="match_parent"
android:paddingTop="120dp"
android:layout_height="200dp"
android:layout_marginLeft="45dp"
android:layout_marginRight="215dp"
android:text="掌空"
android:textSize="30sp"
android:textColor="#ffffff"
android:gravity="center"/>
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="账号"
android:textColorHint="#e062c5f2"
android:background="@null"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"/>
<View
android:layout_marginTop="3dp"
android:layout_width="match_parent"
android:layout_height="2px"
android:background="#e0ffffff"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"/>
<EditText
android:hint="密码"
android:textColorHint="#e062c5f2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@null"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"/>
<View
android:layout_marginTop="3dp"
android:layout_width="match_parent"
android:layout_height="2px"
android:background="#e0ffffff"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"/>
<Button
android:layout_marginTop="20dp"
android:layout_marginRight="50dp"
android:layout_marginLeft="50dp"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="登录"
android:background="@drawable/loginbg"
android:textColor="#ffffff"/>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="15dp"
android:layout_marginRight="50dp"
android:layout_marginLeft="50dp"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="忘记密码?"
android:textColor="#e062c5f2"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="新用户注册"
android:gravity="right"
android:textColor="#e062c5f2"/>
</LinearLayout>
<TextView
android:layout_marginBottom="20dp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="bottom|center"
android:text="888888888"
android:textColor="#ffffff"/>
</LinearLayout>
</FrameLayout>
fullScreen是我自定义的一个类,继承自VideoView这个类里面其实就是实现视频播放宽度和手机屏幕一样宽,(要不然就不好看了);
上码:
public class fullScreen extends VideoView {
public fullScreen(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public fullScreen(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public fullScreen(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {//这里重写onMeasure的方法
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = getDefaultSize(0, widthMeasureSpec);//得到默认的大小(0,宽度测量规范)
int height = getDefaultSize(0, heightMeasureSpec);//得到默认的大小(0,高度度测量规范)
setMeasuredDimension(width, height); //设置测量尺寸,将高和宽放进去
}
}
其他的我也就不多说了,都是一些简单的布局,如果你想做的更好看,那就在细节上花费点功夫吧!很容易发现我是使用了FrameLayout,现在的你是不是想问 ,如果点击那会不会弹出videoView应该有的控制栏?
告诉你不会,如果你想清除的理解为什么,建议去看看View的事件分发机制,扯远了,继续看,我是怎么控制视频播放的呢:
简单,看码:
private void initEvent() {
String path=getContext().getExternalCacheDir().getAbsolutePath();
path=path.split("Android")[0];
File file=new File(path+"DCIM/Video/1223.MP4");
final Uri uri = Uri.parse(file.getAbsolutePath());
if (!file.exists()) {
Toast.makeText(getContext(), "视频文件路径错误", Toast.LENGTH_SHORT).show();
}else {
final android.widget.MediaController mp=new android.widget.MediaController(getContext());
mp.setVisibility(View.INVISIBLE);
videoView.setMediaController(mp);
videoView.setClickable(false);
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
videoView.setVideoURI(uri);
videoView.start();
}
});
videoView.setVideoURI(uri);
videoView.start();
}
}
我是控制让视频循环播放,如果你不需要只要将setOnCompletionListener方法去掉即可;视频文件是我本机上的,如果你想从网上直接获取,那么只要将文件路径改成视频的URL即可;