我想在我的android应用程序中显示圆形文本。我知道它是用custome textview完成的,但是一些朋友可以给我正确的代码。我还附上了我想要的外观类型的图像。
7 回答
您可以尝试这个经过测试且完整的工作代码:
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);
}
}
}
输出将是:
希望这有帮助。
创建自定义视图并覆盖 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)
您必须像这样创建自己的对象“视图”
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 :) 希望对您有所帮助
您可以在 SDK 目录中查看与 android SDK 捆绑在一起的 APIDemo 源代码。
示例“图形/文本对齐”(文件 TextAlign.java)显示了如何沿路径显示文本:
然后你可以调整它来构建你的屏幕。
您已经使用自定义视图来实现它,例如在 onDraw 方法中:
Path path = new Path();
path.addCircle(x, y, radius, Path.Direction.CW);
myCanvas.drawTextOnPath(myText, path, offset, 0, myPaint);
试试下面的代码..
onDraw(Canvas canvas){
Path path = new Path();
path.addArc(oval, startAngle, sweepAngle)
path.addArc(oval, 0, 180)
}
这可能有效....
你不需要做自定义视图。找到正确TypeFace
的,放入您的项目并设置为您的TextView
或使用 microsoft office word art 并将图像复制到您的项目