public class

TextShape

extends Shape
java.lang.Object
   ↳ sofia.graphics.Shape
     ↳ sofia.graphics.TextShape

Class Overview

A shape that renders a text string on the canvas.

Summary

Nested Classes
class TextShape.Animator<AnimatorType extends Animator<AnimatorType>> Provides animation support for shapes. 
Public Constructors
TextShape(String text, PointF origin)
Creates a TextShape with the specified text that is positioned so that the top-left corner of the area containing the text is at the specified point.
TextShape(String text, float x, float y)
Creates a TextShape with the specified text that is positioned so that the top-left corner of the area containing the text is at the specified point.
TextShape(String text, PointAndAnchor pointAndAnchor)
Creates a TextShape with the specified text that is positioned with respect to the specified point and anchor.
Public Methods
Animator<?> animate(long duration)

Gets an animator object that lets the user animate properties of the receiving shape.

void draw(Canvas canvas)
Subclasses must implement this method to define how the shape is to be drawn on the canvas.
float getAscent()
RectF getBounds()
Gets the bounding rectangle of the shape.
float getDescent()
String getText()
Gets the text drawn by this shape.
float getTypeSize()
Gets the point size of the text in the shape.
Typeface getTypeface()
Gets the typeface used to render the text in this shape.
void setBounds(RectF newBounds)
Updates the position of the text shape so that the text would be drawn in the center of the bounding rectangle.
void setText(String text)
Sets the text drawn by this shape.
void setTypeSize(float typeSize)
Sets the point size of the text in the shape.
void setTypeface(Typeface typeface)
Sets the typeface used to render the text in this shape.
void setTypefaceAndSize(String typefaceAndSize)

Sets the typeface and size of the text in this shape using a shorthand string to describe these properties.

Protected Methods
void createFixtures()
This method is intended for internal use only, or by advanced users subclassing one of the abstract shape types.
Paint getPaint()
Gets the Paint object that describes how this shape should be drawn.
[Expand]
Inherited Methods
From class sofia.graphics.Shape
From class java.lang.Object

Public Constructors

public TextShape (String text, PointF origin)

Creates a TextShape with the specified text that is positioned so that the top-left corner of the area containing the text is at the specified point.

Parameters
text the text to draw in the shape
origin the point on the view to draw the text

public TextShape (String text, float x, float y)

Creates a TextShape with the specified text that is positioned so that the top-left corner of the area containing the text is at the specified point.

Parameters
text the text to draw in the shape

public TextShape (String text, PointAndAnchor pointAndAnchor)

Creates a TextShape with the specified text that is positioned with respect to the specified point and anchor. For example, new TextShape("foo", Anchor.BOTTOM.anchoredAt(point)) would anchor the text so that the bottom-center of the text is at the specified point.

Parameters
text the text to draw in the shape
pointAndAnchor a PointAndAnchor that determines where the text should be positioned

Public Methods

public Animator<?> animate (long duration)

Gets an animator object that lets the user animate properties of the receiving shape.

For ease of use, the description of the animation desired can be chained directly to the result of this method. For example, the following code fragment would create an animation that runs for 2 seconds, gradually changing the shape's color to red, its position to the top-right corner of the view, and then starts the animation after a delay of 1 second after the method is called:

     shape.animate(2000)
          .delay(1000)
          .color(Color.red)
          .position(Anchor.TOP_RIGHT.ofView())
          .play();

Parameters
duration The length of the animation in milliseconds.
Returns
  • A ShapeAnimator that lets the user animate properties of the receiving shape.

public void draw (Canvas canvas)

Subclasses must implement this method to define how the shape is to be drawn on the canvas. Users should never call this method directly; it is called as part of the repaint cycle by the ShapeView that contains the shape.

Parameters
canvas The Canvas on which to draw the shape.

public float getAscent ()

public RectF getBounds ()

Gets the bounding rectangle of the shape. The top-left corner of the bounding rectangle is the shape's origin, and the bottom-right corner is the shape's extent.

Returns
  • The bounding rectangle of the shape.

public float getDescent ()

public String getText ()

Gets the text drawn by this shape.

Returns
  • the text drawn by this shape

public float getTypeSize ()

Gets the point size of the text in the shape.

Returns
  • the point size of the text in the shape

public Typeface getTypeface ()

Gets the typeface used to render the text in this shape. The typeface indicates the font family (such as "Droid Sans") and whether or not the type is bold and/or italicized.

Returns
  • the Typeface used to render the text in this shape

public void setBounds (RectF newBounds)

Updates the position of the text shape so that the text would be drawn in the center of the bounding rectangle.

Parameters
newBounds the new bounds

public void setText (String text)

Sets the text drawn by this shape.

Parameters
text the text to be drawn by this shape

public void setTypeSize (float typeSize)

Sets the point size of the text in the shape.

Parameters
typeSize the new point size of the text in the shape

public void setTypeface (Typeface typeface)

Sets the typeface used to render the text in this shape. The typeface indicates the font family (such as "Droid Sans") and whether or not the type is bold and/or italicized.

Parameters
typeface the Typeface used to render the text in this shape

public void setTypefaceAndSize (String typefaceAndSize)

Sets the typeface and size of the text in this shape using a shorthand string to describe these properties.

The string passed to this method should be in the format "typeface-style-size", where:

  • typeface is the name of the typeface (such as "DroidSans")
  • style is one of the following: plain or normal for normal text, bold for bold text, italic for italicized text, or bolditalic for both bold and italicized text
  • size is the size of the text, in points

Any of the three parts of this string can be replaced with a "*" wildcard, which means retain its original value while changing the others.

Parameters
typefaceAndSize a string describing the typeface and text size

Protected Methods

protected void createFixtures ()

This method is intended for internal use only, or by advanced users subclassing one of the abstract shape types. Subclasses must override this method to create the necessary fixtures for the body that this shape represents. Use the getB2Body() method to access the body when creating fixtures.

protected Paint getPaint ()

Gets the Paint object that describes how this shape should be drawn. By default, the Paint's style is set to STROKE and the color to the value returned by getColor(). Subclasses can override this method to add their own attributes; they should call the superclass implementation and then add their own styles to the returned object.

Returns
  • A Paint object describing how the shape should be drawn.