Nandagopal T
Nandagopal T

Reputation: 2037

How to Place a Image at the center of Screen using Canvas

I am using the canvas to display an image in Android. I want this 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 too.

@Override
        protected void onDraw(Canvas canvas) {
        ..........
            ..........
            sampleImage.draw(canvas,getWidth(),getHeight()); // This moves the image to right end of the view.
            ..........
            ..........

Any kind of help is highly appreciated.

Thanks.

Upvotes: 1

Views: 4555

Answers (1)

Lukas
Lukas

Reputation: 465

sampleImage.draw(
      canvas,
      getWidth()/2 - imageWidth/2,
      getHeight()/2 - imageHeight/2
); 

;)

Upvotes: 1

Related Questions