2011-01-28 74 views
12

我有兩個圖像的佈局:級圖像到屏幕寬度

  • 一個應該STRETCH時到屏幕寬度
  • 一個上面應該擴展到相同的比例的第一個是automaticaly縮放(相對於原始圖像的大小)

更具體:兩個圖像是相同的圖像切片,並因此一些細節內他們應該匹配。
我可以使用XML嗎?

如果我不能通過XML來完成,也許我可以對圖形進行預分頻。在這種情況下,我應該如何對它們進行預分頻?

+0

你解決了這個問題嗎?順便說一句,不知道你是否看過,但我更新了我對你的`Canvas.onDraw`問題的答案。 – techiServices 2011-02-13 21:47:09

回答

6

這是一個黑客,但它可以讓你在XML中做到這一點。

如果你知道,例如,上面的圖像是底部一個大小的X%,那麼你可以使用的LinearLayout的layout_weight在屏幕的百分比的形式位置和大小,頂部圖像:

<LinearLayout android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
    <ImageView android:id="@+id/left_filler" android:layout_weight="20" 
     android:layout_width="wrap_content" android:layout_height="wrap_content"/> 
    <ImageView android:id="@+id/top_image" android:layout_weight="50" 
     android:layout_width="wrap_content" android:layout_height="wrap_content"/> 
    <ImageView android:id="@+id/right_filler" android:layout_weight="30" 
     android:layout_width="wrap_content" android:layout_height="wrap_content"/> 
</LinearLayout> 
... bottom image

上面的代碼會在屏幕的50%處調整top_image的大小,左邊的偏移量爲20%。只要top_image是bottom_image大小的50%,這將保持相似的比例。

或者,執行此操作的「正確」方法可能是在自定義視圖中覆蓋onDraw()並使用畫布繪製方法。

0

您可以使用Canvas類方法drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint) 通過自動縮放/翻譯以填充目標矩形來繪製指定的位圖。這可以用於具有不同Rect的位圖。 Rect可以通過劃分佈局的當前寬度和高度來制定。因此,程序將根據具有不同屏幕尺寸的設備來縮放圖像。