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

Commit f0cb303

Browse files
committed
Merge pull request #306 from amlcurran/custom-showcase-drawer
Custom + material showcase drawer API
2 parents c013ab8 + 47cf440 commit f0cb303

File tree

16 files changed

+280
-24
lines changed

16 files changed

+280
-24
lines changed

library/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ buildscript {
2020
}
2121

2222
dependencies {
23-
classpath 'com.android.tools.build:gradle:1.0.0'
23+
classpath 'com.android.tools.build:gradle:1.3.0'
2424
}
2525
}
2626

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.github.amlcurran.showcaseview;
2+
3+
import android.content.res.Resources;
4+
import android.graphics.Bitmap;
5+
import android.graphics.Canvas;
6+
import android.graphics.Paint;
7+
import android.graphics.PorterDuff;
8+
import android.graphics.PorterDuffXfermode;
9+
10+
public class MaterialShowcaseDrawer implements ShowcaseDrawer {
11+
12+
private final float radius;
13+
private final Paint basicPaint;
14+
private final Paint eraserPaint;
15+
private int backgroundColor;
16+
17+
public MaterialShowcaseDrawer(Resources resources) {
18+
this.radius = resources.getDimension(R.dimen.showcase_radius_material);
19+
this.eraserPaint = new Paint();
20+
this.eraserPaint.setColor(0xFFFFFF);
21+
this.eraserPaint.setAlpha(0);
22+
this.eraserPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));
23+
this.eraserPaint.setAntiAlias(true);
24+
this.basicPaint = new Paint();
25+
}
26+
27+
@Override
28+
public void setShowcaseColour(int color) {
29+
// no-op
30+
}
31+
32+
@Override
33+
public void drawShowcase(Bitmap buffer, float x, float y, float scaleMultiplier) {
34+
Canvas bufferCanvas = new Canvas(buffer);
35+
bufferCanvas.drawCircle(x, y, radius, eraserPaint);
36+
}
37+
38+
@Override
39+
public int getShowcaseWidth() {
40+
return (int) (radius * 2);
41+
}
42+
43+
@Override
44+
public int getShowcaseHeight() {
45+
return (int) (radius * 2);
46+
}
47+
48+
@Override
49+
public float getBlockedRadius() {
50+
return radius;
51+
}
52+
53+
@Override
54+
public void setBackgroundColour(int backgroundColor) {
55+
this.backgroundColor = backgroundColor;
56+
}
57+
58+
@Override
59+
public void erase(Bitmap bitmapBuffer) {
60+
bitmapBuffer.eraseColor(backgroundColor);
61+
}
62+
63+
@Override
64+
public void drawToCanvas(Canvas canvas, Bitmap bitmapBuffer) {
65+
canvas.drawBitmap(bitmapBuffer, 0, 0, basicPaint);
66+
}
67+
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@
1919
import android.graphics.Bitmap;
2020
import android.graphics.Canvas;
2121

22-
/**
23-
* Created by curraa01 on 13/10/2013.
24-
*/
25-
interface ShowcaseDrawer {
22+
public interface ShowcaseDrawer {
2623

2724
void setShowcaseColour(int color);
2825

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

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public class ShowcaseView extends RelativeLayout
5050

5151
private final Button mEndButton;
5252
private final TextDrawer textDrawer;
53-
private final ShowcaseDrawer showcaseDrawer;
53+
private ShowcaseDrawer showcaseDrawer;
5454
private final ShowcaseAreaCalculator showcaseAreaCalculator;
5555
private final AnimationFactory animationFactory;
5656
private final ShotStateStore shotStateStore;
@@ -75,6 +75,8 @@ public class ShowcaseView extends RelativeLayout
7575
private long fadeInMillis;
7676
private long fadeOutMillis;
7777
private boolean isShowing;
78+
private int backgroundColor;
79+
private int showcaseColor;
7880

7981
protected ShowcaseView(Context context, boolean newStyle) {
8082
this(context, null, R.styleable.CustomTheme_showcaseViewStyle, newStyle);
@@ -392,6 +394,12 @@ public Builder(Activity activity) {
392394
this(activity, false);
393395
}
394396

397+
/**
398+
* @param useNewStyle should use "new style" showcase (see {@link #withNewStyleShowcase()}
399+
* @deprecated use {@link #withHoloShowcase()}, {@link #withNewStyleShowcase()}, or
400+
* {@link #setShowcaseDrawer(ShowcaseDrawer)}
401+
*/
402+
@Deprecated
395403
public Builder(Activity activity, boolean useNewStyle) {
396404
this.activity = activity;
397405
this.showcaseView = new ShowcaseView(activity, useNewStyle);
@@ -408,6 +416,38 @@ public ShowcaseView build() {
408416
return showcaseView;
409417
}
410418

419+
/**
420+
* Draw a holo-style showcase. This is the default.<br/>
421+
* <img alt="Holo showcase example" src="../../../../../../../../example2.png" />
422+
*/
423+
public Builder withHoloShowcase() {
424+
return setShowcaseDrawer(new StandardShowcaseDrawer(activity.getResources()));
425+
}
426+
427+
/**
428+
* Draw a new-style showcase.<br/>
429+
* <img alt="Holo showcase example" src="../../../../../../../../example.png" />
430+
*/
431+
public Builder withNewStyleShowcase() {
432+
return setShowcaseDrawer(new NewShowcaseDrawer(activity.getResources()));
433+
}
434+
435+
/**
436+
* Draw a material style showcase.
437+
* <img alt="Material showcase" src="../../../../../../../../material.png" />
438+
*/
439+
public Builder withMaterialShowcase() {
440+
return setShowcaseDrawer(new MaterialShowcaseDrawer(activity.getResources()));
441+
}
442+
443+
/**
444+
* Set a custom showcase drawer which will be responsible for measuring and drawing the showcase
445+
*/
446+
public Builder setShowcaseDrawer(ShowcaseDrawer showcaseDrawer) {
447+
showcaseView.setShowcaseDrawer(showcaseDrawer);
448+
return this;
449+
}
450+
411451
/**
412452
* Set the title text shown on the ShowcaseView.
413453
*/
@@ -506,17 +546,33 @@ public Builder setShowcaseEventListener(OnShowcaseEventListener showcaseEventLis
506546
return this;
507547
}
508548

549+
/**
550+
* Sets the paint that will draw the text as specified by {@link #setContentText(CharSequence)}
551+
* or {@link #setContentText(int)}
552+
*/
509553
public Builder setContentTextPaint(TextPaint textPaint) {
510554
showcaseView.setContentTextPaint(textPaint);
511555
return this;
512556
}
513557

558+
/**
559+
* Sets the paint that will draw the text as specified by {@link #setContentTitle(CharSequence)}
560+
* or {@link #setContentTitle(int)}
561+
*/
514562
public Builder setContentTitlePaint(TextPaint textPaint) {
515563
showcaseView.setContentTitlePaint(textPaint);
516564
return this;
517565
}
518566
}
519567

568+
private void setShowcaseDrawer(ShowcaseDrawer showcaseDrawer) {
569+
this.showcaseDrawer = showcaseDrawer;
570+
this.showcaseDrawer.setBackgroundColour(backgroundColor);
571+
this.showcaseDrawer.setShowcaseColour(showcaseColor);
572+
hasAlteredText = true;
573+
invalidate();
574+
}
575+
520576
private void setContentTitlePaint(TextPaint textPaint) {
521577
this.textDrawer.setTitlePaint(textPaint);
522578
hasAlteredText = true;
@@ -595,8 +651,8 @@ public boolean isShowing() {
595651
}
596652

597653
private void updateStyle(TypedArray styled, boolean invalidate) {
598-
int backgroundColor = styled.getColor(R.styleable.ShowcaseView_sv_backgroundColor, Color.argb(128, 80, 80, 80));
599-
int showcaseColor = styled.getColor(R.styleable.ShowcaseView_sv_showcaseColor, HOLO_BLUE);
654+
backgroundColor = styled.getColor(R.styleable.ShowcaseView_sv_backgroundColor, Color.argb(128, 80, 80, 80));
655+
showcaseColor = styled.getColor(R.styleable.ShowcaseView_sv_showcaseColor, HOLO_BLUE);
600656
String buttonText = styled.getString(R.styleable.ShowcaseView_sv_buttonText);
601657
if (TextUtils.isEmpty(buttonText)) {
602658
buttonText = getResources().getString(android.R.string.ok);

library/src/main/res/values/dimens.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@
2222
<dimen name="showcase_radius">94dp</dimen>
2323
<dimen name="showcase_radius_inner">96dp</dimen>
2424
<dimen name="showcase_radius_outer">128dp</dimen>
25+
<dimen name="showcase_radius_material">48dip</dimen>
2526
</resources>

material.png

46.4 KB
Loading

sample/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ buildscript {
2020
}
2121

2222
dependencies {
23-
classpath 'com.android.tools.build:gradle:1.0.0'
23+
classpath 'com.android.tools.build:gradle:1.3.0'
2424
}
2525
}
2626

@@ -36,9 +36,9 @@ apply plugin: 'com.android.application'
3636
dependencies {
3737
compile project(':library')
3838
//compile 'com.github.amlcurran.showcaseview:library:5.0.0-SNAPSHOT'
39-
compile 'com.android.support:support-v4:21.0.0'
39+
compile 'com.android.support:support-v4:23.0.1'
4040
compile 'com.nineoldandroids:library:2.4.0'
41-
compile 'com.android.support:appcompat-v7:21.0.0'
41+
compile 'com.android.support:appcompat-v7:23.0.1'
4242
}
4343

4444
android {

sample/src/main/AndroidManifest.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,7 @@
4040

4141
<activity android:name=".CustomTextActivity" />
4242

43+
<activity android:name=".CustomShowcaseActivity" />
44+
4345
</application>
4446
</manifest>
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package com.github.amlcurran.showcaseview.sample;
2+
3+
import android.app.Activity;
4+
import android.content.res.Resources;
5+
import android.graphics.Bitmap;
6+
import android.graphics.Canvas;
7+
import android.graphics.Paint;
8+
import android.graphics.PorterDuff;
9+
import android.graphics.PorterDuffXfermode;
10+
import android.graphics.RectF;
11+
import android.os.Bundle;
12+
13+
import com.github.amlcurran.showcaseview.ShowcaseDrawer;
14+
import com.github.amlcurran.showcaseview.ShowcaseView;
15+
import com.github.amlcurran.showcaseview.targets.ViewTarget;
16+
17+
public class CustomShowcaseActivity extends Activity {
18+
19+
@Override
20+
protected void onCreate(Bundle savedInstanceState) {
21+
super.onCreate(savedInstanceState);
22+
setContentView(R.layout.activity_custom_showcase);
23+
24+
new ShowcaseView.Builder(this)
25+
.setTarget(new ViewTarget(R.id.imageView, this))
26+
.setContentTitle(R.string.custom_text_painting_title)
27+
.setContentText(R.string.custom_text_painting_text)
28+
.setShowcaseDrawer(new CustomShowcaseView(getResources()))
29+
.build();
30+
}
31+
32+
private static class CustomShowcaseView implements ShowcaseDrawer {
33+
34+
private final float width;
35+
private final float height;
36+
private final Paint eraserPaint;
37+
private final Paint basicPaint;
38+
private final int eraseColour;
39+
private final RectF renderRect;
40+
41+
public CustomShowcaseView(Resources resources) {
42+
width = resources.getDimension(R.dimen.custom_showcase_width);
43+
height = resources.getDimension(R.dimen.custom_showcase_height);
44+
PorterDuffXfermode xfermode = new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY);
45+
eraserPaint = new Paint();
46+
eraserPaint.setColor(0xFFFFFF);
47+
eraserPaint.setAlpha(0);
48+
eraserPaint.setXfermode(xfermode);
49+
eraserPaint.setAntiAlias(true);
50+
eraseColour = resources.getColor(R.color.custom_showcase_bg);
51+
basicPaint = new Paint();
52+
renderRect = new RectF();
53+
}
54+
55+
@Override
56+
public void setShowcaseColour(int color) {
57+
eraserPaint.setColor(color);
58+
}
59+
60+
@Override
61+
public void drawShowcase(Bitmap buffer, float x, float y, float scaleMultiplier) {
62+
Canvas bufferCanvas = new Canvas(buffer);
63+
renderRect.left = x - width / 2f;
64+
renderRect.right = x + width / 2f;
65+
renderRect.top = y - height / 2f;
66+
renderRect.bottom = y + height / 2f;
67+
bufferCanvas.drawRect(renderRect, eraserPaint);
68+
}
69+
70+
@Override
71+
public int getShowcaseWidth() {
72+
return (int) width;
73+
}
74+
75+
@Override
76+
public int getShowcaseHeight() {
77+
return (int) height;
78+
}
79+
80+
@Override
81+
public float getBlockedRadius() {
82+
return width;
83+
}
84+
85+
@Override
86+
public void setBackgroundColour(int backgroundColor) {
87+
// No-op, remove this from the API?
88+
}
89+
90+
@Override
91+
public void erase(Bitmap bitmapBuffer) {
92+
bitmapBuffer.eraseColor(eraseColour);
93+
}
94+
95+
@Override
96+
public void drawToCanvas(Canvas canvas, Bitmap bitmapBuffer) {
97+
canvas.drawBitmap(bitmapBuffer, 0, 0, basicPaint);
98+
}
99+
100+
}
101+
102+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ protected void onCreate(Bundle savedInstanceState) {
2727
title.setTypeface(Typeface.createFromAsset(getAssets(), "RobotoSlab-Regular.ttf"));
2828

2929
new ShowcaseView.Builder(this)
30+
.withNewStyleShowcase()
3031
.setTarget(new ViewTarget(R.id.imageView, this))
3132
.setContentTitle(R.string.custom_text_painting_title)
3233
.setContentText(R.string.custom_text_painting_text)

0 commit comments

Comments
 (0)