2010-10-28 47 views
26

Tôi đang cố gắng phát triển một lớp biểu đồ hình tròn đơn giản cho android. Bây giờ, nó có thể lấy một bản đồ của các nhãn và các giá trị và vẽ biểu đồ hình tròn. Tôi chưa thêm các truyền thuyết cho chiếc bánh, đó là nơi tôi cần phải đặt các văn bản gần hình chữ nhật nhỏ trên góc màn hình. Bất kỳ trợ giúp nào được đánh giá cao, vì tôi là người mới sử dụng Android dev.Làm cách nào để vẽ văn bản trên canvas?

Trả lời

1

khác (cho là tốt hơn) cách để vẽ văn bản trên một canvas là sử dụng một StaticLayout. Điều này xử lý văn bản nhiều dòng khi cần.

String text = "This is some text."; 

TextPaint textPaint = new TextPaint(); 
textPaint.setAntiAlias(true); 
textPaint.setTextSize(16 * getResources().getDisplayMetrics().density); 
textPaint.setColor(0xFF000000); 

int width = (int) textPaint.measureText(text); 
StaticLayout staticLayout = new StaticLayout(text, textPaint, (int) width, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0, false); 
staticLayout.draw(canvas); 

TextPaintStaticLayout được khởi tạo ngay trước khi được sử dụng ở đây để minh hoạ. Làm như vậy trong onDraw sẽ làm tổn thương hiệu suất, mặc dù. Here is a better example hiển thị chúng trong ngữ cảnh của chế độ xem tùy chỉnh vẽ văn bản riêng của nó.

6

Có một câu trả lời khác ở đây đã bị xóa vì đó chỉ là một liên kết. Liên kết gốc là here. Mã cơ bản là giống nhau, nhưng tôi đã lấy ra các phần bản vẽ không phải văn bản và cũng tăng kích thước để làm việc tốt hơn trên mật độ màn hình hiện đại.

Điều này chỉ hiển thị một vài điều bạn có thể làm với bản vẽ văn bản.

enter image description here

Đây là mã Cập nhật:

public class MainActivity extends AppCompatActivity { 

    DemoView demoview; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     demoview = new DemoView(this); 
     setContentView(demoview); 
    } 

    private class DemoView extends View { 
     public DemoView(Context context){ 
      super(context); 
     } 

     @Override protected void onDraw(Canvas canvas) { 
      super.onDraw(canvas); 

      // custom drawing code here 
      // remember: y increases from top to bottom 
      // x increases from left to right 
      int x = 0; 
      int y = 0; 
      Paint paint = new Paint(); 
      paint.setStyle(Paint.Style.FILL); 

      canvas.save(); 
      canvas.translate(100, 200); 

      // make the entire canvas white 
      canvas.drawColor(Color.WHITE); 

      // draw some text using STROKE style 
      paint.setStyle(Paint.Style.STROKE); 
      paint.setStrokeWidth(1); 
      paint.setColor(Color.MAGENTA); 
      paint.setTextSize(100); 
      canvas.drawText("Style.STROKE", 0, 0, paint); 

      canvas.translate(0, 200); 

      // draw some text using FILL style 
      paint.setStyle(Paint.Style.FILL); 
      //turn antialiasing on 
      paint.setAntiAlias(true); 
      //paint.setTextSize(30); 
      canvas.drawText("Style.FILL", 0, 0, paint); 

      canvas.translate(0, 200); 

      // draw some rotated text 
      // get text width and height 
      // set desired drawing location 
      x = 75; 
      y = 185; 
      paint.setColor(Color.GRAY); 
      //paint.setTextSize(25); 
      String str2rotate = "Rotated!"; 

      // draw bounding rect before rotating text 
      Rect rect = new Rect(); 
      paint.getTextBounds(str2rotate, 0, str2rotate.length(), rect); 
      canvas.translate(x, y); 
      paint.setStyle(Paint.Style.FILL); 
      // draw unrotated text 
      canvas.drawText("!Rotated", 0, 0, paint); 
      paint.setStyle(Paint.Style.STROKE); 
      canvas.drawRect(rect, paint); 
      // undo the translate 
      canvas.translate(-x, -y); 

      // rotate the canvas on center of the text to draw 
      canvas.rotate(-45, x + rect.exactCenterX(), 
        y + rect.exactCenterY()); 
      // draw the rotated text 
      paint.setStyle(Paint.Style.FILL); 
      canvas.drawText(str2rotate, x, y, paint); 

      //undo the translation and rotation 
      canvas.restore(); 
     } 
    } 
} 

Cái gì khác mà tôi muốn thử sau là drawing text along a path.

Xem thêm this fuller answer here cung cấp hình ảnh sau đây.

enter image description here

Các vấn đề liên quan