From 6ba661a574be4f80f1f13af36d1a398e4886f0e5 Mon Sep 17 00:00:00 2001 From: xufulong <839789740@qq.com> Date: Sun, 17 Nov 2019 23:45:06 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=86=E9=A2=91=E9=A2=84=E8=A7=88=E7=94=BB?= =?UTF-8?q?=E9=9D=A2=E8=B7=9F=E9=9A=8F=E8=BF=9B=E5=BA=A6=E6=9D=A1=E7=A7=BB?= =?UTF-8?q?=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 视频预览画面跟随进度条移动 --- .../ffmpeg/activity/VideoPreviewActivity.java | 4 +- .../frank/ffmpeg/view/VideoPreviewBar.java | 40 ++++++++++++++++++- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/frank/ffmpeg/activity/VideoPreviewActivity.java b/app/src/main/java/com/frank/ffmpeg/activity/VideoPreviewActivity.java index 331ad62..8203db3 100644 --- a/app/src/main/java/com/frank/ffmpeg/activity/VideoPreviewActivity.java +++ b/app/src/main/java/com/frank/ffmpeg/activity/VideoPreviewActivity.java @@ -97,7 +97,7 @@ public class VideoPreviewActivity extends BaseActivity implements VideoPreviewBa mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { - Log.e(TAG, "onPrepared..."); + Log.i(TAG, "onPrepared..."); mediaPlayer.start(); mHandler.sendEmptyMessage(MSG_UPDATE); } @@ -133,7 +133,7 @@ public class VideoPreviewActivity extends BaseActivity implements VideoPreviewBa @Override public void onStopTracking(long progress) { if (mediaPlayer != null) { - Log.e(TAG, "onStopTracking progress=" + progress); + Log.i(TAG, "onStopTracking progress=" + progress); mediaPlayer.seekTo((int) progress); } } diff --git a/app/src/main/java/com/frank/ffmpeg/view/VideoPreviewBar.java b/app/src/main/java/com/frank/ffmpeg/view/VideoPreviewBar.java index bdcdc4a..4b6ffb2 100644 --- a/app/src/main/java/com/frank/ffmpeg/view/VideoPreviewBar.java +++ b/app/src/main/java/com/frank/ffmpeg/view/VideoPreviewBar.java @@ -15,6 +15,7 @@ import android.widget.TextView; import com.frank.ffmpeg.R; import com.frank.ffmpeg.hardware.HardwareDecode; +import com.frank.ffmpeg.util.ScreenUtil; import com.frank.ffmpeg.util.TimeUtil; /** @@ -40,6 +41,14 @@ public class VideoPreviewBar extends RelativeLayout implements HardwareDecode.On private TextView txtVideoDuration; + private boolean isShowing; + + private int screenWidth; + + private int moveEndPos = 0; + + private int previewHalfWidth; + public VideoPreviewBar(Context context) { super(context); initView(context); @@ -57,6 +66,23 @@ public class VideoPreviewBar extends RelativeLayout implements HardwareDecode.On txtVideoProgress = view.findViewById(R.id.txt_video_progress); txtVideoDuration = view.findViewById(R.id.txt_video_duration); setListener(); + screenWidth = ScreenUtil.getScreenWidth(context); + } + + @Override + protected void onLayout(boolean changed, int l, int t, int r, int b) { + super.onLayout(changed, l, t, r, b); + if (moveEndPos == 0) { + int previewWidth = texturePreView.getWidth(); + previewHalfWidth = previewWidth / 2; + int marginEnd = 0; + MarginLayoutParams layoutParams = (MarginLayoutParams) texturePreView.getLayoutParams(); + if (layoutParams != null) { + marginEnd = layoutParams.getMarginEnd(); + } + moveEndPos = screenWidth - previewWidth - marginEnd; + Log.i(TAG, "previewWidth=" + previewWidth); + } } private void setPreviewCallback(final String filePath, TextureView texturePreView) { @@ -104,15 +130,26 @@ public class VideoPreviewBar extends RelativeLayout implements HardwareDecode.On // us to ms hardwareDecode.seekTo(progress * 1000); } + int percent = progress * screenWidth / duration; + if (percent > previewHalfWidth && percent < moveEndPos && texturePreView != null) { + texturePreView.setTranslationX(percent - previewHalfWidth); + } } @Override public void onStartTrackingTouch(SeekBar seekBar) { - + if (!isShowing && texturePreView != null) { + isShowing = true; + texturePreView.setVisibility(VISIBLE); + } } @Override public void onStopTrackingTouch(SeekBar seekBar) { + if (isShowing && texturePreView != null) { + isShowing = false; + texturePreView.setVisibility(GONE); + } if (mPreviewBarCallback != null) { mPreviewBarCallback.onStopTracking(seekBar.getProgress()); } @@ -131,6 +168,7 @@ public class VideoPreviewBar extends RelativeLayout implements HardwareDecode.On public void run() { previewBar.setMax(durationMs); txtVideoDuration.setText(TimeUtil.getVideoTime(durationMs)); + texturePreView.setVisibility(GONE); } }); }