Skip to content

Commit

Permalink
优化绘制性能
Browse files Browse the repository at this point in the history
  • Loading branch information
yuzhiqiang1993 committed Jun 11, 2018
1 parent a487cbd commit 196025a
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 75 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
File renamed without changes.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ buildscript {
min_sdk_version = 16
target_sdk_version = 27
constraint_version = '1.1.0'
version_code = 1_00_00
version_name = '1.0.0'// E.g 1.9.72 == 1,09,72
version_code = 100
version_name = '1.0.0'

/*support*/
android_support = '27.1.1'
Expand Down
1 change: 0 additions & 1 deletion zxinglibrary/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
android:name=".android.CaptureActivity"
android:configChanges="orientation|screenSize"
android:screenOrientation="portrait"
android:theme="@style/ZxingTheme"
android:windowSoftInputMode="adjustPan|stateHidden" />
</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import com.google.zxing.ResultPoint;
import com.yzq.zxinglibrary.R;
Expand All @@ -30,6 +31,7 @@ public final class ViewfinderView extends View {
private static final int CURRENT_POINT_OPACITY = 0xA0;
private static final int MAX_RESULT_POINTS = 20;
private static final int POINT_SIZE = 6;
private final View scanLineView;

private CameraManager cameraManager;
private final Paint paint;
Expand All @@ -41,13 +43,12 @@ public final class ViewfinderView extends View {
private int reactColor;//四个角的颜色



private List<ResultPoint> possibleResultPoints;
private List<ResultPoint> lastPossibleResultPoints;
// 扫描线移动的y
private int scanLineTop;
// 扫描线移动速度
private int SCAN_VELOCITY = 10;
private int SCAN_VELOCITY = 20;
//扫描线高度
private int scanLightHeight = 20;
// 扫描线
Expand Down Expand Up @@ -89,6 +90,10 @@ public ViewfinderView(Context context, @Nullable AttributeSet attrs, int defStyl
lastPossibleResultPoints = null;
scanLight = BitmapFactory.decodeResource(resources, R.drawable.scan_light);



scanLineView=new View(context);

}

public void setCameraManager(CameraManager cameraManager) {
Expand All @@ -112,18 +117,17 @@ public void onDraw(Canvas canvas) {
int height = canvas.getHeight();


// Draw the exterior (i.e. outside the framing rect) darkened
// 绘制取景框外的暗灰色的表面,分四个矩形绘制
paint.setColor(resultBitmap != null ? resultColor : maskColor);
/*上面的框*/
canvas.drawRect(0, 0, width, frame.top, paint);
/*绘制左边的框*/
canvas.drawRect(0, frame.top, frame.left, frame.bottom + 1, paint);
/*绘制右边的框*/
canvas.drawRect(frame.right + 1, frame.top, width, frame.bottom + 1,
paint);
/*绘制下面的框*/
canvas.drawRect(0, frame.bottom + 1, width, height, paint);
scanLineView.setLayoutParams(new ViewGroup.LayoutParams(frame.width(),10));
scanLineView.setBackgroundColor(reactColor);


/*绘制遮罩*/
drawMaskView(canvas, frame, width, height);

/*绘制取景框边框*/
drawFrameBounds(canvas, frame);



if (resultBitmap != null) {
// Draw the opaque result bitmap over the scanning rectangle
Expand All @@ -132,59 +136,77 @@ public void onDraw(Canvas canvas) {
canvas.drawBitmap(resultBitmap, null, frame, paint);
} else {

/*绘制取景框边框*/
drawFrameBounds(canvas, frame);

/*绘制提示文字*/
// drawStatusText(canvas, frame, width);

/*绘制扫描线*/
drawScanLight(canvas, frame);

float scaleX = frame.width() / (float) previewFrame.width();
float scaleY = frame.height() / (float) previewFrame.height();

// 绘制扫描线周围的特征点
List<ResultPoint> currentPossible = possibleResultPoints;
List<ResultPoint> currentLast = lastPossibleResultPoints;
int frameLeft = frame.left;
int frameTop = frame.top;
if (currentPossible.isEmpty()) {
lastPossibleResultPoints = null;
} else {
possibleResultPoints = new ArrayList<ResultPoint>(5);
lastPossibleResultPoints = currentPossible;
paint.setAlpha(CURRENT_POINT_OPACITY);
paint.setColor(resultPointColor);
synchronized (currentPossible) {
for (ResultPoint point : currentPossible) {
canvas.drawCircle(frameLeft
+ (int) (point.getX() * scaleX), frameTop
+ (int) (point.getY() * scaleY), POINT_SIZE,
paint);
}
// drawScanLight(canvas, frame);

/*绘制闪动的点*/
drawPoint(canvas, frame, previewFrame);
}
}

private void drawPoint(Canvas canvas, Rect frame, Rect previewFrame) {
float scaleX = frame.width() / (float) previewFrame.width();
float scaleY = frame.height() / (float) previewFrame.height();

// 绘制扫描线周围的特征点
List<ResultPoint> currentPossible = possibleResultPoints;
List<ResultPoint> currentLast = lastPossibleResultPoints;
int frameLeft = frame.left;
int frameTop = frame.top;
if (currentPossible.isEmpty()) {
lastPossibleResultPoints = null;
} else {
possibleResultPoints = new ArrayList<ResultPoint>(5);
lastPossibleResultPoints = currentPossible;
paint.setAlpha(CURRENT_POINT_OPACITY);
paint.setColor(resultPointColor);
synchronized (currentPossible) {
for (ResultPoint point : currentPossible) {
canvas.drawCircle(frameLeft
+ (int) (point.getX() * scaleX), frameTop
+ (int) (point.getY() * scaleY), POINT_SIZE,
paint);
}
}
if (currentLast != null) {
paint.setAlpha(CURRENT_POINT_OPACITY / 2);
paint.setColor(resultPointColor);
synchronized (currentLast) {
float radius = POINT_SIZE / 2.0f;
for (ResultPoint point : currentLast) {
canvas.drawCircle(frameLeft
+ (int) (point.getX() * scaleX), frameTop
+ (int) (point.getY() * scaleY), radius, paint);
}
}
if (currentLast != null) {
paint.setAlpha(CURRENT_POINT_OPACITY / 2);
paint.setColor(resultPointColor);
synchronized (currentLast) {
float radius = POINT_SIZE / 2.0f;
for (ResultPoint point : currentLast) {
canvas.drawCircle(frameLeft
+ (int) (point.getX() * scaleX), frameTop
+ (int) (point.getY() * scaleY), radius, paint);
}
}

// Request another update at the animation interval, but only
// repaint the laser line,
// not the entire viewfinder mask.
postInvalidateDelayed(ANIMATION_DELAY, frame.left - POINT_SIZE,
frame.top - POINT_SIZE, frame.right + POINT_SIZE,
frame.bottom + POINT_SIZE);
}

// Request another update at the animation interval, but only
// repaint the laser line,
// not the entire viewfinder mask.
postInvalidateDelayed(ANIMATION_DELAY, frame.left - POINT_SIZE,
frame.top - POINT_SIZE, frame.right + POINT_SIZE,
frame.bottom + POINT_SIZE);
}

private void drawMaskView(Canvas canvas, Rect frame, int width, int height) {
// Draw the exterior (i.e. outside the framing rect) darkened
// 绘制取景框外的暗灰色的表面,分四个矩形绘制
paint.setColor(resultBitmap != null ? resultColor : maskColor);
/*上面的框*/
canvas.drawRect(0, 0, width, frame.top, paint);
/*绘制左边的框*/
canvas.drawRect(0, frame.top, frame.left, frame.bottom + 1, paint);
/*绘制右边的框*/
canvas.drawRect(frame.right + 1, frame.top, width, frame.bottom + 1,
paint);
/*绘制下面的框*/
canvas.drawRect(0, frame.bottom + 1, width, height, paint);
}


Expand All @@ -196,21 +218,15 @@ public void onDraw(Canvas canvas) {
*/
private void drawFrameBounds(Canvas canvas, Rect frame) {



/*扫描框的边框线*/


if (config.getFrameLineColor() != -1) {
paint.setColor(ContextCompat.getColor(getContext(),config.getFrameLineColor()));
paint.setColor(ContextCompat.getColor(getContext(), config.getFrameLineColor()));
paint.setStrokeWidth(2);
paint.setStyle(Paint.Style.STROKE);
canvas.drawRect(frame, paint);

}



/*扫描框的四个角*/
paint.setColor(reactColor);
paint.setStyle(Paint.Style.FILL);
Expand All @@ -221,7 +237,6 @@ private void drawFrameBounds(Canvas canvas, Rect frame) {
int corLength = (int) (width * 0.07);
int corWidth = (int) (corLength * 0.2);


corWidth = corWidth > 15 ? 15 : corWidth;


Expand Down Expand Up @@ -303,14 +318,19 @@ private void drawScanLight(Canvas canvas, Rect frame) {
} else {

/*缓动动画*/
SCAN_VELOCITY = (frame.bottom - scanLineTop) / 12;
SCAN_VELOCITY = (int) (SCAN_VELOCITY > 10 ? Math.ceil(SCAN_VELOCITY) : 10);
// SCAN_VELOCITY = (frame.bottom - scanLineTop) / 12;
// SCAN_VELOCITY = (int) (SCAN_VELOCITY > 10 ? Math.ceil(SCAN_VELOCITY) : 10);
scanLineTop += SCAN_VELOCITY;
}

Rect scanRect = new Rect(frame.left, scanLineTop, frame.right,
scanLineTop + scanLightHeight);
canvas.drawBitmap(scanLight, null, scanRect, paint);
// Rect scanRect = new Rect(frame.left, scanLineTop, frame.right,
// scanLineTop + scanLightHeight);
// canvas.drawBitmap(scanLight, null, scanRect, paint);


paint.setColor(reactColor);
paint.setStrokeWidth(8);
canvas.drawLine(frame.left, scanLineTop, frame.right, scanLineTop, paint);


}
Expand Down
4 changes: 2 additions & 2 deletions zxinglibrary/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
<color name="possible_result_points">#ffbd21</color>
<color name="result_minor_text">#c0c0c0</color>
<color name="result_points">#99cc00</color>
<color name="result_text">#ffffff</color>
<color name="result_view">#000000</color>
<color name="result_text">#ffffffff</color>
<color name="result_view">#ff000000</color>
<color name="status_text">#ffffff</color>
<color name="viewfinder_mask">#50000000</color>
<color name="react">#63B8FF</color>
Expand Down

0 comments on commit 196025a

Please sign in to comment.