Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Draw TextView on Canvas in android..?

Tags:

How to Draw TextView on Canvas in android..?

We have Canvas.DrawBitmap(), Canvas.drawText(). Do we have any Method in Canvas which takes TextView as a parameter or any other method to display TextView on Canvas?

Actually, I have a alphabet in TextView and I have to make drawing on that alphabet which is in canvas.

Please suggest anything....Thanks for your cooperation

like image 643
Rajesh Avatar asked Mar 09 '11 07:03

Rajesh


People also ask

What is canvas drawing in Android?

Canvas API is a drawing framework that is provided in Android, with the help of which we can create custom shapes like rectangle, circle, and many more in our UI design. With the help of this API, we can draw any type of shape for our app. The drawing of the different shapes is done using Bitmap.

What is a TextView in Android Studio?

A TextView displays text to the user and optionally allows them to edit it. A TextView is a complete text editor, however the basic class is configured to not allow editing.

What is static layout in Android?

StaticLayout (similar to DynamicLayout and BoringLayout ) is used to layout and draw text on a canvas. It is commonly used for the following tasks: Measuring how big multiline text would be after being laid out.


2 Answers

You can't draw a Textview directly, but you can put it in a layout and draw the layout. Something like this:

LinearLayout layout = new LinearLayout(context);  TextView textView = new TextView(context);  textView.setVisibility(View.VISIBLE); textView.setText("Hello world"); layout.addView(textView);  layout.measure(canvas.getWidth(), canvas.getHeight()); layout.layout(0, 0, canvas.getWidth(), canvas.getHeight());  // To place the text view somewhere specific: //canvas.translate(0, 0);  layout.draw(canvas); 
like image 149
emidander Avatar answered Sep 23 '22 20:09

emidander


May be you need to use StaticLayout. It can draw formatted text, manages word wrapping and so on. Have a look at http://developer.android.com/reference/android/text/StaticLayout.html

like image 39
Ilya Izhovkin Avatar answered Sep 24 '22 20:09

Ilya Izhovkin