Make sure view is laid out before calling createBitmap
This CL contains two fixes: - Make sure view is laid out before calling createBitmap. - Organize some field declarations. Bug: 246412041 Test: manual Change-Id: Icd5637086b3935afe89743472e68bbd3a6102222
This commit is contained in:
@@ -34,16 +34,16 @@ public class QrDecorateView extends View {
|
||||
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;
|
||||
private final int mCornerColor;
|
||||
private final int mFocusedCornerColor;
|
||||
private final int mBackgroundColor;
|
||||
|
||||
final private Paint mStrokePaint;
|
||||
final private Paint mTransparentPaint;
|
||||
final private Paint mBackgroundPaint;
|
||||
private final Paint mStrokePaint;
|
||||
private final Paint mTransparentPaint;
|
||||
private final Paint mBackgroundPaint;
|
||||
|
||||
final private float mRadius;
|
||||
final private float mInnerRidus;
|
||||
private final float mRadius;
|
||||
private final float mInnerRadius;
|
||||
|
||||
private Bitmap mMaskBitmap;
|
||||
private Canvas mMaskCanvas;
|
||||
@@ -72,7 +72,7 @@ public class QrDecorateView extends View {
|
||||
mRadius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, CORNER_RADIUS,
|
||||
getResources().getDisplayMetrics());
|
||||
// Inner radius needs to minus stroke width for keeping the width of border consistent.
|
||||
mInnerRidus = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
|
||||
mInnerRadius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
|
||||
CORNER_RADIUS - CORNER_STROKE_WIDTH, getResources().getDisplayMetrics());
|
||||
|
||||
mCornerColor = context.getResources().getColor(R.color.qr_corner_line_color);
|
||||
@@ -95,7 +95,10 @@ public class QrDecorateView extends View {
|
||||
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
|
||||
super.onLayout(changed, left, top, right, bottom);
|
||||
|
||||
if(mMaskBitmap == null) {
|
||||
if (!isLaidOut()) {
|
||||
return;
|
||||
}
|
||||
if (mMaskBitmap == null) {
|
||||
mMaskBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
|
||||
mMaskCanvas = new Canvas(mMaskBitmap);
|
||||
}
|
||||
@@ -105,16 +108,18 @@ public class QrDecorateView extends View {
|
||||
|
||||
@Override
|
||||
protected void onDraw(Canvas 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, mInnerRidus, mInnerRidus, mTransparentPaint);
|
||||
if (mMaskCanvas != null && mMaskBitmap != null) {
|
||||
// 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, mInnerRadius, mInnerRadius, mTransparentPaint);
|
||||
|
||||
canvas.drawBitmap(mMaskBitmap, 0, 0, mBackgroundPaint);
|
||||
canvas.drawBitmap(mMaskBitmap, 0, 0, mBackgroundPaint);
|
||||
}
|
||||
super.onDraw(canvas);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user