在画布上添加文本到路径

时间:2015-06-04 10:18:15

标签: android android-canvas

我正在尝试在Canvas上绘制文本但使用路径。

我有一个文本类,然后我为文本创建了一个新路径,并将此路径添加到所有对象的路径中。当我尝试添加文本时,它不会打印在屏幕上。

我不知道会出现什么问题,但我没有找到太多关于如何在路径上绘制文字的文档,我不知道该怎么做。

protected void onDraw(Canvas canvas){
    for(Path p : paths){
        canvas.drawPath(p, drawPaint);
    }
    canvas.drawPath(drawPath, drawPaint); 
}

//I skip the ontouch event and only write here the action up

case MotionEvent.ACTION_UP:
      Path txtPath=new Path();
      TextPaint txt=new TextPaint();
      txt.setPaint(drawPaint);
      txt.setText(text);
      txt.setX(x);
      txt.setY(y);
      txt.setPath(txtPath);
      drawPath.addPath(txtPath, x, y); 

//This is the class for the text
public static class TextPaint{
   public String text;
   public float x;
   public float y;
   public Paint paint;
   public String selector ;
   public Path path;

   public String getSelector() {
       return selector;
   }

   public void setSelector(String selector) {
       this.selector = selector;
   }

   public Path getPath() {
       return path;
   }

   public void setPath(Path path) {
       this.path = path;
   }

   public float getX() {
       return x;
   }

   public void setX(float x) {
       this.x = x;
   }

   public float getY() {
       return y;
   }

   public void setY(float y) {
       this.y = y;
   }

   public Paint getPaint() {
       return paint;
   }

   public void setPaint(Paint paint) {
       this.paint = paint;
   }

   public String getText() {

       return text;
   }

   public void setText(String text) {
       this.text = text;
   }

  }    

0 个答案:

没有答案