看到QQ的动态背景引导页感觉很炫酷,我也来操作一把
先看效果

效果
第一步,自定义Videoview
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中写入
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/lin"
android:layout_width="match_parent"
android:background="@mipmap/backgro"
android:layout_height="match_parent"
tools:context=".activity.LoginActivity">
<com.zkzj.ztl_tests.view.CustomVideoView
android:id="@+id/videoview"
android:layout_width="match_parent"
android:visibility="gone"
android:background="@color/white"
android:layout_height="match_parent" />
<!-- android:paddingTop="@dimen/dp_30"-->
<LinearLayout
android:id="@+id/mlin"
android:layout_width="match_parent"
android:gravity="top"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/dp_20"
android:layout_marginTop="60dp"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/iv_user"
android:layout_width="65dp"
android:layout_height="65dp"
android:src="@drawable/user" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="50dp">
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="用户:"
android:textSize="16sp" />
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginLeft="5dp"
android:layout_marginBottom="13dp"
android:layout_toRightOf="@+id/tv_name"
android:background="@null"
android:hint="请输入用户名" />
<ImageView
android:id="@+id/user_delete"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/delete" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:layout_marginLeft="@dimen/dp_5"
android:layout_marginBottom="6dp"
android:layout_toRightOf="@+id/tv_name"
android:background="@color/color_lin" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="@dimen/dp_20">
<TextView
android:id="@+id/tv_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="密码:"
android:textSize="16sp" />
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/dp_5"
android:layout_marginBottom="13dp"
android:layout_toRightOf="@+id/tv_password"
android:background="@null"
android:hint="请输入密码"
android:inputType="textPassword"
android:maxLines="1"
android:textColor="@android:color/black"
android:textColorHint="@color/tv_gray_deep" />
<ImageView
android:id="@+id/see_password"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@mipmap/icon_nosee_pass" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:layout_marginLeft="@dimen/dp_5"
android:layout_marginBottom="6dp"
android:layout_toRightOf="@+id/tv_password"
android:background="@color/color_lin" />
</RelativeLayout>
<!-- android:background="@drawable/touch_bg_tuceng"-->
<com.zkzj.ztl_tests.view.CircleImageView
android:id="@+id/login"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginTop="40dp"
android:background="?android:attr/selectableItemBackground"
android:contentDescription="@string/app_name"
android:src="@drawable/login" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- <com.zkzj.android_commer.util.WaveView-->
<!-- android:layout_width="@dimen/qb_px_100"-->
<!-- android:layout_height="match_parent"-->
<!-- android:layout_centerInParent="true"-->
<!-- android:background="#ffffff"-->
<!-- />-->
<!-- <com.zkzj.android_commer.util.ScrapeView-->
<!-- android:layout_width="@dimen/qb_px_280"-->
<!-- android:layout_height="@dimen/qb_px_140"-->
<!-- android:layout_centerInParent="true"-->
<!-- />-->
<!-- content view -->
<TextView
android:id="@+id/tv_ggl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="待开奖"
android:textColor="#Ff00"
android:textSize="30sp" />
<com.zkzj.ztl_tests.view.ScratchView
android:id="@+id/scratch_view"
android:layout_width="300dp"
android:layout_height="180dp"
android:layout_centerInParent="true"
android:background="@drawable/shape_ggl" />
</RelativeLayout>
</LinearLayout>
</FrameLayout>
第三步,在Activity中填写
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private CustomVideoView videoview;
private Button btn_enter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initView();
}
/**
* 初始化
*/
private void initView() {
btn_enter = (Button) findViewById(R.id.btn_enter);
btn_enter.setOnClickListener(this);
videoview = (CustomVideoView) findViewById(R.id.videoview);
videoview.setVideoURI(Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.sport));
//播放
videoview.start();
//防止播放视频出现黑屏(利用Background的背景颜色)
mVideoview.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.setOnInfoListener(new MediaPlayer.OnInfoListener() {
@Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START)
mVideoview.setBackgroundColor(Color.TRANSPARENT);
return true;
}
});
}
});
//循环播放
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_enter:
Toast.makeText(this,"登录成功了",Toast.LENGTH_SHORT).show();
break;
}
}
//返回重启加载
@Override
protected void onRestart() {
super.onRestart();
initView();
}
//防止锁屏或者切出的时候,音乐在播放
@Override
protected void onStop() {
super.onStop();
videoview.stopPlayback();
}
}
网友评论