Questions tagged [android-canvas]

The Android Java Canvas (android.graphics.Canvas)

This is the tag for the Android Java Canvas, android.graphics.Canvas (official documentation).

If your question is about HTML5 (JavaScript) Canvas on Android devices please tag it and .

3799 questions
229
votes
12 answers

Android Center text on canvas

I'm trying to display a text using the code below. The problem is that the text is not centered horizontally. When I set the coordinates for drawText, it sets the bottom of the text at this position. I would like the text to be drawn so that the…
Sebastian
  • 2,698
  • 2
  • 17
  • 26
157
votes
8 answers

Measuring text height to be drawn on Canvas ( Android )

Any straight forward way to measure the height of text? The way I am doing it now is by using Paint's measureText() to get the width, then by trial and error finding a value to get an approximate height. I've also been messing around with…
Danedo
  • 2,193
  • 5
  • 29
  • 37
137
votes
8 answers

Measuring text width to be drawn on Canvas ( Android )

Is there a method which returns the width ( in pixels ) of a text to be drawn on an Android canvas using the drawText() method according to the Paint used to draw it?
NioX5199
  • 1,772
  • 4
  • 13
  • 12
136
votes
19 answers

Draw multi-line text to Canvas

A hopefully quick question, but I can't seem to find any examples... I'd like to write multi-line text to a custom View via a Canvas, and in onDraw() I have: ... String text = "This is\nmulti-line\ntext"; canvas.drawText(text, 100, 100,…
Unpossible
  • 10,607
  • 22
  • 75
  • 113
124
votes
3 answers

Understanding Canvas and Surface concepts

I'm struggling to understand the process of drawing to SurfaceView and therefore the whole Surface/Canvas/Bitmap system, which is used in Android. I've read all articles and API documentation pages, which I was able to find on android-developers…
Fyodor
  • 2,793
  • 4
  • 21
  • 24
124
votes
6 answers

Draw in Canvas by finger, Android

I need to build a project for drawing on canvas by fingers. How to get the touch event and motion event of my finger and then draw?
Sameer H. Ibra
  • 1,816
  • 2
  • 17
  • 25
119
votes
2 answers

Creating an empty bitmap and drawing though canvas in Android

I'd like to create an empty bitmap and set a canvas to that bitmap and then draw any shape on the bitmap.
Sunil Pandey
  • 7,042
  • 7
  • 35
  • 48
116
votes
7 answers

Android canvas draw rectangle

how to draw empty rectangle with etc. borderWidth=3 and borderColor=black and part within rectangle don't have content or color. Which function in Canvas to use void drawRect(float left, float top, float right, float bottom, Paint paint) void…
Kec
  • 1,435
  • 4
  • 16
  • 21
114
votes
7 answers

Drawable image on a canvas

How can I get an image to a canvas in order to draw on that image?
Lana
  • 1,161
  • 3
  • 8
  • 5
111
votes
21 answers

Android, canvas: How do I clear (delete contents of) a canvas (= bitmaps), living in a surfaceView?

In order to make a simple game, I used a template that draws a canvas with bitmaps like this: private void doDraw(Canvas canvas) { for (int i=0;i<8;i++) for (int j=0;j<9;j++) for (int k=0;k<7;k++) { …
samClem
  • 1,117
  • 2
  • 8
  • 6
109
votes
2 answers

Android Canvas.drawText

I have a view, I'm drawing with the Canvas object in the onDraw(Canvas canvas) method. My code is: Paint paint = new…
Gaz
  • 3,855
  • 6
  • 28
  • 33
95
votes
8 answers

How to draw a filled triangle in android canvas?

So I'm drawing this triangle in android maps using the code below in my draw method: paint.setARGB(255, 153, 29, 29); paint.setStyle(Paint.Style.FILL_AND_STROKE); paint.setAntiAlias(true); Path path = new Path(); path.moveTo(point1_returned.x,…
Pavel
  • 5,213
  • 10
  • 34
  • 47
92
votes
6 answers

Hex colors in Android are sometimes eight digits. How? What is the difference between #FFFFFF and #FFFFFF00?

I sometimes have seen in examples where the coloring in Android is done as #FF191919. I mean an eight-digit hex number. But it should only be a six-digit number. How are they related? If I want to convert a six-digit number to a eight-digit number,…
Vinod
  • 31,933
  • 35
  • 96
  • 119
86
votes
3 answers

How do I draw bold text on a Bitmap?

I want a Bitmap icon with bold text to draw it on map. I have a snippet to write text on image: Bitmap icon = BitmapFactory.decodeResource(PropertyMapList.this.getResources(), R.drawable.location_mark); TextPaint paint = new…
Sampath Kumar
  • 4,433
  • 2
  • 27
  • 42
77
votes
16 answers

How to use android canvas to draw a Rectangle with only topleft and topright corners round?

I found a function for rectangles with all 4 corners being round, but I want to have just the top 2 corners round. What can I do? canvas.drawRoundRect(new RectF(0, 100, 100, 300), 6, 6, paint);
virsir
  • 15,159
  • 25
  • 75
  • 109
1
2 3
99 100