如何在自定义视图中绘制文本? [英] How to draw text inside custom view?

查看:86
本文介绍了如何在自定义视图中绘制文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了自定义视图以在其中绘制自定义气泡 我想在自定义视图上放一些文字.我使用了 drawTextOnPath ,但是它不能正常工作,我希望文本逐行显示.

I have created custom view to draw a custom speech bubble inside and I want put some text on custom view. I use drawTextOnPath but it doesn't work right, I want the text to appear line by line.

自定义视图-语音气泡

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);

谢谢.

推荐答案

我像这样在气泡中创建了一个矩形

I created a rectangle in the speech bubble like so

Rect rcText;
rcText = new Rect(rcBounds);//rcBounds is y speech bubble rect
canvas.save();

限制绘画区域

canvas.cliprect(rect);

并像这样在气泡内绘制文本

And draw the text inside the speech bubble like this

canvas.drawtext(mytext,rect.left,rect.top,paint);
canvas.restore();

感谢mc_fish.

这篇关于如何在自定义视图中绘制文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆