Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for issue #35: OutOfMemoryError in MaterialShowcaseView.onDraw #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
Expand Down Expand Up @@ -41,10 +40,6 @@
*/
public class MaterialShowcaseView extends FrameLayout implements View.OnTouchListener, View.OnClickListener {

private int mOldHeight;
private int mOldWidth;
private Bitmap mBitmap;// = new WeakReference<>(null);
private Canvas mCanvas;
private Paint mEraser;
private Target mTarget;
private Shape mShape;
Expand All @@ -67,7 +62,6 @@ public class MaterialShowcaseView extends FrameLayout implements View.OnTouchLis
private long mFadeDurationInMillis = ShowcaseConfig.DEFAULT_FADE_TIME;
private Handler mHandler;
private long mDelayInMillis = ShowcaseConfig.DEFAULT_DELAY;
private int mBottomMargin = 0;
private boolean mSingleUse = false; // should display only once
private PrefsManager mPrefsManager; // used to store state doe single use mode
List<IShowcaseListener> mListeners; // external listeners who want to observe when we show and dismiss
Expand All @@ -76,27 +70,27 @@ public class MaterialShowcaseView extends FrameLayout implements View.OnTouchLis

public MaterialShowcaseView(Context context) {
super(context);
init(context);
init();
}

public MaterialShowcaseView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
init();
}

public MaterialShowcaseView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
init();
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public MaterialShowcaseView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context);
init();
}


private void init(Context context) {
private void init() {
setWillNotDraw(false);

// create our animation factory
Expand All @@ -114,12 +108,18 @@ private void init(Context context) {
mMaskColour = Color.parseColor(ShowcaseConfig.DEFAULT_MASK_COLOUR);
setVisibility(INVISIBLE);

// prepare eraser paint
mEraser = new Paint();
mEraser.setColor(0xFFFFFFFF);
mEraser.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
mEraser.setFlags(Paint.ANTI_ALIAS_FLAG);

View contentView = LayoutInflater.from(getContext()).inflate(R.layout.showcase_content, this, true);
mContentBox = contentView.findViewById(R.id.content_box);
mContentTextView = (TextView) contentView.findViewById(R.id.tv_content);
mDismissButton = (TextView) contentView.findViewById(R.id.tv_dismiss);
mDismissButton.setOnClickListener(this);
setLayerType(LAYER_TYPE_SOFTWARE, null);
}


Expand All @@ -137,44 +137,14 @@ protected void onDraw(Canvas canvas) {
// don't bother drawing if we're not ready
if (!mShouldRender) return;

// get current dimensions
int width = getMeasuredWidth();
int height = getMeasuredHeight();

// build a new canvas if needed i.e first pass or new dimensions
if (mBitmap == null || mCanvas == null || mOldHeight != height || mOldWidth != width) {

if (mBitmap != null) mBitmap.recycle();

mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);


mCanvas = new Canvas(mBitmap);
}

// save our 'old' dimensions
mOldWidth = width;
mOldHeight = height;

// clear canvas
mCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);

// draw solid background
mCanvas.drawColor(mMaskColour);

// Prepare eraser Paint if needed
if (mEraser == null) {
mEraser = new Paint();
mEraser.setColor(0xFFFFFFFF);
mEraser.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
mEraser.setFlags(Paint.ANTI_ALIAS_FLAG);
}
canvas.drawColor(mMaskColour);

// draw (erase) shape
mShape.draw(mCanvas, mEraser, mXPosition, mYPosition, mShapePadding);

// Draw the bitmap on our views canvas.
canvas.drawBitmap(mBitmap, 0, 0, null);
mShape.draw(canvas, mEraser, mXPosition, mYPosition, mShapePadding);
}

@Override
Expand Down Expand Up @@ -257,11 +227,11 @@ public void setTarget(Target target) {
* If we're on lollipop then make sure we don't draw over the nav bar
*/
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mBottomMargin = getSoftButtonsBarSizePort((Activity) getContext());
int bottomMargin = getSoftButtonsBarSizePort((Activity) getContext());
FrameLayout.LayoutParams contentLP = (LayoutParams) getLayoutParams();

if (contentLP != null && contentLP.bottomMargin != mBottomMargin)
contentLP.bottomMargin = mBottomMargin;
if (contentLP != null && contentLP.bottomMargin != bottomMargin)
contentLP.bottomMargin = bottomMargin;
}

// apply the target position
Expand Down Expand Up @@ -617,14 +587,8 @@ public void removeFromWindow() {
((ViewGroup) getParent()).removeView(this);
}

if (mBitmap != null) {
mBitmap.recycle();
mBitmap = null;
}

mEraser = null;
mAnimationFactory = null;
mCanvas = null;
mHandler = null;

getViewTreeObserver().removeGlobalOnLayoutListener(mLayoutListener);
Expand Down