簡體   English   中英

具有右對齊,裁剪,未縮放內容的ImageView

[英]ImageView with right-aligned, cropped, unscaled content

我一直在搜索,擺弄和哭泣了好幾個小時,但無法弄清楚如何將圖像放置到ImageView中,以及如何將圖像呈現為未縮放,右對齊以及裁剪超出ImageView左側的溢出。

ImageView的定義如下:

<ImageView
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:src="@drawable/bleh"
></ImageView>

layout_width設置為fill_parent以拉伸和縮小ImageView以適合屏幕。

layout_height設置為wrap_content因為ImageView始終具有固定的高度(根據圖像確定)。

我尚未定義layout_gravity="right"因為我的理解是這與ImageView在其父級內部的位置有關,在這種情況下,由於ImageView的寬度設置為fill_parent,因此該設置無效。

使用上面的布局代碼,圖像可以縮放以適合ImageView。 我可以防止縮放的唯一方法是設置android:scaleType="center" ,它可以防止縮放,但不幸的是使圖像居中。

有想法嗎? 否則,我目前正在嘗試以下替代方法:

  • 子類化ImageView並實現我自己的onDraw()方法。
  • 將全圖像大小的ImageView放到ScrollView中,並將滾動固定在最右邊(並禁用任何指示它是滾動視圖的指示)。

而不是滾動視圖-將圖像放在LinearLayout或RelativeLayout內。 使布局的layout_width fill_parent和layout_height包裝內容。 然后在ImageView上,使用布局重力right和wrap_content分別設置寬度和高度。

<LinerLayout android:layout_width="wrap_content" 
  android:layout_height="wrap_content">
<ImageView android:layout_gravity="right"
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content"
  android:src="@drawable/bleh"/>
</LinearLayout>

@harmanjd:您做了一個小錯誤,這是正確的方法:(對不起,我無法評論您的回答)

<LinearLayout android:layout_width="fill_parent" 
     android:layout_height="wrap_content"
     android:gravity="right">

    <ImageView 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:src="@drawable/bleh"/>
    />
</LinearLayout>

暫無
暫無

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

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