From 0b0dec957d3f7336c634aca9237fa227b8c4c214 Mon Sep 17 00:00:00 2001 From: Johnson Lu Date: Mon, 24 Dec 2018 17:29:06 +0800 Subject: [PATCH] Refine decorate view of QR Code scanner 1.Draw rounded corner line. 2.Paint 70% white transparency background. Bug: 118797380 Test: make RunSettingsRoboTests Change-Id: I1192e32cf4ec801a3efcad4287c2e547deeac3bf --- color-check-baseline.xml | 114 +++++++++------ res/values/colors.xml | 7 +- .../settings/wifi/qrcode/QrDecorateView.java | 136 +++++++++--------- 3 files changed, 144 insertions(+), 113 deletions(-) diff --git a/color-check-baseline.xml b/color-check-baseline.xml index 787c559e4ac..fd0fc6ceeec 100644 --- a/color-check-baseline.xml +++ b/color-check-baseline.xml @@ -1,18 +1,6 @@ - - - - + + + + + + + + + + + + @@ -2473,7 +2509,7 @@ errorLine2=" ^"> @@ -2489,7 +2525,7 @@ errorLine2=" ^"> @@ -2505,7 +2541,7 @@ errorLine2=" ^"> @@ -2521,23 +2557,7 @@ errorLine2=" ^"> - - - - @@ -2553,7 +2573,7 @@ errorLine2=" ^"> @@ -2569,7 +2589,7 @@ errorLine2=" ^"> @@ -2585,7 +2605,7 @@ errorLine2=" ^"> @@ -2601,7 +2621,7 @@ errorLine2=" ^"> @@ -2617,7 +2637,7 @@ errorLine2=" ^"> @@ -2633,7 +2653,7 @@ errorLine2=" ^"> @@ -2649,7 +2669,7 @@ errorLine2=" ^"> @@ -2665,7 +2685,7 @@ errorLine2=" ^"> @@ -2681,7 +2701,7 @@ errorLine2=" ^"> @@ -2697,7 +2717,7 @@ errorLine2=" ^"> @@ -2713,7 +2733,7 @@ errorLine2=" ^"> @@ -2729,7 +2749,7 @@ errorLine2=" ^"> @@ -2745,7 +2765,7 @@ errorLine2=" ^"> @@ -2761,7 +2781,7 @@ errorLine2=" ^"> diff --git a/res/values/colors.xml b/res/values/colors.xml index 587184b2c50..2103649851d 100644 --- a/res/values/colors.xml +++ b/res/values/colors.xml @@ -128,5 +128,10 @@ @*android:color/accent_device_default_light - + + #ffdadce0 + #ff1a73e8 + #b3ffffff + + \ No newline at end of file diff --git a/src/com/android/settings/wifi/qrcode/QrDecorateView.java b/src/com/android/settings/wifi/qrcode/QrDecorateView.java index 253bdb8973b..6952a63fafd 100644 --- a/src/com/android/settings/wifi/qrcode/QrDecorateView.java +++ b/src/com/android/settings/wifi/qrcode/QrDecorateView.java @@ -17,10 +17,13 @@ package com.android.settings.wifi.qrcode; import android.content.Context; +import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Point; +import android.graphics.PorterDuff; +import android.graphics.PorterDuffXfermode; import android.graphics.Rect; import android.graphics.RectF; import android.util.AttributeSet; @@ -33,11 +36,26 @@ import com.android.settings.R; * Draws the lines at the corner of the inner frame. */ public class QrDecorateView extends View { - private static final float CORNER_STROKE_WIDTH = 3f; // 3dp - private static final float CORNER_LINE_LENGTH = 20f; // 20dp + private static final float CORNER_STROKE_WIDTH = 4f; // 4dp + private static final float CORNER_LINE_LENGTH = 264f; // 264dp + private static final float CORNER_RADIUS = 16f; // 16dp + + final private int mCornerColor; + final private int mFocusedCornerColor; + final private int mBackgroundColor; + + final private Paint mStrokePaint; + final private Paint mTransparentPaint; + final private Paint mBackgroundPaint; + + final private float mRadius; + + private Bitmap mMaskBitmap; + private Canvas mMaskCanvas; + + private RectF mOuterFrame; + private RectF mInnerFrame; - final private Paint mPaint; - private RectF mFrame; private boolean mFocused; public QrDecorateView(Context context) { @@ -54,78 +72,66 @@ public class QrDecorateView extends View { public QrDecorateView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); - final float strokeWidth = TypedValue.applyDimension( - TypedValue.COMPLEX_UNIT_DIP, - CORNER_STROKE_WIDTH, - getResources().getDisplayMetrics() - ); - mPaint = new Paint(); - mPaint.setStrokeWidth(strokeWidth); + mFocused = false; + mRadius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, CORNER_RADIUS, + getResources().getDisplayMetrics()); + + mCornerColor = context.getResources().getColor(R.color.qr_corner_line_color); + mFocusedCornerColor = context.getResources().getColor(R.color.qr_focused_corner_line_color); + mBackgroundColor = context.getResources().getColor(R.color.qr_background_color); + + mStrokePaint = new Paint(); + mStrokePaint.setAntiAlias(true); + + mTransparentPaint = new Paint(); + mTransparentPaint.setAntiAlias(true); + mTransparentPaint.setColor(getResources().getColor(android.R.color.transparent)); + mTransparentPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); + + mBackgroundPaint = new Paint(); + mBackgroundPaint.setColor(mBackgroundColor); + } + + @Override + protected void onLayout(boolean changed, int left, int top, int right, int bottom) { + super.onLayout(changed, left, top, right, bottom); + + if(mMaskBitmap == null) { + mMaskBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888); + mMaskCanvas = new Canvas(mMaskBitmap); + } + + calculateFramePos(); } @Override protected void onDraw(Canvas canvas) { - calculateFramePos(); - final float cornerLineLength = TypedValue.applyDimension( - TypedValue.COMPLEX_UNIT_DIP, - CORNER_LINE_LENGTH, - getResources().getDisplayMetrics() - ); - mPaint.setColor(mFocused ? Color.GREEN : Color.WHITE); - drawCorner(mFrame, cornerLineLength, canvas); - super.onDraw(canvas); - } + // Set frame line color. + mStrokePaint.setColor(mFocused ? mFocusedCornerColor : mCornerColor); + // Draw background color. + mMaskCanvas.drawColor(mBackgroundColor); + // Draw outer corner. + mMaskCanvas.drawRoundRect(mOuterFrame, mRadius, mRadius, mStrokePaint); + // Draw inner transparent corner. + mMaskCanvas.drawRoundRect(mInnerFrame, mRadius, mRadius, mTransparentPaint); - private void drawCorner(RectF frame, float lineLength, Canvas canvas) { - final float strokeWidth = TypedValue.applyDimension( - TypedValue.COMPLEX_UNIT_DIP, - CORNER_STROKE_WIDTH, - getResources().getDisplayMetrics() - ); - // Draw top-left corner. - canvas.drawLine( - frame.left - strokeWidth / 2, - frame.top, - frame.left + lineLength, - frame.top, - mPaint); - canvas.drawLine(frame.left, frame.top, frame.left, frame.top + lineLength, mPaint); - // Draw top-right corner. - canvas.drawLine( - frame.right + strokeWidth / 2, - frame.top, - frame.right - lineLength, - frame.top, - mPaint); - canvas.drawLine(frame.right, frame.top, frame.right, frame.top + lineLength, mPaint); - // Draw bottom-left corner. - canvas.drawLine( - frame.left - strokeWidth / 2, - frame.bottom, - frame.left + lineLength, - frame.bottom, - mPaint); - canvas.drawLine(frame.left, frame.bottom, frame.left, frame.bottom - lineLength, mPaint); - // Draw bottom-right corner. - canvas.drawLine( - frame.right + strokeWidth / 2, - frame.bottom, - frame.right - lineLength, - frame.bottom, - mPaint); - canvas.drawLine(frame.right, frame.bottom, frame.right, frame.bottom - lineLength, mPaint); + canvas.drawBitmap(mMaskBitmap, 0, 0, mBackgroundPaint); + super.onDraw(canvas); } private void calculateFramePos() { final int centralX = getWidth() / 2; final int centralY = getHeight() / 2; - final float halfFrameWidth = getWidth() / 3; - mFrame = new RectF( - centralX - halfFrameWidth, - centralY - halfFrameWidth, - centralX + halfFrameWidth, - centralY + halfFrameWidth); + final float cornerLineLength = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, + CORNER_LINE_LENGTH, getResources().getDisplayMetrics()) / 2; + final float strokeWidth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, + CORNER_STROKE_WIDTH, getResources().getDisplayMetrics()) / 2; + + mOuterFrame = new RectF(centralX - cornerLineLength, centralY - cornerLineLength, + centralX + cornerLineLength, centralY + cornerLineLength); + mInnerFrame = new RectF(mOuterFrame.left + strokeWidth, mOuterFrame.top + strokeWidth, + mOuterFrame.right - strokeWidth, mOuterFrame.bottom - strokeWidth); } // Draws green lines if focued. Otherwise, draws white lines.