Skip to content

Commit

Permalink
新增视频裁剪时的进度条显示
Browse files Browse the repository at this point in the history
  • Loading branch information
JessYanCoding committed Jul 30, 2020
1 parent eeb031f commit fe9af2d
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.luck.picture.lib.instagram.process;


import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Window;

import com.luck.picture.lib.R;

public class InstagramLoadingDialog extends Dialog {

private InstagramLoadingView mContentView;

public InstagramLoadingDialog(Context context) {
super(context, R.style.Picture_Theme_AlertDialog);
setCancelable(true);
setCanceledOnTouchOutside(false);
Window window = getWindow();
window.setWindowAnimations(R.style.PictureThemeDialogWindowStyle);
}


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContentView = new InstagramLoadingView(getContext());
setContentView(mContentView);
}

public void updateProgress(double progress) {
if (mContentView != null) {
mContentView.updateProgress(progress);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.luck.picture.lib.instagram.process;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.view.View;

import com.luck.picture.lib.R;
import com.luck.picture.lib.tools.ScreenUtils;

import androidx.core.content.ContextCompat;

/**
* ================================================
* Created by JessYan on 2020/7/30 10:35
* <a href="mailto:[email protected]">Contact me</a>
* <a href="https://github.com/JessYanCoding">Follow me</a>
* ================================================
*/
public class InstagramLoadingView extends View {
private Paint mPaint;
private RectF mBackRect;
private int mHeight;
private int mWidth;
private final int mProgressWidth;
private final RectF mProgressRect;
private float mProgress;

public InstagramLoadingView(Context context) {
super(context);
mWidth = ScreenUtils.dip2px(getContext(), 45);
mHeight = ScreenUtils.dip2px(getContext(), 45);
mProgressWidth = ScreenUtils.dip2px(getContext(), 25);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

mBackRect = new RectF(0, 0, mWidth, mHeight);

float progressLeft = (mWidth - mProgressWidth) / 2F;
float progressTop = (mHeight - mProgressWidth) / 2F;
mProgressRect = new RectF(progressLeft, progressTop, progressLeft + mProgressWidth, progressTop + mProgressWidth);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(mWidth, mHeight);
}

public void updateProgress(double progress) {
mProgress = Math.round(360 * progress);
invalidate();
}

@Override
protected void onDraw(Canvas canvas) {
mPaint.setStyle(Paint.Style.FILL);
mPaint.setColor(ContextCompat.getColor(getContext(), R.color.picture_color_a83));
canvas.drawRoundRect(mBackRect, ScreenUtils.dip2px(getContext(), 5), ScreenUtils.dip2px(getContext(), 5), mPaint);

mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(ScreenUtils.dip2px(getContext(), 2));
mPaint.setColor(0xFF444444);
canvas.drawCircle(getMeasuredWidth() / 2F, getMeasuredHeight() / 2F, mProgressWidth / 2F, mPaint);


mPaint.setColor(Color.WHITE);
canvas.drawArc(mProgressRect, -90, mProgress, false, mPaint);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import androidx.annotation.NonNull;
Expand Down Expand Up @@ -92,6 +93,8 @@ public class TrimContainer extends FrameLayout {
private LinearInterpolator mInterpolator;
private boolean isRangeChange = true;
private boolean mIsPreviewStart = true;
private InstagramLoadingDialog mLoadingDialog;
private Future<Void> mTranscodeFuture;

public TrimContainer(@NonNull Context context, PictureSelectionConfig config, LocalMedia media, VideoView videoView, VideoPauseListener videoPauseListener) {
super(context);
Expand Down Expand Up @@ -262,7 +265,7 @@ public long getEndTime() {
}

public void cropVideo(InstagramMediaProcessActivity activity, boolean isAspectRatio) {
activity.showLoadingView(true);
showLoadingView(true);
long startTime = getStartTime();
long endTime = getEndTime();

Expand Down Expand Up @@ -313,10 +316,13 @@ public void cropVideo(InstagramMediaProcessActivity activity, boolean isAspectRa
} else {
builder.addDataSource(new ClipDataSource(new FilePathDataSource(mMedia.getPath()), startTimeUS, endTimeUS));
}
builder.setListener(new TranscoderListener() {
mTranscodeFuture = builder.setListener(new TranscoderListener() {
@Override
public void onTranscodeProgress(double progress) {

if (mLoadingDialog != null
&& mLoadingDialog.isShowing()) {
mLoadingDialog.updateProgress(progress);
}
}

@Override
Expand All @@ -332,25 +338,48 @@ public void onTranscodeCompleted(int successCode) {
} else if (successCode == Transcoder.SUCCESS_NOT_NEEDED) {

}
activity.showLoadingView(false);
showLoadingView(false);
}

@Override
public void onTranscodeCanceled() {
activity.showLoadingView(false);
showLoadingView(false);
}

@Override
public void onTranscodeFailed(@NonNull Throwable exception) {
exception.printStackTrace();
ToastUtils.s(getContext(), getContext().getString(R.string.video_clip_failed));
activity.showLoadingView(false);
showLoadingView(false);
}
})
.setVideoTrackStrategy(videoStrategy)
.transcode();
}

private void showLoadingView(boolean isShow) {
if (isShow) {
if (mLoadingDialog == null) {
mLoadingDialog = new InstagramLoadingDialog(getContext());
mLoadingDialog.setOnCancelListener(dialog -> {
if (mTranscodeFuture != null) {
mTranscodeFuture.cancel(true);
}
});
}
if (mLoadingDialog.isShowing()) {
mLoadingDialog.dismiss();
}
mLoadingDialog.updateProgress(0);
mLoadingDialog.show();
} else {
if (mLoadingDialog != null
&& mLoadingDialog.isShowing()) {
mLoadingDialog.dismiss();
}
}
}

public void trimVideo(InstagramMediaProcessActivity activity, CountDownLatch count) {
activity.showLoadingView(true);

Expand Down

0 comments on commit fe9af2d

Please sign in to comment.