VideoView引导页

来源:互联网 发布:淘宝联盟怎么领券 编辑:程序博客网 时间:2024/05/16 10:07

首先创建一个类继承VideoView实现其中的构造方法,然后是测量方法如下代码;

<span style="font-size:18px;">public class CustomViewDiveo extends VideoView {    public CustomViewDiveo(Context context) {</span><h2><span style="font-size:18px;">        super(context);</span></h2><span style="font-size:18px;">    }    public CustomViewDiveo(Context context, AttributeSet attrs) {        super(context, attrs);    }    public CustomViewDiveo(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);    }    @Override    public void setOnPreparedListener(MediaPlayer.OnPreparedListener l) {        super.setOnPreparedListener(l);    }    @Override    public boolean onKeyDown(int keyCode, KeyEvent event) {        return super.onKeyDown(keyCode, event);    }</span>

如下是activity_main.xml的布局然后在res下建一个raw的文件下放.mp4格式的视频
<span style="color:#000099;"><?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"    android:layout_width="match_parent" android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.huanyingjiemian.MainActivity">    <com.example.huanyingjiemian.customviewdevio.CustomViewDiveo        android:id="@+id/viewdiveo"        android:layout_width="match_parent"        android:layout_height="match_parent"></com.example.huanyingjiemian.customviewdevio.CustomViewDiveo>    <Button        android:id="@+id/btn_start"        android:layout_width="100dp"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_centerHorizontal="true"        android:layout_marginBottom="56dp"        android:text="进入" /></RelativeLayout></span>

下面是Activity的代码也是一目了然;

<span style="font-size:18px;">package com.example.huanyingjiemian;import android.media.MediaPlayer;import android.net.Uri;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.Toast;import com.example.huanyingjiemian.customviewdevio.CustomViewDiveo;public class MainActivity extends AppCompatActivity implements View.OnClickListener{    private Button button;    private CustomViewDiveo viewdiveo;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initview();    }    private void initview() {        button = (Button) findViewById(R.id.btn_start);        viewdiveo = (CustomViewDiveo) findViewById(R.id.viewdiveo);        button.setOnClickListener(this);        //设置播放加载路径        viewdiveo.setVideoURI(Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.bofang));        //播放        viewdiveo.start();        //循环播放        viewdiveo.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {            @Override            public void onCompletion(MediaPlayer mediaPlayer) {                viewdiveo.start();            }        });    }    @Override    public void onClick(View v) {        switch (v.getId()){            case R.id.btn_start:                Toast.makeText(this,"进入了主页",Toast.LENGTH_SHORT).show();                break;        }    }}</span>
好了终于完工了;是不是很简单;

0 0