简体   繁体   English

Android-将图片定位在准确的位置

[英]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. 我正在尝试将图像放置在我的android应用中,以使其显示在屏幕宽度的中间和屏幕高度的下部。 I need it to look like this. 我需要它看起来像这样。

在此处输入图片说明

My image is 200x50, so I used following logic to achieve this. 我的图片是200x50,因此我使用以下逻辑来实现此目的。

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. 但是,我仍然没有找到正确的位置,而是像这样。

在此处输入图片说明

What I am doing wrong here? 我在这里做错了什么? How can I do this? 我怎样才能做到这一点?

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);

why there is an need to write java code for this ? 为什么需要为此编写Java代码?

just specify properties from xml. 只需从xml指定属性即可。

center horizontal = true and margin bottom = "some value"

its done 完成

Try this 尝试这个

alignParentBottom - true
centerHorizontal - true
marginBottom (Calculate 15% of height and set it)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM