我创建了自定义视图以在其中绘制自定义语音气泡 我想在自定义视图上放一些文字。我使用 drawTextOnPath 但它无法正常工作,我希望文本逐行显示。
自定义视图 - 语音气泡
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setAntiAlias(true);
paint.setStrokeWidth(2);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setPathEffect(new CornerPathEffect(15) );
paint.setStyle(Paint.Style.FILL_AND_STROKE);
paint.setDither(true);
paint.setShader(new LinearGradient(0, 0, 0, getHeight(), Color.BLACK, Color.WHITE,
Shader.TileMode.MIRROR));
Path path = new Path();
paint.setShadowLayer(4, 2, 2, 0x80000000);
path.moveTo(myPath[0].x, myPath[0].y);
for (int i = 1; i < myPath.length; i++){
path.lineTo(myPath[i].x, myPath[i].y);
}
path.close();
canvas.clipPath(path);
canvas.drawPath(path, paint);
canvas.save();
paint = new Paint();
paint.setColor(Color.WHITE);
canvas.drawTextOnPath(MessageBody, path, 10, 10, paint);
感谢。
答案 0 :(得分:3)
这样的事情?
Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setStyle(Style.FILL);
canvas.drawPaint(paint);
...
//tell the paint of the new color
paint.setColor(android.R.color.black);
paint.setTextSize(20);
canvas.drawText("Some Text", 10, 25, paint);
编辑:
然后为什么不这样呢
Path path = new Path();
path.addCircle(width/2, height/2, radius, Path.Direction.CW);
Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setTextSize(20);
canvas.drawTextOnPath("Some Text", path, 0, 0, paint);
编辑nr2:
为什么不添加一个矩形?
path.addRect(left, top, right, bottom, Direction.CW);
或
path.addRect(rect, Direction.CW);
答案 1 :(得分:1)
我在讲话泡泡中创建了一个矩形,如此
Rect rcText;
rcText = new Rect(rcBounds);//rcBounds is y speech bubble rect
canvas.save();
限制绘制区域
canvas.cliprect(rect);
然后像这样在语音气泡中画出文字
canvas.drawtext(mytext,rect.left,rect.top,paint);
canvas.restore();
谢谢mc_fish。