Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw text inside custom view?

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.

Custom View - Speech Bubble

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

Thanks.

like image 707
Saeed-rz Avatar asked May 31 '13 16:05

Saeed-rz


People also ask

How to draw custom view in Android?

The most important step in drawing a custom view is to override the onDraw() method. The parameter to onDraw() is a Canvas object that the view can use to draw itself. The Canvas class defines methods for drawing text, lines, bitmaps, and many other graphics primitives.

What is a custom view in Android?

A well-designed custom view is much like any other well-designed class. It encapsulates a specific set of functionality with an easy to use interface, it uses CPU and memory efficiently, and so on. In addition to being a well-designed class, though, a custom view should: Conform to Android standards.

How to create custom shape in Android studio?

Launch Android Studio 3.6. 1 or later and select Open an existing Android Studio project. Then navigate to and select the starter project folder where you'll find the files you need to start, along with some widgets. Your app already has its basic UI set up so you can focus on drawing custom shapes in Android.


2 Answers

something like this?

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

edit:

then why not something like this

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

edit nr2:

why not add a rect?

path.addRect(left, top, right, bottom, Direction.CW);

or

path.addRect(rect, Direction.CW);
like image 174
mc_fish Avatar answered Sep 19 '22 14:09

mc_fish


I created a rectangle in the speech bubble like so

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

Limited the draw area

canvas.cliprect(rect);

And draw the text inside the speech bubble like this

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

Thanks mc_fish.

like image 30
Saeed-rz Avatar answered Sep 22 '22 14:09

Saeed-rz