簡體   English   中英

確定ImageView中空白的大小

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM