2016-04-26 90 views
0

我使用imageView來顯示活動圖像。此圖像大小不應根據設備屏幕更改。但這不起作用。我想爲每個設備使用圖像的實際大小。以下是我所嘗試過的。如何在android中的不同設備上保留相同的圖像大小

<?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> 

我怎樣才能使用相同的圖像大小,而不在每個設備屏幕上縮放? 有什麼想法。

謝謝。

回答

0

使用英寸,而不是像素大小

例如,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

+0

你能舉個例子嗎?我對你的共享鏈接感到困惑。 – Barrier

+0

@Brier,已經做到了。或者你想要什麼樣的代碼? – Vyacheslav

+0

@Brier,你試過了嗎? – Vyacheslav

0

你必須設計佈局爲每一個屏幕尺寸還是應該在不同的助攻宣告你夢詩文件夾,而不是wrap_content,因爲它會根據屏幕密度縮放您的圖像,

值文件夾,您必須創建與維度:

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 
+0

這是非常不舒服和惱人的使用很多xmls – Vyacheslav

+0

不,這不是我所期望的。 – Barrier

+0

我不想縮放圖像或imageView。我只是想在每臺設備上保留其實際大小。 – Barrier

0

使用固定寬度和高度在dp

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

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

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

0

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

imgview.setScaleType(ScaleType.FIT_XY); 
0

使用的軟件,只檢查怨婦形象的實際大小像Photoshop或瘸子,然後設置實際寬度和高度(例如:50像素X 40像素),在你的ImageView這樣:

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

只是它來做這樣!高度是固定的,所以你需要提供一個高度值,寬度必須是match_parent.Its對我來說工作得很好。

<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" 
    /> 
0

把你的圖像中drawabledrawable-nodpi。 Android不會縮放您的圖片大小。您的代碼不需要更改。

+0

那樣的話,如何提供圖片src的路徑? – Barrier

+0

@ drawable/circle – AlphaBoom

相關問題