Android - 将图像定位在确切位置

时间:2016-08-11 04:42:19

标签: android image canvas bitmap position

我正在尝试将图像放置在我的Android应用程序中,以便它将显示在屏幕宽度的中间和屏幕高度的下半部分。我需要它看起来像这样。

enter image description here

我的图片是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);

但我仍然没有找到正确的位置而且我得到的就像这样。

enter image description here

我在这里做错了什么?我怎么能这样做?

3 个答案:

答案 0 :(得分:1)

你能试试吗

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

答案 1 :(得分:0)

为什么需要为此编写java代码?

只需从xml指定属性即可。

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

完成了

答案 2 :(得分:0)

试试这个

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