본문 바로가기
프로그래밍 놀이터/안드로이드, Java

[android] Pie (POS) 에서의 text

by 돼지왕 왕돼지 2019. 7. 25.
반응형

[android] Pie (POS) 에서의 text


https://android-developers.googleblog.com/2018/07/whats-new-for-text-in-android-p.html


android pie text, baseline text alignment, bg thread cache, bg thread layout, bg thread measure, cache, firstBaselineToTopHeight, jetpack, jetpack precomputedtext, lastBaselineToBottomHeight, layout, line height, lineHeight, lineSpacingExtra, lineSpacingMultiplier, magnifier, measure, ml algorithm, pos text, precomputedtext, PrecomputedText.create, precomputedtextcompat api 14, Regex, smart linkify, surface zoom in, text link, textclassifier, textview layout, textview measure, textview performance, textview 성능 저하, view zoom in, [android] Pie (POS) 에서의 text


PrecomputedText


-

TextView 에서 text 의 measure, layout 에 엄청 많은 일을 한다.

font file 을 읽고, 상형문자를 찾고, shape 를 결정하고, bounding box 를 measure 하고, cache 를 한다.

그리고 이 모든 일들은 UI Thread 에서 이루어진다. ( frame 하락을 가져온다. )


이 중에서 text measuring 이 90% 시간을 차지한다.

이 문제를 해결하기 위해 Jetpack 의 일부로 PrecomputedText 가 들어갔다.

이 API 는 PrecomputedTextCompat 이라는 이름으로 API 14(ICS) 부터 사용가능하다.



-

이 녀석을 사용하면 text layout 관련된 시간이 드는 대부분의 작업(layout, cache, measure)을 미리 수행할 수 있다.

이것은 bg thread 에서도 할 수 있다.



-

PrecomputedText.create(CharSequence, params) 의 결과물은 TextView 에 바로 assign 될 수 있다.

TextView 는 기존 대비 10% 정도의 일만 하면 된다.



-

val params: PrecomputedText.Params = textView.getTextMetricsParams()
val ref = WeakReference(textView)
executor.execute {
    // background thread
    val text = PrecomputedText.create("Hello", params)
    val textView = ref.get()
    textView?.post {
        // UI thread
        val textViewRef = ref.get()
        textViewRef?.text = text
    }
}



Magnifier


-

Magnifier 라는 widget 은 window 에 붙어 있는 어떤 것이든 적용될 수 있다.

이 녀석은 view 나 surface 에 zoomed-in version 을 제공할 수 있다.



-

Magnifier 는 3가지의 메인 메소드를 가진다.

show, update, dismiss

fun onTouchEvent(event: MotionEvent): Boolean {
    when (event.actionMasked) {
        MotionEvent.ACTION_DOWN -> 
              magnifier.show(event.x, event.y)
        MotionEvent.ACTION_MOVE -> 
             magnifier.show(event.x, event.y)
        MotionEvent.ACTION_UP -> 
             magnifier.dismiss()
    }
}






Smart Linkify


-

Linkify 는 regex 를 사용하여 text 에 link 를 넣는 기능이며 API 1 부터 있었다.

그러나 Physical address 를 찾는 과정이 WebView 의 성능 하향을 가져오곤 했다.

이를 개선하기 위해서 TextClassifier API 를 쓰면 된다.



-

Smart Linkify 는 ML algorithm 을 사용해서 text 에서 link 할 것을 찾아낸다.

Smart Linkify 는 어떤 action 을 취해야 하는지를 알아서 인식해준다.

예를 들어 phone number 를 인식해서 메시지 보내기, 전화하기, 연락처에 저장하기 등의 액션을 추천한다.



-

val text: Spannable = ...
val request = TextLinks.Request.Builder(text)
val ref = WeakReference(textView)
executor.execute {
    // background thread
    TextClassifier.generateLinks(request).apply(text)
    val textView = ref.get()
    textView?.post {
        // UI thread
        val textViewRef = ref.get()
        textViewRef?.text = text
    }
}




Line Height and Baseline Text Alignment


-

P 이전에는 line 사이 간격은 lineSpacingExtra 나 lineSpacingMultiplier 로 조정되었다.

하지만 디자이너들은 대부분 이 대신 line hight 로 표기하곤 한다.

그래서 P 에서는 lineHeight 라는 것이 추가되었다. (내부적으로 보면 이녀석은 lineSpacingExtra 와 lineSpacingMultiplier 를 조작한다.)



-

first, last baseline 의 distance control 을 하기 위해서 firstBaselineToTopHeight, lastBaselineToBottomHeight 가 추가되었다.

firstBaselineToTopHeight 는 TextView 의 top boundary 와 TextVIew 의 첫번째 줄의 baseline 까지의 거리를 나타낸다.

반대로 lastBaselineToBottomHeight 는 TextView 의 bottom boundary 부터 마지막 줄의 baseline 까지의 거리를 나타낸다.




반응형

댓글