2014-09-26 59 views
0

我有兩個圖像文件:填充ImageView的篩選,同時保持縱橫比

  • 320dpi 768x1280
  • 480dpi 1080×1920

我把每幀圖像的xhdpi和xxhdpi可繪製的文件夾我的項目。我試圖讓這兩個圖像適當縮放,並填充屏幕沒有任何空格。

我的XML代碼如下:

<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" 
    tools:context="com.example.vax.gamurs.LandingPage" 
    android:background="#ffffffff" 
    android:orientation="vertical" 
> 


<ImageView 
    android:id="@+id/LandingBack" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:src="@drawable/background" 
    android:adjustViewBounds="true" 
    android:layout_gravity="center" 
    android:layout_centerVertical="true" 
    android:layout_centerHorizontal="true" /> 

我越來越空白雙方的的Nexus 4和Nexus 5的側面怎樣消除空白,並具有圖像填充屏幕,同時保持長寬比?

Nexus4

+1

由於圖像的寬高比與屏幕的寬高比不同,所以不能。您的選擇是在邊框上留有空白或裁剪圖像。 – GreyBeardedGeek 2014-09-26 02:47:04

+0

@GreyBeardedGeek我應該使用什麼樣的比例來確保屏幕被填滿? – 2014-09-26 02:54:19

+0

取決於設備。沒有一個通用長寬比。 – GreyBeardedGeek 2014-09-26 10:04:37

回答

1

使用scaleType="centerCrop"如下:

<ImageView 
     android:id="@+id/LandingBack" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:src="@drawable/background" 
     android:scaleType="centerCrop" 
     android:adjustViewBounds="true" 
     android:layout_gravity="center" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true" /> 

上述方法將裁剪圖像。不幸的是,如果您使用固定大小的繪圖並且想要支持不同的屏幕尺寸,這是一個缺點。

另一種方法是使用下面附加的9補丁圖像。這將拉伸藍色背景以填充空白空間。

下載鏈接9Patch圖像:https://dl.dropboxusercontent.com/u/2411239/sampleimg.9.png

下面是一個簡單的截圖,顯示了它是如何工作的:

enter image description here

更新:

您的新ImageView應儘可能如下:

<ImageView 
    android:id="@+id/LandingBack" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/sampleimg" 
    android:adjustViewBounds="true" 
    android:layout_gravity="center" 
    android:layout_centerVertical="true" 
    android:layout_centerHorizontal="true"/> 

同樣爲了參考,我使用1px的拉伸貼劑如下所示的圖像中:

enter image description here

+0

centreCrop讓我因裁剪而失去圖像的一部分。 – 2014-09-26 06:57:08

+0

@OsborneCox不幸的是,如果您有一個固定大小的圖像(尤其是所有具有不同尺寸的設備),這是缺點。您可以使用可繪製的九個補丁,這將自動縮放藍色背景以填充白色區域。讓我知道你是否需要關於這種方法的更多信息。 – 2014-09-26 07:03:58

+0

在這種情況下,我怎樣才能適當地使用九個補丁?縮放藍色背景將是完美的,因爲它會保留文本和按鈕的大小/比例。 – 2014-09-26 07:13:26

-1

只要使用的ImageView

 android:scaleType="fitXY" 

的屬性,這意味着

<ImageView 
    android:scaleType="fitXY" 
    android:id="@+id/LandingBack" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:src="@drawable/background" 
    android:adjustViewBounds="true" 
    android:layout_gravity="center" 
    android:layout_centerVertical="true" 
    android:layout_centerHorizontal="true" /> 

在Imageview中,你完成了。快樂編碼

+0

這會導致圖像被拉伸。 – 2014-09-26 06:56:36

0

在保持寬高比時無法填充,因爲沒有意義。你可以做的是將背景設置爲與藍色相同。(非常簡單)或者使用簡單的數學方法填充圖像的高度或僅填充寬度

w = W; h =(w/W)H;

其中W是屏幕的寬度,H是屏幕的高度,w是圖像和h的寬度圖像

的高度

交匯處寬度與高度以填充整個高度,而不是寬度

0

嘗試像這樣設置imageview的背景:

android:background="@drawable/background" 

而不是「src」。如果您使用src,您的圖片將不會縮放,但背景將會縮放。

但是這種縮放可能會破壞你的圖像,你應該使用九個補丁。

相關問題