Android 绘制具有特定文字外观的文字
如何使用特定的Android 绘制具有特定文字外观的文字,android,paint,Android,Paint,如何使用特定的文本外观绘制文本 这是我的密码 Paint paint = new Paint(); paint.setAntiAlias(true); paint.setColor(Color.WHITE); paint.setFakeBoldText(false); paint.setTextSize(textSize); // TODO: set the style of the text to a specific TextAppearanc
文本外观绘制文本
这是我的密码
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
paint.setFakeBoldText(false);
paint.setTextSize(textSize);
// TODO: set the style of the text to a specific TextAppearance. E.g. @android:style/TextAppearance.DeviceDefault.Large.Inverse
canvas.drawText(context.getResources().getString(R.string.myString), textOffsetX, textOffsetY, paint);
我已经研究了TypeFace
对象,但我不确定如何从文本外观创建字体 一位同事给了我一个解决方案,它使用TextView
作为TextAppearance
TextView textView = new TextView(context);
textView.setTextAppearance(context, android.R.style.TextAppearance.DeviceDefault.Large);
paint.setColor(textView.getCurrentTextColor());
paint.setTextSize(textView.getTextSize());
paint.setTypeface(textView.getTypeface());
基于响应,我找到了一个适用于所有API版本的解决方案。您必须使用AppCompatTextView
和textview compat.settextearance
:
TextView textView = new AppCompatTextView(getContext());
TextViewCompat.setTextAppearance(textView, styleRes);
//Just get paint from textview like janosch suggests
paint = textView.getPaint();
//Or get some info from textview
paint.setColor(textView.getCurrentTextColor());
paint.setTextSize(textView.getTextSize());
paint.setTypeface(textView.getTypeface());
或者您可以只调用textView.getPaint()
。这是一个很好的开始,但是您必须使用AppCompatTextView
和TextViewCompat.setTextAppearance
才能应用字体。