2013-06-25 36 views
1

什麼是處理圖像寬高比的正確和乾淨的方法?如何處理不同分辨率和旋轉的圖像寬高比

在我的應用程序,如果我旋轉屏幕,我失去了縱橫比和我的圖像出現難看得要命 - 看到這些圖像進行比較的我的應用程序和Play商店 - http://imgur.com/a/GriwP#0

這是我正在做:

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
    android:layout_gravity="center" > 

    <Button 
     android:id="@+id/btnStark" 
     android:layout_marginLeft="4dp" 
     android:layout_marginRight="2dp" 
     android:layout_width="fill_parent" 
     android:layout_height="100dp" 
     android:layout_weight="1" 
     android:background="@drawable/stark" 
     android:gravity="center|bottom" 
     android:layout_gravity="top" /> 

    <Button 
     android:id="@+id/btnLannister" 
     android:layout_marginLeft="2dp" 
     android:layout_marginRight="4dp" 
     android:layout_width="fill_parent" 
     android:layout_height="100dp" 
     android:layout_weight="1" 
     android:background="@drawable/stark" 
     android:gravity="center|bottom" 
     android:layout_gravity="top" /> 
</LinearLayout> 

我該如何處理高度?我應該設置一個固定值嗎?或者有什麼辦法可以自動保持寬高比?

我使用的是像400x300像素,

回答

1

使用ImageViews代替鈕釦的考慮 - 他們可以點擊就像按鈕。您可以使用scaleType屬性設置圖像的縮放比例。

http://developer.android.com/reference/android/widget/ImageView.ScaleType.html

例如,CENTER_INSIDE:

縮放圖像均勻(保持圖像的縱橫比),以使圖像的兩個尺寸(寬度和高度)將等於或小於比視圖的相應維度(減填充)。

所以,像這樣:

<ImageView 
    android:id="@+id/btnStark" 
    android:layout_marginLeft="4dp" 
    android:layout_marginRight="2dp" 
    android:layout_width="fill_parent" 
    android:layout_height="100dp" 
    android:layout_weight="1" 
    android:src="@drawable/stark" 
    android:scaleType="centerInside" 
    android:gravity="center|bottom" 
    android:layout_gravity="top" /> 

一個蘭尼斯特總是支付他的債務。

+0

謝謝。 ImageView真正解決了寬高比問題。 另一個問題:爲android:layout_height設置固定值是否是最佳做法? – Ricardo

+0

謝謝@Ricardo。請考慮標記此答案爲已接受,如果它解決了您的問題!原因如下:http://meta.stackexchange.com/a/5235 –

0

您可以嘗試使用ImageButton而不是簡單的Button並嘗試使用不同的ScaleType模式來選擇更適合您的設備。

相關問題