Chamila Wijayarathna Chamila Wijayarathna - 3 months ago 18
Android Question

Android - Positioning an image at exact location

I am trying to position an image in my android app so that it will display in the middle of the width of screen and lower part of the height of the screen. I need it to look like this.

enter image description here

My image is 200x50, so I used following logic to achieve this.

Display display = ((Activity)context).getWindowManager().getDefaultDisplay();
screenWidth = display.getWidth();
screenHeight = display.getHeight();
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.play), screenWidth/2 - 100, 3*screenHeight/4 - 25, null);


But still I am not getting located at correct place and I am getting it like this.

enter image description here

What I am doing wrong here? How can I do this?

Answer

Can you try this

Display display = ((Activity)context).getWindowManager().getDefaultDisplay();
screenWidth = display.getWidth();
screenHeight = display.getHeight();
Bitmap playImage = BitmapFactory.decodeResource(getResources(), R.drawable.play);
canvas.drawBitmap(playImage, (screenWidth/2) - (playImage.getWidth()/2), 3*screenHeight/4 - 25, null);
Comments