From 148f965cafcf268d3920f3912401cb91d49444f0 Mon Sep 17 00:00:00 2001 From: Chris Broadfoot Date: Thu, 23 Oct 2014 22:42:44 +1100 Subject: [PATCH] Fix faulty setup of views in IconGenerator --- .../google/maps/android/ui/IconGenerator.java | 52 +++++-------------- 1 file changed, 14 insertions(+), 38 deletions(-) diff --git a/library/src/com/google/maps/android/ui/IconGenerator.java b/library/src/com/google/maps/android/ui/IconGenerator.java index 4a95f4d5f..13cd3361b 100644 --- a/library/src/com/google/maps/android/ui/IconGenerator.java +++ b/library/src/com/google/maps/android/ui/IconGenerator.java @@ -58,6 +58,10 @@ public class IconGenerator { public IconGenerator(Context context) { mContext = context; mBackground = new BubbleDrawable(mContext.getResources()); + mContainer = (ViewGroup) LayoutInflater.from(mContext).inflate(R.layout.text_bubble, null); + mRotationLayout = (RotationLayout) mContainer.getChildAt(0); + mContentView = mTextView = (TextView) mRotationLayout.findViewById(R.id.text); + setStyle(STYLE_DEFAULT); } /** @@ -66,8 +70,6 @@ public IconGenerator(Context context) { * @param text the text content to display inside the icon. */ public Bitmap makeIcon(String text) { - ensureViewsSetUp(); - if (mTextView != null) { mTextView.setText(text); } @@ -82,19 +84,17 @@ public Bitmap makeIcon(String text) { * applicable. */ public Bitmap makeIcon() { - ViewGroup container = getContainer(); - int measureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); - container.measure(measureSpec, measureSpec); + mContainer.measure(measureSpec, measureSpec); - int measuredWidth = container.getMeasuredWidth(); - int measuredHeight = container.getMeasuredHeight(); + int measuredWidth = mContainer.getMeasuredWidth(); + int measuredHeight = mContainer.getMeasuredHeight(); - container.layout(0, 0, measuredWidth, measuredHeight); + mContainer.layout(0, 0, measuredWidth, measuredHeight); if (mRotation == 1 || mRotation == 3) { - measuredHeight = container.getMeasuredWidth(); - measuredWidth = container.getMeasuredHeight(); + measuredHeight = mContainer.getMeasuredWidth(); + measuredWidth = mContainer.getMeasuredHeight(); } Bitmap r = Bitmap.createBitmap(measuredWidth, measuredHeight, Bitmap.Config.ARGB_8888); @@ -113,7 +113,7 @@ public Bitmap makeIcon() { canvas.translate(0, measuredHeight); canvas.rotate(270); } - container.draw(canvas); + mContainer.draw(canvas); return r; } @@ -124,7 +124,6 @@ public Bitmap makeIcon() { * #setTextAppearance} and {@link #makeIcon(String)} will operate upon that {@link TextView}. */ public void setContentView(View contentView) { - ensureViewsSetUp(); mRotationLayout.removeAllViews(); mRotationLayout.addView(contentView); mContentView = contentView; @@ -138,7 +137,6 @@ public void setContentView(View contentView) { * @param degrees the amount the contents should be rotated, as a multiple of 90 degrees. */ public void setContentRotation(int degrees) { - ensureViewsSetUp(); mRotationLayout.setViewRotation(degrees); } @@ -190,7 +188,6 @@ private float rotateAnchor(float u, float v) { * @param resid the identifier of the resource. */ public void setTextAppearance(Context context, int resid) { - ensureViewsSetUp(); if (mTextView != null) { mTextView.setTextAppearance(context, resid); } @@ -232,36 +229,16 @@ public void setColor(int color) { @SuppressWarnings("deprecation") // View#setBackgroundDrawable is compatible with pre-API level 16 (Jelly Bean). public void setBackground(Drawable background) { - getContainer().setBackgroundDrawable(background); + mContainer.setBackgroundDrawable(background); // Force setting of padding. // setBackgroundDrawable does not call setPadding if the background has 0 padding. if (background != null) { Rect rect = new Rect(); background.getPadding(rect); - getContainer().setPadding(rect.left, rect.top, rect.right, rect.bottom); + mContainer.setPadding(rect.left, rect.top, rect.right, rect.bottom); } else { - getContainer().setPadding(0, 0, 0, 0); - } - } - - /** - * Not thread safe. - */ - private ViewGroup getContainer() { - ensureViewsSetUp(); - return mContainer; - } - - /** - * Ensure views are ready. This allows us to lazily inflate the main layout. - */ - private void ensureViewsSetUp() { - if (mContainer == null) { - mContainer = (ViewGroup) LayoutInflater.from(mContext).inflate(R.layout.text_bubble, null); - mRotationLayout = (RotationLayout) mContainer.getChildAt(0); - mContentView = mTextView = (TextView) mRotationLayout.findViewById(R.id.text); - setStyle(STYLE_DEFAULT); + mContainer.setPadding(0, 0, 0, 0); } } @@ -275,7 +252,6 @@ private void ensureViewsSetUp() { * @param bottom the bottom padding in pixels. */ public void setContentPadding(int left, int top, int right, int bottom) { - ensureViewsSetUp(); mContentView.setPadding(left, top, right, bottom); }