绘制具有特定文本外观的文本

时间:2014-10-30 19:07:52

标签: android paint

如何使用特定TextAppearance绘制文字?

这是我的代码。

    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对象,但我不确定如何从TextAppearance创建TypeFace。

2 个答案:

答案 0 :(得分:2)

同事给了我一个解决方案,使用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());

答案 1 :(得分:0)

基于louis.luo的响应,我找到了适用于所有API版本的解决方案。您必须使用AppCompatTextViewTextViewCompat.setTextAppearance

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());