簡體   English   中英

如何在Android中的不同設備上保持相同的圖像大小

[英]How to keep same image size on different devices in android

我使用imageView在activity上顯示圖像。 該圖片大小不應根據設備屏幕改變。 但這是行不通的。 我想為每個設備使用圖像的實際大小。 以下是我嘗試過的方法。

<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/circle"
        android:id="@+id/imageView"
        android:scaleType="center"/>

</RelativeLayout>

如何在不縮放每個設備屏幕的情況下使用相同的圖像大小? 有什么想法。

謝謝。

使用英寸而不是像素大小

例如android:layout_width=".5in"

<ImageView
        android:layout_width="1in"
        android:layout_height="1in"
        android:src="@drawable/circle"
        android:id="@+id/imageView"
        android:scaleType="center"/>

https://developer.android.com/guide/practices/screens_support.html

您必須針對每種屏幕尺寸設計布局,或者應在不同的dims文件夾中聲明您的Dim而不是wrap_content,因為它會根據屏幕密度縮放圖像,

您必須使用dimens創建的values文件夾是:

values-hdpi/dimens.xml------for hd handsets
values-xhdpi/dimens.xml-----for 5" screens xHD
values-xxhdpi/dimens.xml----for nexus 5X
values-xxxhdpi/dimens.xml----for nexus 6 and 6P devices
values-sw720dp/dimens.xml-----for 9" tablets
values-sw800dp-hdpi/dimens.xml--------for 10" tablets
values-sw600dp/dimens.xml------for 7" tablets

dp使用固定寬度和高度。

DP是dp單位到屏幕像素的轉換很簡單:px = dp *(dpi / 160)。 例如,在240 dpi屏幕上,1 dp等於1.5物理像素。 定義應用程序的UI時,應始終使用dp單位,以確保在具有不同密度的屏幕上正確顯示UI。

<ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/circle"
        android:id="@+id/imageView"
        android:scaleType="center"/>

有關DP,SP,px的更多信息。 請檢查一下

為此,您必須為ImageView定義靜態的固定高度和寬度,然后使用以下屬性:

imgview.setScaleType(ScaleType.FIT_XY);

只需使用photoshop或gimp之類的軟件檢查jour圖像的實際大小,然后以這種方式在imageview中設置實際的寬度和高度(例如:50px x 40px):

   <ImageView
    android:layout_width="50px"
    android:layout_height="40px"
    android:src="@drawable/circle"
    android:id="@+id/imageView"
    android:scaleType="center"/>

只是這樣做!! 高度是固定的,因此您需要提供一個高度值,寬度必須是match_parent。

<ImageView
    android:id="@+id/img_saving_image"
    android:layout_width="match_parent"
    android:layout_height="90dp"
    android:layout_centerHorizontal="true"
    android:src="@drawable/ic_cross"
    android:scaleType="fitCenter"
             or
    android:scaleType="fitXY"
    />

將圖像放入drawabledrawable-nodpi Android不會縮放您的圖片大小。您的代碼無需更改。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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