2012-12-01 36 views
4

我想以編程方式在圓圈或半圓中添加文字,而不是使用具有線邊緣的圓圈,邊緣是文字。查看圖片以獲得更好的解釋。如何在Android中以圓形圖案的方式繪製文字到視圖

text circle

我怎樣才能做到這在Android中,我也可以爲了幫我解決這個問題讀什麼資源?

+0

它會幫助你http://mobile.android.com/blog/272/android-custom-ui-making-a-vintage-thermometer/ –

+0

[一個強大的工具](http://developer.android.com/參考/機器人/圖形/ Path.html) –

回答

2

爲了做到這一點,您需要將文字繪製到Canvas上。 View的任何子類都會通過Canvas,onDraw(),您可以使用它來繪製自定義文本。方法drawTextOnPath()可讓您將文字放在您選擇的任何Path對象上。您可以通過創建新實例並使用addArc()來創建半圓路徑。

1

你可以使用下面的代碼。並使其成爲你想要的Textview。 在這裏,如果你想要的東西作爲Backgroung圖像,然後使用setBackgroundResource(R.drawable.YOUR_IMAGE);

public class MainActivity extends Activity { 
      @Override 
     public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
      setContentView(new GraphicsView(this));} 

     static public class GraphicsView extends View { 
     private static final String QUOTE = "text in a half-circle"; 
     private Path circle; 
     private Paint cPaint; 
     private Paint tPaint; 

    public GraphicsView(Context context) { 
     super(context); 

     int color = Color.argb(127, 255, 0, 255); 

     circle = new Path(); 
     circle.addCircle(230, 350, 150, Direction.CW); 

     cPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 
     cPaint.setStyle(Paint.Style.STROKE); 
     cPaint.setColor(Color.LTGRAY); 
     cPaint.setStrokeWidth(3); 

     // For Background Image 
    setBackgroundResource(R.drawable.YOUR_IMAGE); 

     tPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 
     tPaint.setStyle(Paint.Style.FILL_AND_STROKE); 
    //TextColor you want to set 
     tPaint.setColor(Color.BLACK); 
     //TextSize you want to set 
     tPaint.setTextSize(50);} 


     @Override 
     protected void onDraw(Canvas canvas) { 
     canvas.drawTextOnPath(QUOTE, circle, 485, 20, tPaint);} 
              } 
} 

嘗試一下。希望它能幫助你。

相關問題