Back to project page Sertimus.
The source code is released under:
GNU General Public License
If you think the Android project Sertimus listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package gameaddict30.wallpaper.sertimus.objects; //from w w w .j a v a 2s.com import gameaddict30.wallpaper.sertimus.MainCanvas; import gameaddict30.wallpaper.sertimus.R; import gameaddict30.wallpaper.sertimus.utilities.Strings; import android.graphics.*; import android.graphics.Paint.*; public class TalkBubble extends LiveObject { private static float fontSize = 0f; private static final float boxRadius = 5f; private static final float boxPadding = 5f; private RectF boundary; private Path objPointer; private LiveObject hook; private String[] text; private long duration = 5000l, startTime=System.currentTimeMillis(); private Paint rectP, textP; public TalkBubble(MainCanvas c, LiveObject l, String text) { // TalkBubble with default duration super(c); initBubble(l,text); } public LiveObject getHook() { return hook; } public TalkBubble(MainCanvas c, LiveObject l, String text, long duration) { // TalkBubble with default duration super(c); this.duration = duration; initBubble(l,text); } @Override protected void init(MainCanvas c) { /* An instantiation of a new LiveObject, by default, will add itself to * the referenced MainCanvas instance's ArrayList<LiveObject> object. * This method override will also send a reference of this instance to * the appropriate ArrayList<TalkBubble> object, otherwise we'll have * additional TalkBubble references that we can't get rid of. */ c.queueTalkBubble(this); refCanvas = c; } protected void initBubble(LiveObject l, String text) { this.hook = l; textP = new Paint(); textP.setColor(Color.BLACK); textP.setTextSize(fontSize); textP.setTextAlign(Align.LEFT); if (fontSize == 0f) { fontSize = refCanvas.getAppContext().getResources().getDimension(R.dimen.sertimus_talkbubble_fontsize); } width = (int)(hook.getWidth()*2+boxPadding*2); String completeString = autoWrap(text); height = (int)((Strings.countOccurrenceOf(completeString,'\n')+1)*fontSize+boxPadding*2); this.text = completeString.split("\n"); boundary = new RectF(x,y,x+width+boxPadding,x+height+boxPadding); rectP = new Paint(); objPointer = new Path(); updateObject(); } public long getStartTime() { return startTime; } public long getDuration() { return duration; } protected String autoWrap(String text) { // Format the text to fit within the boundaries of our TalkBubble. Rect textBounds = new Rect(); for (int x=0,lastNLIndex=0; x < text.length(); x++) { textP.getTextBounds(text, lastNLIndex, x+1, textBounds); if (textBounds.width() > width-boxRadius-boxPadding) { lastNLIndex=x; if (text.charAt(x) == '\n') continue; String startSub = text.substring(0, x), endSub = text.substring(x, text.length()); text = startSub+'\n'+endSub; } } return text; } @Override public void updateObject() { x = (float)(hook.getX()-width/2); y = (float)(hook.getY()-hook.getHeight()-15); if (x < 0) x = 0; else if (x+width > refCanvas.getWidth()) x = refCanvas.getWidth()-width; if (y < 0) y = 0; else if (y+height > refCanvas.getHeight()) y = refCanvas.getHeight()-height; boundary.set(x,y,x+width,y+height); objPointer.reset(); objPointer.moveTo(x+((float)width+boxPadding)*(2f/6f), y+((float)height-boxPadding)); objPointer.lineTo(x+((float)width+boxPadding)*(4f/6f), y+((float)height-boxPadding)); objPointer.lineTo(x+(hook.getX()-x), y+((float)height-boxPadding)+15f); objPointer.close(); } @Override public void drawObject(Canvas c) { rectP.setColor(Color.WHITE); rectP.setStyle(Style.FILL); c.drawRoundRect(boundary, boxRadius, boxRadius, rectP); rectP.setColor(Color.BLACK); rectP.setStrokeCap(Cap.ROUND); rectP.setStrokeJoin(Join.MITER); rectP.setStrokeWidth(1f); rectP.setStyle(Style.STROKE); c.drawRoundRect(boundary, boxRadius, boxRadius, rectP); for (int x=0; x < text.length; x++) { c.drawText(text[x], boundary.left+boxPadding, boundary.top+x*fontSize+fontSize, textP); } rectP.setColor(Color.WHITE); rectP.setStyle(Style.FILL_AND_STROKE); rectP.setStrokeWidth(1f); rectP.setStrokeCap(Cap.BUTT); rectP.setStrokeJoin(Join.MITER); c.drawPath(objPointer, rectP); } }