Skip to content

Commit

Permalink
扫描线动画的优化
Browse files Browse the repository at this point in the history
  • Loading branch information
maning0303 committed Nov 20, 2018
1 parent 435bc61 commit e16850d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.zxing.client.android;

import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
Expand All @@ -24,6 +25,7 @@
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.google.zxing.client.android.camera.CameraManager;
Expand All @@ -50,6 +52,10 @@ public final class ViewfinderView extends View {

private Context context;

private Rect frame;
private int margin;
private int lineW;

// This constructor is used when the class is built from an XML resource.
public ViewfinderView(Context context, AttributeSet attrs) {
super(context, attrs);
Expand All @@ -69,6 +75,9 @@ public ViewfinderView(Context context, AttributeSet attrs) {
paintText.setTextAlign(Paint.Align.CENTER);
//扫描线 + 四角
paintLine.setColor(laserColor);

margin = CommonUtils.dip2px(context, 4);
lineW = CommonUtils.dip2px(context, 2);
}

//设置颜色
Expand All @@ -92,7 +101,7 @@ public void onDraw(Canvas canvas) {
if (cameraManager == null) {
return; // not ready yet, early draw before done configuring
}
Rect frame = cameraManager.getFramingRect();
frame = cameraManager.getFramingRect();
Rect previewFrame = cameraManager.getFramingRectInPreview();
if (frame == null || previewFrame == null) {
return;
Expand Down Expand Up @@ -128,16 +137,8 @@ public void onDraw(Canvas canvas) {
canvas.drawRect(frame.right - rectH, frame.bottom - rectW, frame.right + 1, frame.bottom + 1, paintLine);

//中间的线:动画
int margin = CommonUtils.dip2px(context, 6);
int lineW = CommonUtils.dip2px(context, 2);
if (linePosition == 0) {
linePosition = frame.top + margin;
} else {
if (linePosition > frame.bottom - margin * 2) {
linePosition = frame.top + margin;
} else {
linePosition += 8;
}
}
canvas.drawRect(frame.left + margin, linePosition, frame.right - margin, linePosition + lineW, paintLine);

Expand All @@ -148,10 +149,33 @@ public void onDraw(Canvas canvas) {
frame.top,
frame.right,
frame.bottom);

startAnimation();
}


ValueAnimator anim;

public void startAnimation() {
if (anim != null && anim.isRunning()) {
return;
}
anim = ValueAnimator.ofInt(frame.top + margin, frame.bottom - margin);
anim.setRepeatCount(ValueAnimator.INFINITE);
anim.setRepeatMode(ValueAnimator.RESTART);
anim.setDuration(2500);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
linePosition = (int) animation.getAnimatedValue();
postInvalidate();
}
});
anim.start();
}

public void drawViewfinder() {
invalidate();
postInvalidate();
}

}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e16850d

Please sign in to comment.