Skip to content

Commit

Permalink
Support different location modes on MemeElementText
Browse files Browse the repository at this point in the history
  • Loading branch information
Lonami committed Sep 24, 2017
1 parent 43a5ff7 commit 5fe546e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Bundle;
Expand Down Expand Up @@ -583,14 +584,12 @@ public Bitmap drawMultilineTextToBitmap(Context c, MemeSetting memeSetting) {
//paint.setStrokeWidth(memeSetting.getFontSize() / 4);
paint.setStrokeWidth(borderScale);

String[] textStrings = {memeSetting.getCaptionTop().getText(), memeSetting.getCaptionBottom().getText()};
if (memeSetting.getCaptionTop().isAllCaps()) {
for (int i = 0; i < textStrings.length; i++) {
textStrings[i] = textStrings[i].toUpperCase();
}
}
MemeSetting.MemeElementText[] captions = {memeSetting.getCaptionTop(), memeSetting.getCaptionBottom()};
boolean allCaps = memeSetting.getCaptionTop().isAllCaps();

for (int i = 0; i < captions.length; i++) {
String textString = allCaps ? captions[i].getText().toUpperCase() : captions[i].getText();

for (int i = 0; i < textStrings.length; i++) {
paint.setColor(memeSetting.getCaptionTop().getBorderColor());
paint.setStyle(Paint.Style.FILL_AND_STROKE);

Expand All @@ -599,22 +598,18 @@ public Bitmap drawMultilineTextToBitmap(Context c, MemeSetting memeSetting) {

// init StaticLayout for text
StaticLayout textLayout = new StaticLayout(
textStrings[i], paint, textWidth, Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
textString, paint, textWidth, Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);

// get height of multiline text
int textHeight = textLayout.getHeight();

// get position of text's top left corner center: (bitmap.getWidth() - textWidth)/2
float x = (bitmap.getWidth() - textWidth) / 2;
float y = 0;
if (i == 0)
y = bitmap.getHeight() / 15;
else
y = bitmap.getHeight() - textHeight;
// get position of text in the canvas, this will depend in its internal location mode
PointF where = captions[i].getPositionInCanvas(
bitmap.getWidth(), bitmap.getHeight(), textWidth, textHeight);

// draw text to the Canvas center
canvas.save();
canvas.translate(x, y);
canvas.translate(where.x, where.y);
textLayout.draw(canvas);

// new antialiased Paint
Expand All @@ -623,7 +618,7 @@ public Bitmap drawMultilineTextToBitmap(Context c, MemeSetting memeSetting) {

// init StaticLayout for text
textLayout = new StaticLayout(
textStrings[i], paint, textWidth, Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
textString, paint, textWidth, Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);

// get height of multiline text
textHeight = textLayout.getHeight();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import java.io.File;

/**
* Contains colors, fontsizes, categorynames and folders where fonts and memetemplates are saved
* Contains colors, fontsizes, locations, categorynames
* and folders where fonts and memetemplates are saved
*/
@SuppressWarnings("WeakerAccess")
public class MemeLibConfig {
Expand All @@ -18,6 +19,12 @@ public static class FONT_SIZES {
public static final int MAX = 30;
}

public static class LOCATION_MODE {
public static final int CUSTOM = 0;
public static final int TOP = 1;
public static final int BOTTOM = 2;
}

public static class MEME_COLORS {
public static final int BLACK = 0xff000000;
public static final int WHITE = 0xffFFFFFF;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.gsantner.memetastic.data;

import android.graphics.Bitmap;
import android.graphics.PointF;

/**
* A memes settings
Expand All @@ -12,8 +13,8 @@ public class MemeSetting extends MemeSettingBase implements MemeSettingBase.OnMe
private MemeElementImage _imageMain;

public MemeSetting(MemeData.Font font, Bitmap image) {
_captionTop = new MemeElementText(font);
_captionBottom = new MemeElementText(font);
_captionTop = new MemeElementText(font, MemeLibConfig.LOCATION_MODE.TOP);
_captionBottom = new MemeElementText(font, MemeLibConfig.LOCATION_MODE.BOTTOM);
_imageMain = new MemeElementImage(image);

_captionTop.setMemeSettingChangedListener(this);
Expand Down Expand Up @@ -63,11 +64,21 @@ public static class MemeElementText extends MemeSettingBase {
private int _fontSize = MemeLibConfig.FONT_SIZES.DEFAULT;
private int _textColor = MemeLibConfig.MEME_COLORS.DEFAULT_TEXT;
private int _borderColor = MemeLibConfig.MEME_COLORS.DEFAULT_BORDER;
private int _locationMode = MemeLibConfig.LOCATION_MODE.CUSTOM;
private PointF _location = new PointF(); // (x, y)% for custom location
private boolean _allCaps = true;
private MemeData.Font _font = null;

public MemeElementText(MemeData.Font font) {
public MemeElementText(MemeData.Font font, int locationMode) {
_font = font;
_locationMode = locationMode;
notifyChangedListener();
}

public MemeElementText(MemeData.Font font, float x, float y) {
_font = font;
_locationMode = MemeLibConfig.LOCATION_MODE.CUSTOM;
_location.set(x, y);
notifyChangedListener();
}

Expand All @@ -89,6 +100,22 @@ public void setFontSize(int fontSize) {
notifyChangedListener();
}

public PointF getPositionInCanvas(float width, float height, float textWidth, float textHeight) {
switch (_locationMode) {
case MemeLibConfig.LOCATION_MODE.CUSTOM:
default:
return new PointF(
width * _location.x - textWidth * 0.5f,
height * _location.y - textHeight * 0.5f);

case MemeLibConfig.LOCATION_MODE.TOP:
return new PointF((width - textWidth) * 0.5f, height / 15f);

case MemeLibConfig.LOCATION_MODE.BOTTOM:
return new PointF((width - textWidth) * 0.5f, height - textHeight);
}
}

public int getTextColor() {
return _textColor;
}
Expand Down

0 comments on commit 5fe546e

Please sign in to comment.