VideoView引导页

首先创建一个类继承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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值