Skip to content

Commit

Permalink
Made dribbble GIF badges work for spanned columns. Some code cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
nickbutcher committed Nov 18, 2015
1 parent 9c6468a commit 48ddfdb
Showing 1 changed file with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PorterDuff;
Expand All @@ -30,6 +31,7 @@
import android.support.annotation.ColorInt;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.Gravity;

import io.plaidapp.R;
Expand Down Expand Up @@ -75,6 +77,12 @@ public void draw(Canvas canvas) {
}
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
layoutBadge();
}

private void layoutBadge() {
Rect badgeBounds = badge.getBounds();
Gravity.apply(badgeGravity,
Expand All @@ -94,10 +102,10 @@ private void layoutBadge() {
private static class GifBadge extends Drawable {

private static final String GIF = "GIF";
private static final int TEXT_SIZE = 12; // dp
private static final int TEXT_SIZE = 12; // sp
private static final int PADDING = 4; // dp
private static final int CORNER_RADIUS = 2; // dp
private static final int BACKGROUND_COLOR = 0xffffffff;
private static final int BACKGROUND_COLOR = Color.WHITE;
private static final String TYPEFACE = "sans-serif-black";
private static final int TYPEFACE_STYLE = Typeface.NORMAL;
private static Bitmap bitmap;
Expand All @@ -107,13 +115,16 @@ private static class GifBadge extends Drawable {

GifBadge(Context context) {
if (bitmap == null) {
final float density = context.getResources().getDisplayMetrics().scaledDensity;
final DisplayMetrics dm = context.getResources().getDisplayMetrics();
final float density = dm.density;
final float scaledDensity = dm.scaledDensity;
final TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint
.SUBPIXEL_TEXT_FLAG);
textPaint.setTypeface(Typeface.create(TYPEFACE, TYPEFACE_STYLE));
textPaint.setTextSize(TEXT_SIZE * density);
textPaint.setTextSize(TEXT_SIZE * scaledDensity);

final float padding = PADDING * density;
final float cornerRadius = CORNER_RADIUS * density;
final Rect textBounds = new Rect();
textPaint.getTextBounds(GIF, 0, GIF.length(), textBounds);
height = (int) (padding + textBounds.height() + padding);
Expand All @@ -123,9 +134,9 @@ private static class GifBadge extends Drawable {
final Canvas canvas = new Canvas(bitmap);
final Paint backgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
backgroundPaint.setColor(BACKGROUND_COLOR);
canvas.drawRoundRect(0, 0, width, height, CORNER_RADIUS * density, CORNER_RADIUS
* density, backgroundPaint);
// this is the magic, this mode punches out the word as transparency
canvas.drawRoundRect(0, 0, width, height, cornerRadius, cornerRadius,
backgroundPaint);
// punch out the word 'GIF', leaving transparency
textPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
canvas.drawText(GIF, padding, height - padding, textPaint);
}
Expand Down

0 comments on commit 48ddfdb

Please sign in to comment.