Java 图形形状和文字是否需要单独的绘制对象

Java 图形形状和文字是否需要单独的绘制对象,java,android,android-drawable,android-paint,Java,Android,Android Drawable,Android Paint,我有以下自定义绘图功能,但我需要定义两个绘制文本和形状的绘制对象: public class LetterOvalDrawable extends Drawable { private final Paint textPaint; private final Paint shapePaint; private final Rect textRect; private String letter; public LetterOvalDrawable() {

我有以下自定义绘图功能,但我需要定义两个绘制文本和形状的绘制对象:

public class LetterOvalDrawable
    extends Drawable {

  private final Paint textPaint;
  private final Paint shapePaint;
  private final Rect textRect;
  private String letter;

  public LetterOvalDrawable() {
    textPaint = new Paint();
    shapePaint = new Paint();
    textPaint.setAntiAlias(true);
    shapePaint.setAntiAlias(true);
    textPaint.setTextAlign(Paint.Align.LEFT);
    textRect = new Rect();
  }

  public LetterOvalDrawable(float textSize, @NonNull Typeface typeFace) {
    textPaint = new Paint();
    shapePaint = new Paint();
    textPaint.setAntiAlias(true);
    shapePaint.setAntiAlias(true);
    textPaint.setTextAlign(Paint.Align.LEFT);
    textRect = new Rect();

    textPaint.setTextSize(textSize);
    textPaint.setTypeface(typeFace);
  }

  @Override
  public void draw(@NonNull Canvas canvas) {
    int height = getBounds().height();
    int width = getBounds().width();
    RectF rect = new RectF(0.0f, 0.0f, width, height);
    canvas.drawRoundRect(rect, rect.bottom, rect.right, shapePaint);

    if (letter == null) return;

    textPaint.getTextBounds(letter, 0, letter.length(), textRect);
    float x = width / 2f - textRect.width() / 2f - textRect.left;
    float y = height / 2f + textRect.height() / 2f - textRect.bottom;
    canvas.drawText(letter, x, y, textPaint);
  }

  @Override
  public void setAlpha(int alpha) {
    textPaint.setAlpha(alpha);
    shapePaint.setAlpha(alpha);
  }

  @Override
  public void setColorFilter(@Nullable ColorFilter colorFilter) {
    shapePaint.setColorFilter(colorFilter);
  }

  @Override
  public int getOpacity() {
    return PixelFormat.TRANSLUCENT;
  }

  public void setLetter(String letter) {
    if (letter == null || letter.length() < 1) {
      this.letter = null;
      return;
    }
    if (letter.length() == 1) this.letter = letter.toUpperCase();
    else this.letter = letter.substring(0, 1).toUpperCase();
  }
}
公共类LetterOvalDrawable
可拉伸{
私人最终油漆;
私人最终油漆成型漆;
私有最终Rect textRect;
私人字符串字母;
公函可提取(){
text油漆=新油漆();
shapePaint=新油漆();
textPaint.setAntiAlias(true);
shapePaint.setAntiAlias(true);
textPaint.setTextAlign(Paint.Align.LEFT);
textRect=新的Rect();
}
public LetterOvalDrawable(浮动文本大小,@NonNull字体){
text油漆=新油漆();
shapePaint=新油漆();
textPaint.setAntiAlias(true);
shapePaint.setAntiAlias(true);
textPaint.setTextAlign(Paint.Align.LEFT);
textRect=新的Rect();
textPaint.setTextSize(textSize);
textPaint.setTypeface(字体);
}
@凌驾
公共空白绘制(@NonNull画布){
int height=getBounds().height();
int width=getBounds().width();
RectF rect=新的RectF(0.0f,0.0f,宽度,高度);
canvas.drawRoundRect(rect,rect.bottom,rect.right,shapePaint);
如果(字母==null)返回;
textPaint.getTextBounds(letter,0,letter.length(),textRect);
float x=width/2f-textRect.width()/2f-textRect.left;
浮动y=height/2f+textRect.height()/2f-textRect.bottom;
画布.drawText(字母、x、y、textPaint);
}
@凌驾
公共void setAlpha(int-alpha){
textPaint.setAlpha(alpha);
shapePaint.setAlpha(alpha);
}
@凌驾
public void setColorFilter(@Nullable ColorFilter ColorFilter){
setColorFilter(colorFilter);
}
@凌驾
公共int getOpacity(){
返回像素格式。半透明;
}
公共无效设置字母(字符串字母){
if(letter==null | | letter.length()<1){
this.letter=null;
返回;
}
如果(letter.length()==1)this.letter=letter.toUpperCase();
else this.letter=letter.substring(0,1).toUpperCase();
}
}
我不喜欢定义两个绘画对象。是否可以重构此代码,以便只有一个Paint对象


或者在安卓系统中,是否必须拥有这两个对象?如果这是一个基本问题,请原谅,我是android新手,在文档中找不到任何东西。

使用
TextPaint
类而不是
Paint
,有什么好处吗?