Android: fix text layout npe

This commit is contained in:
王劲鹏 2021-07-27 13:52:36 +08:00 committed by osborn
parent a1c67e2b48
commit 224ec7e761

View File

@ -27,6 +27,7 @@ import android.text.Layout;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.TypedValue; import android.util.TypedValue;
import android.view.Gravity; import android.view.Gravity;
import android.view.ViewTreeObserver;
import android.widget.TextView; import android.widget.TextView;
import androidx.core.content.res.ResourcesCompat; import androidx.core.content.res.ResourcesCompat;
@ -95,9 +96,11 @@ public class TextNode extends ViewNode<TextView> {
if (prop.isNumber()) { if (prop.isNumber()) {
view.setTextColor(prop.asNumber().toInt()); view.setTextColor(prop.asNumber().toInt());
} else if (prop.isObject()) { } else if (prop.isObject()) {
view.post(new Runnable() { view.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override @Override
public void run() { public boolean onPreDraw() {
view.getViewTreeObserver().removeOnPreDrawListener(this);
final JSObject dict = prop.asObject(); final JSObject dict = prop.asObject();
int[] colors = null; int[] colors = null;
@ -164,8 +167,11 @@ public class TextNode extends ViewNode<TextView> {
} }
setGradientTextColor(view, angle, colors, locations); setGradientTextColor(view, angle, colors, locations);
return true;
} }
}); });
view.invalidate();
} }
break; break;
case "textAlignment": case "textAlignment":
@ -321,6 +327,11 @@ public class TextNode extends ViewNode<TextView> {
final Rect textBound = new Rect(Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MIN_VALUE, Integer.MIN_VALUE); final Rect textBound = new Rect(Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MIN_VALUE, Integer.MIN_VALUE);
final Layout layout = textView.getLayout(); final Layout layout = textView.getLayout();
if (layout == null) {
return;
}
for (int i = 0; i < textView.getLineCount(); i++) { for (int i = 0; i < textView.getLineCount(); i++) {
float left = layout.getLineLeft(i); float left = layout.getLineLeft(i);
float right = layout.getLineRight(i); float right = layout.getLineRight(i);