Skip to content
This repository was archived by the owner on Apr 24, 2018. It is now read-only.

Commit 790d91c

Browse files
committed
Merge pull request #320 from amlcurran/multi-showcase
Fix memory management issues
2 parents 4ff73d3 + 0e6a8b4 commit 790d91c

File tree

6 files changed

+85
-70
lines changed

6 files changed

+85
-70
lines changed

library/src/main/java/com/github/amlcurran/showcaseview/Calculator.java

Lines changed: 0 additions & 34 deletions
This file was deleted.

library/src/main/java/com/github/amlcurran/showcaseview/ShowcaseView.java

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import android.view.MotionEvent;
3535
import android.view.View;
3636
import android.view.ViewGroup;
37-
import android.view.ViewTreeObserver;
3837
import android.widget.Button;
3938
import android.widget.RelativeLayout;
4039

@@ -112,9 +111,6 @@ protected ShowcaseView(Context context, AttributeSet attrs, int defStyle, boolea
112111
showcaseAreaCalculator = new ShowcaseAreaCalculator();
113112
shotStateStore = new ShotStateStore(context);
114113

115-
getViewTreeObserver().addOnPreDrawListener(new CalculateTextOnPreDraw());
116-
getViewTreeObserver().addOnGlobalLayoutListener(new UpdateOnGlobalLayout());
117-
118114
// Get the attributes for the ShowcaseView
119115
final TypedArray styled = context.getTheme()
120116
.obtainStyledAttributes(attrs, R.styleable.ShowcaseView, R.attr.showcaseViewStyle,
@@ -173,6 +169,7 @@ void setShowcasePosition(int x, int y) {
173169
showcaseX = x - positionInWindow[0];
174170
showcaseY = y - positionInWindow[1];
175171
//init();
172+
recalculateText();
176173
invalidate();
177174
}
178175

@@ -212,7 +209,6 @@ private void updateBitmap() {
212209
bitmapBuffer.recycle();
213210
}
214211
bitmapBuffer = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(), Bitmap.Config.ARGB_8888);
215-
216212
}
217213
}
218214

@@ -310,7 +306,6 @@ protected void dispatchDraw(Canvas canvas) {
310306

311307
@Override
312308
public void hide() {
313-
clearBitmap();
314309
// If the type is set to one-shot, store that it has shot
315310
shotStateStore.storeShot();
316311
mEventListener.onShowcaseViewHide(this);
@@ -330,6 +325,7 @@ this, fadeOutMillis, new AnimationEndListener() {
330325
@Override
331326
public void onAnimationEnd() {
332327
setVisibility(View.GONE);
328+
clearBitmap();
333329
isShowing = false;
334330
mEventListener.onShowcaseViewDidHide(ShowcaseView.this);
335331
}
@@ -340,10 +336,17 @@ public void onAnimationEnd() {
340336
@Override
341337
public void show() {
342338
isShowing = true;
339+
if (canUpdateBitmap()) {
340+
updateBitmap();
341+
}
343342
mEventListener.onShowcaseViewShow(this);
344343
fadeInShowcase();
345344
}
346345

346+
private boolean canUpdateBitmap() {
347+
return getMeasuredHeight() > 0 && getMeasuredWidth() > 0;
348+
}
349+
347350
private void fadeInShowcase() {
348351
animationFactory.fadeInView(
349352
this, fadeInMillis,
@@ -805,25 +808,6 @@ private void tintButton(int showcaseColor, boolean tintButton) {
805808
}
806809
}
807810

808-
private class UpdateOnGlobalLayout implements ViewTreeObserver.OnGlobalLayoutListener {
809-
810-
@Override
811-
public void onGlobalLayout() {
812-
if (!shotStateStore.hasShot()) {
813-
updateBitmap();
814-
}
815-
}
816-
}
817-
818-
private class CalculateTextOnPreDraw implements ViewTreeObserver.OnPreDrawListener {
819-
820-
@Override
821-
public boolean onPreDraw() {
822-
recalculateText();
823-
return true;
824-
}
825-
}
826-
827811
private OnClickListener hideOnClickListener = new OnClickListener() {
828812
@Override
829813
public void onClick(View v) {

sample/src/main/AndroidManifest.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636

3737
<activity android:name=".animations.AnimationSampleActivity" />
3838

39+
<activity android:name=".MemoryManagementTesting" />
40+
3941
<activity android:name=".SingleShotActivity" />
4042

4143
<activity android:name=".CustomTextActivity" />

sample/src/main/java/com/github/amlcurran/showcaseview/sample/MemoryManagementTesting.java

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,43 @@
1919
import android.app.Activity;
2020
import android.os.Bundle;
2121

22-
public class MemoryManagementTesting extends Activity {
22+
import com.github.amlcurran.showcaseview.OnShowcaseEventListener;
23+
import com.github.amlcurran.showcaseview.ShowcaseView;
24+
import com.github.amlcurran.showcaseview.targets.ViewTarget;
25+
26+
public class MemoryManagementTesting extends Activity implements OnShowcaseEventListener {
27+
28+
int currentShowcase = 1;
2329

2430
@Override
2531
protected void onCreate(Bundle savedInstanceState) {
2632
super.onCreate(savedInstanceState);
2733
setContentView(R.layout.main);
34+
showcase();
35+
}
36+
37+
private void showcase() {
38+
new ShowcaseView.Builder(this)
39+
.withMaterialShowcase()
40+
.setContentText(String.format("Showing %1$d", currentShowcase))
41+
.setTarget(new ViewTarget(R.id.buttonBlocked, this))
42+
.setShowcaseEventListener(this)
43+
.build();
44+
}
45+
46+
@Override
47+
public void onShowcaseViewHide(ShowcaseView showcaseView) {
2848

29-
// ShowcaseViews showcaseViews = new ShowcaseViews(this);
30-
//
31-
// ShowcaseViews.ItemViewProperties properties = new ShowcaseViews.ItemViewProperties(
32-
// R.id.buttonBlocked, R.string.showcase_like_title, R.string.showcase_like_message
33-
// );
34-
//
35-
// showcaseViews.addView(properties).addView(properties).addView(properties).addView(properties)
36-
// .addView(properties).addView(properties).show();
49+
}
50+
51+
@Override
52+
public void onShowcaseViewDidHide(ShowcaseView showcaseView) {
53+
currentShowcase++;
54+
showcase();
55+
}
56+
57+
@Override
58+
public void onShowcaseViewShow(ShowcaseView showcaseView) {
3759

3860
}
3961
}

sample/src/main/java/com/github/amlcurran/showcaseview/sample/SampleActivity.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ public void onItemClick(AdapterView<?> adapterView, View view, int position, lon
140140
case 4:
141141
startActivity(new Intent(this, CustomShowcaseActivity.class));
142142
break;
143+
case 5:
144+
startActivity(new Intent(this, MemoryManagementTesting.class));
145+
break;
143146
}
144147
}
145148

@@ -150,15 +153,17 @@ private static class HardcodedListAdapter extends ArrayAdapter {
150153
R.string.title_animations,
151154
R.string.title_single_shot,
152155
R.string.custom_text,
153-
R.string.custom_showcase_title//, R.string.title_memory
156+
R.string.custom_showcase_title,
157+
R.string.title_memory
154158
};
155159

156160
private static final int[] SUMMARY_RES_IDS = new int[] {
157161
R.string.sum_action_items,
158162
R.string.sum_animations,
159163
R.string.sum_single_shot,
160164
R.string.custom_text_summary,
161-
R.string.custom_showcase_summary//, R.string.sum_memory
165+
R.string.custom_showcase_summary,
166+
R.string.sum_memory
162167
};
163168

164169
public HardcodedListAdapter(Context context) {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<!--
2+
~ Copyright 2014 Alex Curran
3+
~
4+
~ Licensed under the Apache License, Version 2.0 (the "License");
5+
~ you may not use this file except in compliance with the License.
6+
~ You may obtain a copy of the License at
7+
~
8+
~ http://www.apache.org/licenses/LICENSE-2.0
9+
~
10+
~ Unless required by applicable law or agreed to in writing, software
11+
~ distributed under the License is distributed on an "AS IS" BASIS,
12+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
~ See the License for the specific language governing permissions and
14+
~ limitations under the License.
15+
-->
16+
17+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
18+
android:layout_width="fill_parent"
19+
android:layout_height="fill_parent"
20+
android:orientation="vertical">
21+
22+
<android.support.v7.widget.Toolbar
23+
android:layout_width="match_parent"
24+
android:id="@+id/toolbar"
25+
android:layout_height="wrap_content"/>
26+
27+
<Button
28+
android:id="@+id/buttonBlocked"
29+
android:layout_width="wrap_content"
30+
android:layout_height="wrap_content"
31+
android:text="Button"
32+
android:minWidth="96dp"
33+
android:layout_margin="16dp"
34+
android:layout_gravity="center_horizontal" />
35+
36+
</LinearLayout>

0 commit comments

Comments
 (0)