本文实例为大家分享了Android自定义videoview仿抖音界面的具体代码,供大家参考,具体内容如下
1.效果图
和抖音的界面效果一模一样,而且可以自定义,需要什么页面,请自己定义
2.自定义videoview
application20;
t.Context;
import android.util.AttributeSet;
import android.widget.VideoView;
/**
* 作者:created by Jarchie
* 时间:2020/12/7 15:05:57
* 邮箱:jarchie520@gmail
* 说明:自定义宽高VideoView
*/
public class CusVideoView extends VideoView {
public CusVideoView(Context context) {
super(context);
}
public CusVideoView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CusVideoView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = getDefaultSize(getWidth(), widthMeasureSpec);
int height = getDefaultSize(getHeight(), heightMeasureSpec);
setMeasuredDimension(width, height);
}
}
xmlns:app=""
xmlns:tools=""
android:id="@+id/mRootView"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:id="@+id/mThumb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false"
android:focusable="false"
android:scaleType="centerCrop"
android:visibility="visible" />
android:id="@+id/mPlay"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:alpha="0"
android:clickable="true"
android:focusable="true"
android:src="@drawable/play_arrow" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginLeft="10dp"
android:layout_marginBottom="60dp"
android:orientation="vertical">
android:id="@+id/mTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lineSpacingExtra="5dp"
android:textColor="@android:color/white"
android:textSize="16sp"
tools:text="测试测试数据哈哈哈哈n家里几个垃圾了个两个垃圾" />
android:id="@+id/mVideoView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false"
android:focusable="false" />
4.drawable
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="">
5.主界面设置地址,注意,本demo使用的是本地的视频文件,文件存储再../res/raw文件夹里面,请自行获取
application20;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
dia.MediaPlayer;
import android.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
/**
* 作者:JArchie
* 源码参考地址:
*/
public class MainActivity extends AppCompatActivity {
CusVideoView mVideoView;
private int[] videos = {R.raw.v1, R.raw.v2, R.raw.qi};
TextView mTitle;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
mVideoView = findViewById(R.id.mVideoView);
mTitle = findViewById(R.id.mTitle);
String url = source://" + getPackageName() + "/" + videos[1];
Log.e("TAG", "video_onCreate: " + url);
mVideoView.setVideoURI(Uri.parse(url));
mTitle.setText("@王燕n一起来跳支舞吧");
}
@Override
protected void onStart() {
playVideo();
}
@Override
protected void onDestroy() {
releaseVideo();
}
//播放
private void playVideo() {
Log.e("TAG", "play_video");
// View itemView = ChildAt(0);
final CusVideoView mVideoView = findViewById(R.id.mVideoView);
final ImageView mPlay = findViewById(R.id.mPlay);
final ImageView mThumb = findViewById(R.id.mThumb);
final MediaPlayer[] mMediaPlayer = new MediaPlayer[1];
mVideoView.start();
mVideoView.setOnInfoListener(new MediaPlayer.OnInfoListener() {
@Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
mMediaPlayer[0] = mp;
mp.setLooping(true);
mThumb.animate().alpha(0).setDuration(200).start();
return false;
}
});
//暂停控制
mPlay.setOnClickListener(new View.OnClickListener() {
boolean isPlaying = true;
@Override
public void onClick(View v) {
if (mVideoView.isPlaying()) {
mPlay.animate().alpha(1f).start();
mVideoView.pause();
isPlaying = false;
} else {
mPlay.animate().alpha(0f).start();
mVideoView.start();
isPlaying = true;
}
}
});
}
//释放
private void releaseVideo() {
Log.e("TAG", "releaseVideo_video");
// View itemView = ChildAt(index);
final CusVideoView mVideoView = findViewById(R.id.mVideoView);
final ImageView mThumb = findViewById(R.id.mThumb);
final ImageView mPlay = findViewById(R.id.mPlay);
mVideoView.stopPlayback();
mThumb.animate().alpha(1).start();
mPlay.animate().alpha(0f).start();
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
本文发布于:2024-02-04 19:02:50,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170714287058605.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |