6

我创建了自定义视图以在内部绘制自定义语音气泡,并且我想在自定义视图上放置一些文本。我使用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);

谢谢。

4

2 回答 2

4

像这样的东西?

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);
于 2013-05-31T16:16:48.287 回答
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。

于 2013-07-03T04:49:50.957 回答