![](/img/trans.png)
[英]Android: How to determine size of an ImageView on the screen in inches
[英]Determine size of whitespace in ImageView
我有一個以ImageView為中心的平面圖。 圖像的寬高比得以保留,因此圖像的兩邊都有一定的空間(以紅色表示)。
如何確定此紅色空間的寬度(以像素或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"
/>
我注意到,在dimens.xml文件中,頁邊距已設置為0dp。
那只是數學:
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;
請注意,如果圖像水平大於屏幕,則尺寸可能為負^^
在這里查看如何獲取位圖
在這里查看如何獲取屏幕尺寸
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.