TextShaper Classe
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Fournit une mise en forme de texte pour le texte à styles multiples.
[Android.Runtime.Register("android/text/TextShaper", ApiSince=31, DoNotGenerateAcw=true)]
public class TextShaper : Java.Lang.Object
[<Android.Runtime.Register("android/text/TextShaper", ApiSince=31, DoNotGenerateAcw=true)>]
type TextShaper = class
inherit Object
- Héritage
- Attributs
Remarques
Fournit une mise en forme de texte pour le texte à styles multiples.
Voici un exemple d’animation de la taille du texte et de l’espacement des lettres pour du texte simple.
<code>
// In this example, shape the text once for start and end state, then animate between two shape
// result without re-shaping in each frame.
class SimpleAnimationView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
private val textDir = TextDirectionHeuristics.LOCALE
private val text = "Hello, World." // The text to be displayed
// Class for keeping drawing parameters.
data class DrawStyle(val textSize: Float, val alpha: Int)
// The start and end text shaping result. This class will animate between these two.
private val start = mutableListOf<Pair<PositionedGlyphs, DrawStyle>>()
private val end = mutableListOf<Pair<PositionedGlyphs, DrawStyle>>()
init {
val startPaint = TextPaint().apply {
alpha = 0 // Alpha only affect text drawing but not text shaping
textSize = 36f // TextSize affect both text shaping and drawing.
letterSpacing = 0f // Letter spacing only affect text shaping but not drawing.
}
val endPaint = TextPaint().apply {
alpha = 255
textSize =128f
letterSpacing = 0.1f
}
TextShaper.shapeText(text, 0, text.length, textDir, startPaint) { _, _, glyphs, paint ->
start.add(Pair(glyphs, DrawStyle(paint.textSize, paint.alpha)))
}
TextShaper.shapeText(text, 0, text.length, textDir, endPaint) { _, _, glyphs, paint ->
end.add(Pair(glyphs, DrawStyle(paint.textSize, paint.alpha)))
}
}
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
// Set the baseline to the vertical center of the view.
canvas.translate(0f, height / 2f)
// Assume the number of PositionedGlyphs are the same. If different, you may want to
// animate in a different way, e.g. cross fading.
start.zip(end) { (startGlyphs, startDrawStyle), (endGlyphs, endDrawStyle) ->
// Tween the style and set to paint.
paint.textSize = lerp(startDrawStyle.textSize, endDrawStyle.textSize, progress)
paint.alpha = lerp(startDrawStyle.alpha, endDrawStyle.alpha, progress)
// Assume the number of glyphs are the same. If different, you may want to animate in
// a different way, e.g. cross fading.
require(startGlyphs.glyphCount() == endGlyphs.glyphCount())
if (startGlyphs.glyphCount() == 0) return@zip
var curFont = startGlyphs.getFont(0)
var drawStart = 0
for (i in 1 until startGlyphs.glyphCount()) {
// Assume the pair of Glyph ID and font is the same. If different, you may want
// to animate in a different way, e.g. cross fading.
require(startGlyphs.getGlyphId(i) == endGlyphs.getGlyphId(i))
require(startGlyphs.getFont(i) === endGlyphs.getFont(i))
val font = startGlyphs.getFont(i)
if (curFont != font) {
drawGlyphs(canvas, startGlyphs, endGlyphs, drawStart, i, curFont, paint)
curFont = font
drawStart = i
}
}
if (drawStart != startGlyphs.glyphCount() - 1) {
drawGlyphs(canvas, startGlyphs, endGlyphs, drawStart, startGlyphs.glyphCount(),
curFont, paint)
}
}
}
// Draws Glyphs for the same font run.
private fun drawGlyphs(canvas: Canvas, startGlyph: PositionedGlyphs,
endGlyph: PositionedGlyphs, start: Int, end: Int, font: Font,
paint: Paint) {
var cacheIndex = 0
for (i in start until end) {
intArrayCache[cacheIndex] = startGlyph.getGlyphId(i)
// The glyph positions are different from start to end since they are shaped
// with different letter spacing. Use linear interpolation for positions
// during animation.
floatArrayCache[cacheIndex * 2] =
lerp(startGlyph.getGlyphX(i), endGlyph.getGlyphX(i), progress)
floatArrayCache[cacheIndex * 2 + 1] =
lerp(startGlyph.getGlyphY(i), endGlyph.getGlyphY(i), progress)
if (cacheIndex == CACHE_SIZE) { // Cached int array is full. Flashing.
canvas.drawGlyphs(
intArrayCache, 0, // glyphID array and its starting offset
floatArrayCache, 0, // position array and its starting offset
cacheIndex, // glyph count
font,
paint
)
cacheIndex = 0
}
cacheIndex++
}
if (cacheIndex != 0) {
canvas.drawGlyphs(
intArrayCache, 0, // glyphID array and its starting offset
floatArrayCache, 0, // position array and its starting offset
cacheIndex, // glyph count
font,
paint
)
}
}
// Linear Interpolator
private fun lerp(start: Float, end: Float, t: Float) = start * (1f - t) + end * t
private fun lerp(start: Int, end: Int, t: Float) = (start * (1f - t) + end * t).toInt()
// The animation progress.
var progress: Float = 0f
set(value) {
field = value
invalidate()
}
// working copy of paint.
private val paint = Paint()
// Array cache for reducing allocation during drawing.
private var intArrayCache = IntArray(CACHE_SIZE)
private var floatArrayCache = FloatArray(CACHE_SIZE * 2)
}
</code>
Java documentation pour android.text.TextShaper.
Les parties de cette page sont des modifications basées sur le travail créé et partagé par Android Open Source et utilisées en fonction des termes décrits dans la Creative Commons 2.5 Attribution License.
Constructeurs
| Nom | Description |
|---|---|
| TextShaper(IntPtr, JniHandleOwnership) |
Fournit une mise en forme de texte pour le texte à styles multiples. |
Propriétés
| Nom | Description |
|---|---|
| Class |
Retourne la classe runtime de ce |
| Handle |
Handle de l’instance Android sous-jacente. (Hérité de Object) |
| JniIdentityHashCode |
Obtient le code de hachage d’identité affecté à cet homologue Java par le runtime d’interopérabilité. (Hérité de Object) |
| JniManagedPeerState |
Fournit une mise en forme de texte pour le texte à styles multiples. (Hérité de JavaObject) |
| JniPeerMembers |
Fournit une mise en forme de texte pour le texte à styles multiples. |
| PeerReference |
Obtient la référence d’objet JNI pour cet homologue Java. (Hérité de Object) |
| ThresholdClass |
Fournit une mise en forme de texte pour le texte à styles multiples. |
| ThresholdType |
Fournit une mise en forme de texte pour le texte à styles multiples. |
Méthodes
| Nom | Description |
|---|---|
| Clone() |
Crée et retourne une copie de cet objet. (Hérité de Object) |
| Construct(JniObjectReference, JniObjectReferenceOptions) |
Fournit une mise en forme de texte pour le texte à styles multiples. (Hérité de JavaObject) |
| Dispose() |
Libère les ressources détenues par cet homologue Java. (Hérité de Object) |
| Dispose(Boolean) |
Libère les ressources détenues par cet homologue Java. (Hérité de Object) |
| DisposeUnlessReferenced() |
Fournit une mise en forme de texte pour le texte à styles multiples. (Hérité de JavaObject) |
| Equals(Object) |
Fournit une mise en forme de texte pour le texte à styles multiples. (Hérité de JavaObject) |
| Equals(Object) |
Indique si un autre objet est « égal à » celui-ci. (Hérité de Object) |
| GetHashCode() |
Retourne une valeur de code de hachage pour l’objet. (Hérité de Object) |
| JavaFinalize() |
Appelé par le garbage collector sur un objet lorsque le garbage collection détermine qu’il n’y a plus de références à l’objet. (Hérité de Object) |
| Notify() |
Réveille un thread unique qui attend le moniteur de cet objet. (Hérité de Object) |
| NotifyAll() |
Réveille tous les threads qui attendent le moniteur de cet objet. (Hérité de Object) |
| SetHandle(IntPtr, JniHandleOwnership) |
Définit la propriété Handle. (Hérité de Object) |
| SetPeerReference(JniObjectReference, JniObjectReferenceOptions) |
Fournit une mise en forme de texte pour le texte à styles multiples. (Hérité de JavaObject) |
| ShapeText(ICharSequence, Int32, Int32, ITextDirectionHeuristic, TextPaint, TextShaper+IGlyphsConsumer) |
Formez du texte à styles multiples. |
| ShapeText(String, Int32, Int32, ITextDirectionHeuristic, TextPaint, TextShaper+IGlyphsConsumer) |
Formez du texte à styles multiples. |
| ToArray<T>() |
Crée un tableau managé à partir de ce wrapper de tableau Java. (Hérité de Object) |
| ToString() |
Retourne une représentation sous forme de chaîne de l’objet. (Hérité de Object) |
| UnregisterFromRuntime() |
Annule l’inscription de cet homologue Java à partir du runtime d’interopérabilité. (Hérité de Object) |
| Wait() |
Provoque l’attente du thread actuel jusqu’à ce qu’il soit réveillé, généralement en étant <averti par em ou><em>interrompu</em>.<> (Hérité de Object) |
| Wait(Int64, Int32) |
Provoque l’attente du thread actuel jusqu’à ce qu’il soit réveillé, généralement en étant <averti> par< em>ou <em>interrompu/em<,> ou jusqu’à ce qu’une certaine quantité de temps réel s’est écoulée. (Hérité de Object) |
| Wait(Int64) |
Provoque l’attente du thread actuel jusqu’à ce qu’il soit réveillé, généralement en étant <averti> par< em>ou <em>interrompu/em<,> ou jusqu’à ce qu’une certaine quantité de temps réel s’est écoulée. (Hérité de Object) |
Implémentations d’interfaces explicites
| Nom | Description |
|---|---|
| IJavaPeerable.Disposed() |
Fournit une mise en forme de texte pour le texte à styles multiples. (Hérité de JavaObject) |
| IJavaPeerable.Finalized() |
Fournit une mise en forme de texte pour le texte à styles multiples. (Hérité de JavaObject) |
| IJavaPeerable.JniObjectReferenceControlBlock |
Fournit une mise en forme de texte pour le texte à styles multiples. (Hérité de JavaObject) |
| IJavaPeerable.SetJniIdentityHashCode(Int32) |
Fournit une mise en forme de texte pour le texte à styles multiples. (Hérité de JavaObject) |
| IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) |
Fournit une mise en forme de texte pour le texte à styles multiples. (Hérité de JavaObject) |
| IJavaPeerable.SetPeerReference(JniObjectReference) |
Fournit une mise en forme de texte pour le texte à styles multiples. (Hérité de JavaObject) |
Méthodes d’extension
| Nom | Description |
|---|---|
| GetJniTypeName(IJavaPeerable) |
Obtient le nom JNI du type de l’instance |
| JavaAs<TResult>(IJavaPeerable) |
Essayez de forcer |
| JavaCast<TResult>(IJavaObject) |
Effectue une conversion de type vérifiée par le runtime Android. |
| JavaCast<TResult>(IJavaObject) |
Fournit une mise en forme de texte pour le texte à styles multiples. |
| TryJavaCast<TResult>(IJavaPeerable, TResult) |
Essayez de forcer |