From 5fe546ee115146d5aa7ddff6473c47fcc4389f59 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Sun, 24 Sep 2017 15:44:05 +0200 Subject: [PATCH] Support different location modes on MemeElementText --- .../activity/MemeCreateActivity.java | 29 +++++++--------- .../memetastic/data/MemeLibConfig.java | 9 ++++- .../gsantner/memetastic/data/MemeSetting.java | 33 +++++++++++++++++-- 3 files changed, 50 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/io/github/gsantner/memetastic/activity/MemeCreateActivity.java b/app/src/main/java/io/github/gsantner/memetastic/activity/MemeCreateActivity.java index a9150923d..d19587484 100644 --- a/app/src/main/java/io/github/gsantner/memetastic/activity/MemeCreateActivity.java +++ b/app/src/main/java/io/github/gsantner/memetastic/activity/MemeCreateActivity.java @@ -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; @@ -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); @@ -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 @@ -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(); diff --git a/app/src/main/java/io/github/gsantner/memetastic/data/MemeLibConfig.java b/app/src/main/java/io/github/gsantner/memetastic/data/MemeLibConfig.java index 9a8a5a0b1..8bee4a860 100644 --- a/app/src/main/java/io/github/gsantner/memetastic/data/MemeLibConfig.java +++ b/app/src/main/java/io/github/gsantner/memetastic/data/MemeLibConfig.java @@ -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 { @@ -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; diff --git a/app/src/main/java/io/github/gsantner/memetastic/data/MemeSetting.java b/app/src/main/java/io/github/gsantner/memetastic/data/MemeSetting.java index 1e46c2a16..44b2d1b7d 100644 --- a/app/src/main/java/io/github/gsantner/memetastic/data/MemeSetting.java +++ b/app/src/main/java/io/github/gsantner/memetastic/data/MemeSetting.java @@ -1,6 +1,7 @@ package io.github.gsantner.memetastic.data; import android.graphics.Bitmap; +import android.graphics.PointF; /** * A memes settings @@ -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); @@ -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(); } @@ -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; }