簡體   English   中英

如何在Android中以圓形圖案將視圖繪制到視圖中

[英]How can I draw text to a view in a circular pattern in Android

我想以編程方式在圓圈或半圓中添加文本,這樣的方式是不是使用帶有邊線的圓圈,而是邊緣是單詞。 有關更好的解釋,請參見圖像。

文字圈

我怎樣才能在Android中執行此操作,或者我可以閱讀哪些資源來幫助解決此問題?

為此,您需要將文本繪制到Canvas View onDraw()類都在onDraw()中傳遞一個Canvas ,您可以使用它來繪制自定義文本。 方法drawTextOnPath()允許您將文本放在您選擇的任何Path對象上。 您可以通過創建新實例並使用addArc()來創建半圓路徑。

你可以使用下面的代碼。 並按照您的要求制作Textview。 這里如果你想要Something as 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);} 
                                            } 
}

試試看。 希望它會對你有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM