简体   繁体   English

确定ImageView中空白的大小

[英]Determine size of whitespace in ImageView

I have a floor plan which is centred in an ImageView. 我有一个以ImageView为中心的平面图。 The aspect ratio of the image is preserved so there is some space on either side of the image (indicated in red colour). 图像的宽高比得以保留,因此图像的两边都有一定的空间(以红色表示)。

How can I determine the width of this red space in pixels or dp? 如何确定此红色空间的宽度(以像素或dp为单位)?

在此处输入图片说明

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MyActivity"
    android:background="@color/red"
    android:orientation="vertical">

<ImageView
    android:id="@+id/floor_plan"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/floor_plan2"
    android:adjustViewBounds="true"
    android:layout_toStartOf="@+id/locateMe"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentEnd="true"
    />

I note that the margins have been set to 0dp in the dimens.xml file. 我注意到,在dimens.xml文件中,页边距已设置为0dp。

That's only math : 那只是数学:

int originalImgWidth  = bitmap.getWidth();
int originalImgHeight  = bitmap.getHeight();

float scale = screenHeight * 1f / originalImgHeight;  
// since your img will fill the screen vertically

float marginSize =  (screenWidth - (originalImgWidth * scale)) /2; 

Note that the size might be negative if the image is bigger than the screen horizontally ^^ 请注意,如果图像水平大于屏幕,则尺寸可能为负^^

see how to get the bitmap here 这里查看如何获取位图

see how to get screen size here 在这里查看如何获取屏幕尺寸

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

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