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

Commit

Permalink
Merge pull request #306 from amlcurran/custom-showcase-drawer
Browse files Browse the repository at this point in the history
Custom + material showcase drawer API
  • Loading branch information
amlcurran committed Oct 17, 2015
2 parents c013ab8 + 47cf440 commit f0cb303
Show file tree
Hide file tree
Showing 16 changed files with 280 additions and 24 deletions.
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
classpath 'com.android.tools.build:gradle:1.3.0'
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.github.amlcurran.showcaseview;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;

public class MaterialShowcaseDrawer implements ShowcaseDrawer {

private final float radius;
private final Paint basicPaint;
private final Paint eraserPaint;
private int backgroundColor;

public MaterialShowcaseDrawer(Resources resources) {
this.radius = resources.getDimension(R.dimen.showcase_radius_material);
this.eraserPaint = new Paint();
this.eraserPaint.setColor(0xFFFFFF);
this.eraserPaint.setAlpha(0);
this.eraserPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));
this.eraserPaint.setAntiAlias(true);
this.basicPaint = new Paint();
}

@Override
public void setShowcaseColour(int color) {
// no-op
}

@Override
public void drawShowcase(Bitmap buffer, float x, float y, float scaleMultiplier) {
Canvas bufferCanvas = new Canvas(buffer);
bufferCanvas.drawCircle(x, y, radius, eraserPaint);
}

@Override
public int getShowcaseWidth() {
return (int) (radius * 2);
}

@Override
public int getShowcaseHeight() {
return (int) (radius * 2);
}

@Override
public float getBlockedRadius() {
return radius;
}

@Override
public void setBackgroundColour(int backgroundColor) {
this.backgroundColor = backgroundColor;
}

@Override
public void erase(Bitmap bitmapBuffer) {
bitmapBuffer.eraseColor(backgroundColor);
}

@Override
public void drawToCanvas(Canvas canvas, Bitmap bitmapBuffer) {
canvas.drawBitmap(bitmapBuffer, 0, 0, basicPaint);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@
import android.graphics.Bitmap;
import android.graphics.Canvas;

/**
* Created by curraa01 on 13/10/2013.
*/
interface ShowcaseDrawer {
public interface ShowcaseDrawer {

void setShowcaseColour(int color);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class ShowcaseView extends RelativeLayout

private final Button mEndButton;
private final TextDrawer textDrawer;
private final ShowcaseDrawer showcaseDrawer;
private ShowcaseDrawer showcaseDrawer;
private final ShowcaseAreaCalculator showcaseAreaCalculator;
private final AnimationFactory animationFactory;
private final ShotStateStore shotStateStore;
Expand All @@ -75,6 +75,8 @@ public class ShowcaseView extends RelativeLayout
private long fadeInMillis;
private long fadeOutMillis;
private boolean isShowing;
private int backgroundColor;
private int showcaseColor;

protected ShowcaseView(Context context, boolean newStyle) {
this(context, null, R.styleable.CustomTheme_showcaseViewStyle, newStyle);
Expand Down Expand Up @@ -392,6 +394,12 @@ public Builder(Activity activity) {
this(activity, false);
}

/**
* @param useNewStyle should use "new style" showcase (see {@link #withNewStyleShowcase()}
* @deprecated use {@link #withHoloShowcase()}, {@link #withNewStyleShowcase()}, or
* {@link #setShowcaseDrawer(ShowcaseDrawer)}
*/
@Deprecated
public Builder(Activity activity, boolean useNewStyle) {
this.activity = activity;
this.showcaseView = new ShowcaseView(activity, useNewStyle);
Expand All @@ -408,6 +416,38 @@ public ShowcaseView build() {
return showcaseView;
}

/**
* Draw a holo-style showcase. This is the default.<br/>
* <img alt="Holo showcase example" src="../../../../../../../../example2.png" />
*/
public Builder withHoloShowcase() {
return setShowcaseDrawer(new StandardShowcaseDrawer(activity.getResources()));
}

/**
* Draw a new-style showcase.<br/>
* <img alt="Holo showcase example" src="../../../../../../../../example.png" />
*/
public Builder withNewStyleShowcase() {
return setShowcaseDrawer(new NewShowcaseDrawer(activity.getResources()));
}

/**
* Draw a material style showcase.
* <img alt="Material showcase" src="../../../../../../../../material.png" />
*/
public Builder withMaterialShowcase() {
return setShowcaseDrawer(new MaterialShowcaseDrawer(activity.getResources()));
}

/**
* Set a custom showcase drawer which will be responsible for measuring and drawing the showcase
*/
public Builder setShowcaseDrawer(ShowcaseDrawer showcaseDrawer) {
showcaseView.setShowcaseDrawer(showcaseDrawer);
return this;
}

/**
* Set the title text shown on the ShowcaseView.
*/
Expand Down Expand Up @@ -506,17 +546,33 @@ public Builder setShowcaseEventListener(OnShowcaseEventListener showcaseEventLis
return this;
}

/**
* Sets the paint that will draw the text as specified by {@link #setContentText(CharSequence)}
* or {@link #setContentText(int)}
*/
public Builder setContentTextPaint(TextPaint textPaint) {
showcaseView.setContentTextPaint(textPaint);
return this;
}

/**
* Sets the paint that will draw the text as specified by {@link #setContentTitle(CharSequence)}
* or {@link #setContentTitle(int)}
*/
public Builder setContentTitlePaint(TextPaint textPaint) {
showcaseView.setContentTitlePaint(textPaint);
return this;
}
}

private void setShowcaseDrawer(ShowcaseDrawer showcaseDrawer) {
this.showcaseDrawer = showcaseDrawer;
this.showcaseDrawer.setBackgroundColour(backgroundColor);
this.showcaseDrawer.setShowcaseColour(showcaseColor);
hasAlteredText = true;
invalidate();
}

private void setContentTitlePaint(TextPaint textPaint) {
this.textDrawer.setTitlePaint(textPaint);
hasAlteredText = true;
Expand Down Expand Up @@ -595,8 +651,8 @@ public boolean isShowing() {
}

private void updateStyle(TypedArray styled, boolean invalidate) {
int backgroundColor = styled.getColor(R.styleable.ShowcaseView_sv_backgroundColor, Color.argb(128, 80, 80, 80));
int showcaseColor = styled.getColor(R.styleable.ShowcaseView_sv_showcaseColor, HOLO_BLUE);
backgroundColor = styled.getColor(R.styleable.ShowcaseView_sv_backgroundColor, Color.argb(128, 80, 80, 80));
showcaseColor = styled.getColor(R.styleable.ShowcaseView_sv_showcaseColor, HOLO_BLUE);
String buttonText = styled.getString(R.styleable.ShowcaseView_sv_buttonText);
if (TextUtils.isEmpty(buttonText)) {
buttonText = getResources().getString(android.R.string.ok);
Expand Down
1 change: 1 addition & 0 deletions library/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@
<dimen name="showcase_radius">94dp</dimen>
<dimen name="showcase_radius_inner">96dp</dimen>
<dimen name="showcase_radius_outer">128dp</dimen>
<dimen name="showcase_radius_material">48dip</dimen>
</resources>
Binary file added material.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
classpath 'com.android.tools.build:gradle:1.3.0'
}
}

Expand All @@ -36,9 +36,9 @@ apply plugin: 'com.android.application'
dependencies {
compile project(':library')
//compile 'com.github.amlcurran.showcaseview:library:5.0.0-SNAPSHOT'
compile 'com.android.support:support-v4:21.0.0'
compile 'com.android.support:support-v4:23.0.1'
compile 'com.nineoldandroids:library:2.4.0'
compile 'com.android.support:appcompat-v7:21.0.0'
compile 'com.android.support:appcompat-v7:23.0.1'
}

android {
Expand Down
2 changes: 2 additions & 0 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,7 @@

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

<activity android:name=".CustomShowcaseActivity" />

</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.github.amlcurran.showcaseview.sample;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.os.Bundle;

import com.github.amlcurran.showcaseview.ShowcaseDrawer;
import com.github.amlcurran.showcaseview.ShowcaseView;
import com.github.amlcurran.showcaseview.targets.ViewTarget;

public class CustomShowcaseActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_showcase);

new ShowcaseView.Builder(this)
.setTarget(new ViewTarget(R.id.imageView, this))
.setContentTitle(R.string.custom_text_painting_title)
.setContentText(R.string.custom_text_painting_text)
.setShowcaseDrawer(new CustomShowcaseView(getResources()))
.build();
}

private static class CustomShowcaseView implements ShowcaseDrawer {

private final float width;
private final float height;
private final Paint eraserPaint;
private final Paint basicPaint;
private final int eraseColour;
private final RectF renderRect;

public CustomShowcaseView(Resources resources) {
width = resources.getDimension(R.dimen.custom_showcase_width);
height = resources.getDimension(R.dimen.custom_showcase_height);
PorterDuffXfermode xfermode = new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY);
eraserPaint = new Paint();
eraserPaint.setColor(0xFFFFFF);
eraserPaint.setAlpha(0);
eraserPaint.setXfermode(xfermode);
eraserPaint.setAntiAlias(true);
eraseColour = resources.getColor(R.color.custom_showcase_bg);
basicPaint = new Paint();
renderRect = new RectF();
}

@Override
public void setShowcaseColour(int color) {
eraserPaint.setColor(color);
}

@Override
public void drawShowcase(Bitmap buffer, float x, float y, float scaleMultiplier) {
Canvas bufferCanvas = new Canvas(buffer);
renderRect.left = x - width / 2f;
renderRect.right = x + width / 2f;
renderRect.top = y - height / 2f;
renderRect.bottom = y + height / 2f;
bufferCanvas.drawRect(renderRect, eraserPaint);
}

@Override
public int getShowcaseWidth() {
return (int) width;
}

@Override
public int getShowcaseHeight() {
return (int) height;
}

@Override
public float getBlockedRadius() {
return width;
}

@Override
public void setBackgroundColour(int backgroundColor) {
// No-op, remove this from the API?
}

@Override
public void erase(Bitmap bitmapBuffer) {
bitmapBuffer.eraseColor(eraseColour);
}

@Override
public void drawToCanvas(Canvas canvas, Bitmap bitmapBuffer) {
canvas.drawBitmap(bitmapBuffer, 0, 0, basicPaint);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ protected void onCreate(Bundle savedInstanceState) {
title.setTypeface(Typeface.createFromAsset(getAssets(), "RobotoSlab-Regular.ttf"));

new ShowcaseView.Builder(this)
.withNewStyleShowcase()
.setTarget(new ViewTarget(R.id.imageView, this))
.setContentTitle(R.string.custom_text_painting_title)
.setContentText(R.string.custom_text_painting_text)
Expand Down
Loading

0 comments on commit f0cb303

Please sign in to comment.