2012-10-31 32 views
12

我想在我的android應用程序中顯示具有圓形形狀的文本。我知道這是用custome textview完成的,但可以讓一些好友給我正確的代碼。我也附加了圖像的類型看我想要。如何在Android中使用TextView顯示圓形文本

enter image description here

+0

看看http://stackoverflow.com/questions/10150642/draw-text-in-circular-view –

+0

抱歉,但它顯示在cicle中的完整文本。 – yokees

+3

您需要了解它並根據您的需要進行更改。 –

回答

-8

你不需要進行自定義視圖。 找到正確TypeFace,把你的項目,並設置爲您TextView

或者使用Microsoft Office Word藝術和圖像複製到項目

enter image description here

+0

對不起帥哥這是不可能的,因爲型面只給你文字的風格而不是弧形。 – yokees

+0

看到我的更新與圖片 – Yahor10

+0

親愛的我想在運行時顯示這種風格。我也可以使用這種風格的圖像。但我從來不想要它。 – yokees

5

創建自定義視圖和重寫的onDraw。在onDraw中,創建一個路徑並使用drawTextOnPath。像這樣的東西。 textToDraw是你想要顯示的文本。油漆是你想要的任何油漆。

@Override onDraw(Canvas canvas){ 
    Path path = new Path(); 
    path.addCircle(x, y, 200, Path.Direction.CW); 
    canvas.drawTextOnPath(textToDraw, path, textX, textY, paint); 
} 

http://developer.android.com/training/custom-views/index.html http://developer.android.com/reference/android/graphics/Canvas.html#drawTextOnPath(java.lang.String, android.graphics.Path, float, float, android.graphics.Paint)

+0

另外,對於多分辨率,使用底層繪圖的高度來計算半徑。 –

+0

http://www.networketiquette.net/do_not_use_all_caps.htm現在,恢復,「不工作」是什麼意思? – Simon

+0

我把這個鱈魚放在doDraw()中,但沒有在屏幕上顯示canvas.save(); \t \t Path path = new Path(); \t path.addCircle(-10,10,200,Path.Direction.CW); \t canvas.drawTextOnPath(「Hello」,path,-10,-10,null); \t super.onDraw(canvas); \t canvas.restore(); – yokees

3

你必須使用自定義視圖就像在你的onDraw方法實現:

Path path = new Path(); 
path.addCircle(x, y, radius, Path.Direction.CW); 
myCanvas.drawTextOnPath(myText, path, offset, 0, myPaint); 
+0

什麼是x,y和路徑,在這裏偏移? –

1

試試下面的代碼..

onDraw(Canvas canvas){ 
Path path = new Path(); 
path.addArc(oval, startAngle, sweepAngle) 
path.addArc(oval, 0, 180) 
} 

這可能工作...

2

你必須創建自己的對象的「查看」像這樣

public class YourView extends View { 
    private static final String YOUR_TEXT = "something cool"; 
    private Path _arc; 

    private Paint _paintText; 

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

     _arc = new Path(); 
     RectF oval = new RectF(50,100,200,250);; 
     _arc.addArc(oval, -180, 200); 
     _paintText = new Paint(Paint.ANTI_ALIAS_FLAG); 
     _paintText.setStyle(Paint.Style.FILL_AND_STROKE); 
     _paintText.setColor(Color.WHITE); 
     _paintText.setTextSize(20f); 

    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     canvas.drawTextOnPath(YOUR_TEXT, _arc, 0, 20, _paintText); 
     invalidate(); 
    } 
} 

,然後用它作爲你的TextView :)希望這有助於

4

你可以看看與捆綁的APIDemo源代碼SDK中的android SDK目錄。

的例子「圖形/文本對齊」(文件TextAlign.java)展示瞭如何沿着路徑顯示文本: Screen capture of the path

然後,您可以調整它來建立你的屏幕。

+0

感謝您提供來自API示例的圖像。它節省了創建和運行項目的時間。 – AndroidGuy

+0

https://github.com/appium/android-apidemos/blob/master/src/io/appium/android/apis/graphics/TextAlign.java – azurh

16

你可以試試這個測試和充分的工作代碼:

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 = "This is a curved text"; 
    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); 

    setBackgroundResource(R.drawable.heart); 

    tPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 
    tPaint.setStyle(Paint.Style.FILL_AND_STROKE); 
    tPaint.setColor(Color.BLACK); 
    tPaint.setTextSize(50);} 


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

輸出將是:

enter image description here

希望這有助於。

相關問題