TextView: make sure getPaint().getTextSize() returns the expected value

Using pango_font_description_set_absolute_size() now to prevent previous
rounding errors when converting from pixels to points and back.

This is needed to prevent androidx AppCompatTextViewAutoSizeHelper from
constantly requesting a relayout.
This commit is contained in:
Julian Winkler
2025-10-22 21:14:18 +02:00
parent 377c140d7c
commit 3b69e949d9
2 changed files with 4 additions and 3 deletions

View File

@@ -117,13 +117,13 @@ JNIEXPORT jint JNICALL Java_android_graphics_Paint_native_1get_1stroke_1join(JNI
JNIEXPORT void JNICALL Java_android_graphics_Paint_native_1set_1text_1size(JNIEnv *env, jclass clazz, jlong paint_ptr, jfloat size)
{
struct AndroidPaint *paint = _PTR(paint_ptr);
pango_font_description_set_size(paint->font, size * .8f * PANGO_SCALE);
pango_font_description_set_absolute_size(paint->font, roundf(size * PANGO_SCALE));
}
JNIEXPORT jfloat JNICALL Java_android_graphics_Paint_native_1get_1text_1size(JNIEnv *env, jclass clazz, jlong paint_ptr)
{
struct AndroidPaint *paint = _PTR(paint_ptr);
return pango_font_description_get_size(paint->font) / .8f / PANGO_SCALE;
return (jfloat)pango_font_description_get_size(paint->font) / PANGO_SCALE;
}
JNIEXPORT void JNICALL Java_android_graphics_Paint_native_1set_1color_1filter(JNIEnv *env, jclass clazz, jlong paint_ptr, jint mode, jint color)

View File

@@ -30,6 +30,7 @@ import android.view.View;
public class TextView extends View {
private ColorStateList colors = new ColorStateList(new int[][] {new int[0]}, new int[1]);
private CharSequence text = "";
private TextPaint paint = new TextPaint();
public TextView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
@@ -158,7 +159,7 @@ public class TextView extends View {
public static ColorStateList getTextColors(Context context, TypedArray attrs) { return new ColorStateList(new int[][] {new int[0]}, new int[1]); }
public TextPaint getPaint() {
return new TextPaint();
return paint;
}
public void addTextChangedListener(TextWatcher watcher) {}