提交 57f68e73 编写于 作者: S shuyu

进度条预览的另类实现 (2017-09-04)

上级 3e04b3c3
......@@ -12,6 +12,7 @@ import android.widget.ImageView;
import android.widget.RelativeLayout;
import com.example.gsyvideoplayer.listener.SampleListener;
import com.example.gsyvideoplayer.video.PreViewGSYVideoPlayer;
import com.example.gsyvideoplayer.view.ScrollWebView;
import com.shuyu.gsyvideoplayer.builder.GSYVideoOptionBuilder;
import com.shuyu.gsyvideoplayer.video.base.GSYVideoPlayer;
......@@ -33,7 +34,7 @@ public class WebDetailActivity extends AppCompatActivity {
@BindView(R.id.scroll_webView)
ScrollWebView webView;
@BindView(R.id.web_player)
NormalGSYVideoPlayer webPlayer;
PreViewGSYVideoPlayer webPlayer;
@BindView(R.id.web_top_layout)
NestedScrollView webTopLayout;
@BindView(R.id.web_top_layout_video)
......
package com.example.gsyvideoplayer.video;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.gsyvideoplayer.R;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.shuyu.gsyvideoplayer.utils.CommonUtil;
import com.shuyu.gsyvideoplayer.utils.Debuger;
import com.shuyu.gsyvideoplayer.video.NormalGSYVideoPlayer;
import com.shuyu.gsyvideoplayer.video.base.GSYBaseVideoPlayer;
/**
* 进度图小图预览的另类实现
* Created by shuyu on 2016/12/10.
*/
public class PreViewGSYVideoPlayer extends NormalGSYVideoPlayer {
private RelativeLayout mPreviewLayout;
private ImageView mPreView;
//是否因为用户点击
private boolean mIsFromUser;
//是否打开滑动预览
private boolean mOpenPreView = true;
private int mPreProgress = -2;
/**
* 1.5.0开始加入,如果需要不同布局区分功能,需要重载
*/
public PreViewGSYVideoPlayer(Context context, Boolean fullFlag) {
super(context, fullFlag);
}
public PreViewGSYVideoPlayer(Context context) {
super(context);
}
public PreViewGSYVideoPlayer(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void init(Context context) {
super.init(context);
initView();
}
private void initView() {
mPreviewLayout = (RelativeLayout) findViewById(R.id.preview_layout);
mPreView = (ImageView) findViewById(R.id.preview_image);
}
@Override
public int getLayoutId() {
return R.layout.video_layout_preview;
}
@Override
protected void prepareVideo() {
super.prepareVideo();
}
@Override
public void onProgressChanged(SeekBar seekBar, final int progress, boolean fromUser) {
super.onProgressChanged(seekBar, progress, fromUser);
if (fromUser && mOpenPreView) {
int width = seekBar.getWidth();
int time = progress * getDuration() / 100;
int offset = (int) (width - (getResources().getDimension(R.dimen.seek_bar_image) / 2)) / 100 * progress;
Debuger.printfError("***************** " + progress);
Debuger.printfError("***************** " + time);
showPreView(mOriginUrl, time);
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) mPreviewLayout.getLayoutParams();
layoutParams.leftMargin = offset;
//设置帧预览图的显示位置
mPreviewLayout.setLayoutParams(layoutParams);
if (GSYVideoManager.instance().getMediaPlayer() != null
&& mHadPlay && (mOpenPreView)) {
mPreProgress = progress;
}
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
super.onStartTrackingTouch(seekBar);
if (mOpenPreView) {
mIsFromUser = true;
mPreviewLayout.setVisibility(VISIBLE);
mPreProgress = -2;
}
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
if (mOpenPreView) {
if (mPreProgress >= 0) {
seekBar.setProgress(mPreProgress);
}
super.onStopTrackingTouch(seekBar);
mIsFromUser = false;
mPreviewLayout.setVisibility(GONE);
} else {
super.onStopTrackingTouch(seekBar);
}
}
@Override
protected void setTextAndProgress(int secProgress) {
if (mIsFromUser) {
return;
}
super.setTextAndProgress(secProgress);
}
@Override
public GSYBaseVideoPlayer startWindowFullscreen(Context context, boolean actionBar, boolean statusBar) {
GSYBaseVideoPlayer gsyBaseVideoPlayer = super.startWindowFullscreen(context, actionBar, statusBar);
PreViewGSYVideoPlayer customGSYVideoPlayer = (PreViewGSYVideoPlayer) gsyBaseVideoPlayer;
customGSYVideoPlayer.mOpenPreView = mOpenPreView;
return gsyBaseVideoPlayer;
}
@Override
public void onPrepared() {
super.onPrepared();
startDownFrame(mOriginUrl);
}
public boolean isOpenPreView() {
return mOpenPreView;
}
/**
* 如果是需要进度条预览的设置打开,默认关闭
*/
public void setOpenPreView(boolean localFile) {
this.mOpenPreView = localFile;
}
private void showPreView(String url, long time) {
int width = CommonUtil.dip2px(getContext(), 150);
int height = CommonUtil.dip2px(getContext(), 100);
Glide.with(getContext().getApplicationContext())
.setDefaultRequestOptions(
new RequestOptions()
//这里限制了只从缓存读取
.onlyRetrieveFromCache(true)
.frame(1000 * time)
.override(width, height)
.dontAnimate()
.centerCrop())
.load(url)
.into(mPreView);
}
private void startDownFrame(String url) {
for (int i = 1; i <= 100; i++) {
int time = i * getDuration() / 100;
int width = CommonUtil.dip2px(getContext(), 150);
int height = CommonUtil.dip2px(getContext(), 100);
Glide.with(getContext().getApplicationContext())
.setDefaultRequestOptions(
new RequestOptions()
.frame(1000 * time)
.override(width, height)
.centerCrop())
.load(url).preload(width, height);
}
}
}
......@@ -30,7 +30,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.shuyu.gsyvideoplayer.video.NormalGSYVideoPlayer
<com.example.gsyvideoplayer.video.PreViewGSYVideoPlayer
android:id="@+id/web_player"
android:layout_width="match_parent"
android:layout_height="200dp" />
......
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black">
<RelativeLayout
android:id="@+id/surface_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
</RelativeLayout>
<RelativeLayout
android:id="@+id/thumb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#000000"
android:scaleType="fitCenter" />
<LinearLayout
android:id="@+id/layout_bottom"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:background="#99000000"
android:gravity="center_vertical"
android:orientation="horizontal"
android:visibility="invisible">
<TextView
android:id="@+id/current"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:text="00:00"
android:textColor="#ffffff" />
<SeekBar
android:id="@+id/progress"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1.0"
android:background="@null"
android:max="100"
android:maxHeight="4dp"
android:minHeight="4dp"
android:paddingBottom="8dp"
android:paddingTop="8dp"
android:progressDrawable="@drawable/video_seek_progress"
android:thumb="@drawable/video_seek_thumb" />
<TextView
android:id="@+id/total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:text="00:00"
android:textColor="#ffffff" />
<ImageView
android:id="@+id/fullscreen"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:paddingRight="16dp"
android:scaleType="center"
android:src="@drawable/video_enlarge" />
</LinearLayout>
<ProgressBar
android:id="@+id/bottom_progressbar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="1.5dp"
android:layout_alignParentBottom="true"
android:max="100"
android:progressDrawable="@drawable/video_progress" />
<ImageView
android:id="@+id/back_tiny"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginLeft="6dp"
android:layout_marginTop="6dp"
android:visibility="gone" />
<ProgressBar
android:id="@+id/loading"
style="?android:attr/progressBarStyleLarge"
android:layout_width="28dp"
android:layout_height="28dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:visibility="invisible" />
<ImageView
android:id="@+id/start"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:src="@drawable/video_click_play_selector" />
<ImageView
android:id="@+id/small_close"
android:layout_width="30dp"
android:layout_height="30dp"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:scaleType="centerInside"
android:src="@drawable/video_small_close"
android:visibility="gone" />
<ImageView
android:id="@+id/lock_screen"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="50dp"
android:scaleType="centerInside"
android:src="@drawable/unlock"
android:visibility="gone" />
<LinearLayout
android:id="@+id/layout_top"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/back_tiny"
android:layout_toRightOf="@+id/back_tiny"
android:background="@drawable/video_title_bg"
android:gravity="center_vertical">
<ImageView
android:id="@+id/back"
android:layout_width="48dp"
android:layout_height="48dp"
android:paddingLeft="10dp"
android:scaleType="centerInside"
android:src="@drawable/video_back" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="10dp"
android:textColor="@android:color/white"
android:textSize="18sp" />
</LinearLayout>
<RelativeLayout
android:id="@+id/preview_layout"
android:layout_width="@dimen/seek_bar_image"
android:layout_height="100dp"
android:layout_above="@+id/layout_bottom"
android:layout_alignParentBottom="true"
android:layout_marginBottom="30dp"
android:visibility="gone">
<ImageView
android:id="@+id/preview_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000" />
</RelativeLayout>
</RelativeLayout>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册