Displaying the Image in the screen center using Canvas

by Nandagopal T » Thu, 21 Apr 2011 03:17:53 GMT

Sponsored Links
 I am using the canvas to display an image in Android. I want an image
to occupy the center of the screen irrespective of the sizes of the
screen. So how could i achieve it.

This is the snippet that i tried with, Please let me know your ideas

        protected void onDraw(Canvas canvas) {
             // This moves the image to right end of the view.
            // Tried with this code too, but not getting the image at
the center of the screen.
            sampleImage.draw(canvas,getWidth()/2 - imageWidth/
2 ,getHeight()/2 - imageHeight/2);


Thank you


Re: Displaying the Image in the screen center using Canvas

by Kromosome » Thu, 21 Apr 2011 09:58:53 GMT

 Check this out:


Look under the XML Attributes heading!


Sponsored Links

Re: Displaying the Image in the screen center using Canvas

by Tuan.B » Fri, 22 Apr 2011 03:39:25 GMT

 My guess is you want to draw it on the center of the screen, in your
code you refer to the canvas position, then the code you provided will
centered the sample image according to the canvas... so it depend on
how you declared you View referred by your canvas.


Other Threads

1. How to set HTTP proxy


My university requires students to connect to Internet using HTTP
proxy.  There isn't any option in Android phone (Android 1.6, G1) to
set HTTP proxy, moreover proxy also requires authentication.
I haven't found any app in market which can do so.

I am a pro user with computer science background, If there is some
geeky method, even then let me know.


2. Uninstalled apps still showing up.

>From time to time I receive emails regarding a user uinstalling one of
my applications but it still shows up on their phone.  Have any of you
received such messages or experienced this with other applications?

If this is a common problem I'd to at least be able to offer a
suggestion to my customers on how to fix this.  Right now all I can
say is "Sorry, but this is an Android specific problem and there is
nothing I can do on my end to remedy this."



3. HTML5 <video> don't play within a simple WebView

4. New mobile device & Using Android OS

5. Android Operating System Rom Updates


7. CA Certificates