語言

TextShaper 類別

定義

提供多樣式文字的文字形狀。

[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
繼承
TextShaper
屬性

備註

提供多樣式文字的文字形狀。

這裡有一個簡單文字的文字大小與字距動畫範例。

<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&lt;Pair&lt;PositionedGlyphs, DrawStyle&gt;&gt;()
                private val end = mutableListOf&lt;Pair&lt;PositionedGlyphs, DrawStyle&gt;&gt;()

                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 文件 android.text.TextShaper

本頁部分內容為基於 Open Source Project 所創建與分享的作品,並依授權條款所描述的使用進行修改。

建構函式

名稱 Description
TextShaper(IntPtr, JniHandleOwnership)

提供多樣式文字的文字形狀。

屬性

名稱 Description
Class

回傳此 Object的執行時類別。

(繼承來源 Object)
Handle

底層 Android 實例的帳號。

(繼承來源 Object)
JniIdentityHashCode

取得由互通執行時指派給此 Java 對等端的身份雜湊碼。

(繼承來源 Object)
JniManagedPeerState

提供多樣式文字的文字形狀。

(繼承來源 JavaObject)
JniPeerMembers

提供多樣式文字的文字形狀。

PeerReference

取得這個 Java 節點的 JNI 物件參考。

(繼承來源 Object)
ThresholdClass

提供多樣式文字的文字形狀。

ThresholdType

提供多樣式文字的文字形狀。

方法

名稱 Description
Clone()

建立並回傳此物件的副本。

(繼承來源 Object)
Construct(JniObjectReference, JniObjectReferenceOptions)

提供多樣式文字的文字形狀。

(繼承來源 JavaObject)
Dispose()

釋放該 Java 節點所持有的資源。

(繼承來源 Object)
Dispose(Boolean)

釋放該 Java 節點所持有的資源。

(繼承來源 Object)
DisposeUnlessReferenced()

提供多樣式文字的文字形狀。

(繼承來源 JavaObject)
Equals(Object)

提供多樣式文字的文字形狀。

(繼承來源 JavaObject)
Equals(Object)

表示是否有其他物體「等同」於此物。

(繼承來源 Object)
GetHashCode()

回傳物件的雜湊碼值。

(繼承來源 Object)
JavaFinalize()
已淘汰.

當垃圾回收判定該物件不再有相關參考時,由垃圾回收器呼叫。

(繼承來源 Object)
Notify()

喚醒一個正在該物件監視器上等待的執行緒。

(繼承來源 Object)
NotifyAll()

喚醒所有等待該物件監視器的執行緒。

(繼承來源 Object)
SetHandle(IntPtr, JniHandleOwnership)

設定 Handle 屬性。

(繼承來源 Object)
SetPeerReference(JniObjectReference, JniObjectReferenceOptions)

提供多樣式文字的文字形狀。

(繼承來源 JavaObject)
ShapeText(ICharSequence, Int32, Int32, ITextDirectionHeuristic, TextPaint, TextShaper+IGlyphsConsumer)

多樣式文字形狀。

ShapeText(String, Int32, Int32, ITextDirectionHeuristic, TextPaint, TextShaper+IGlyphsConsumer)

多樣式文字形狀。

ToArray<T>()

從這個 Java 陣列包裝器建立一個受管理陣列。

(繼承來源 Object)
ToString()

回傳物件的字串表示。

(繼承來源 Object)
UnregisterFromRuntime()

將此 Java 節點從互通執行時中取消註冊。

(繼承來源 Object)
Wait()

導致目前執行緒等待被喚醒,通常是透過 <em>通知</><em 或 em>中斷</em> 來喚醒。

(繼承來源 Object)
Wait(Int64, Int32)

會讓目前執行緒等待喚醒,通常是透過 <em>通知</><em 或 em>中斷</em>,或是經過一定的真實時間。

(繼承來源 Object)
Wait(Int64)

會讓目前執行緒等待喚醒,通常是透過 <em>通知</><em 或 em>中斷</em>,或是經過一定的真實時間。

(繼承來源 Object)

明確介面實作

名稱 Description
IJavaPeerable.Disposed()

提供多樣式文字的文字形狀。

(繼承來源 JavaObject)
IJavaPeerable.Finalized()

提供多樣式文字的文字形狀。

(繼承來源 JavaObject)
IJavaPeerable.JniObjectReferenceControlBlock

提供多樣式文字的文字形狀。

(繼承來源 JavaObject)
IJavaPeerable.SetJniIdentityHashCode(Int32)

提供多樣式文字的文字形狀。

(繼承來源 JavaObject)
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates)

提供多樣式文字的文字形狀。

(繼承來源 JavaObject)
IJavaPeerable.SetPeerReference(JniObjectReference)

提供多樣式文字的文字形狀。

(繼承來源 JavaObject)

擴充方法

名稱 Description
GetJniTypeName(IJavaPeerable)

取得實例 self類型的 JNI 名稱。

JavaAs<TResult>(IJavaPeerable)

試著強制self輸入 TResult,檢查 強制在 Java 端是否有效。

JavaCast<TResult>(IJavaObject)

執行 Android 執行時檢查型別轉換。

JavaCast<TResult>(IJavaObject)

提供多樣式文字的文字形狀。

TryJavaCast<TResult>(IJavaPeerable, TResult)

試著強制self輸入 TResult,檢查 強制在 Java 端是否有效。

適用於