简体   繁体   English

具有右对齐,裁剪,未缩放内容的ImageView

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

I've been searching, fiddling and crying for hours but can't figure out how to place an image into an ImageView and have the image presented unscaled, right-aligned and have the overflow beyond the left side of the ImageView cropped. 我一直在搜索,摆弄和哭泣了好几个小时,但无法弄清楚如何将图像放置到ImageView中,以及如何将图像呈现为未缩放,右对齐以及裁剪超出ImageView左侧的溢出。

The ImageView is defined as such: ImageView的定义如下:

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

layout_width is set to fill_parent to stretch and shrink the ImageView to suit the screen. layout_width设置为fill_parent以拉伸和缩小ImageView以适合屏幕。

layout_height is set to wrap_content because the ImageView will always have a fixed height, determined from the image. layout_height设置为wrap_content因为ImageView始终具有固定的高度(根据图像确定)。

I have not defined a layout_gravity="right" because my understanding is this relates to the positioning of the ImageView inside its parent, which in this case has no effect because the ImageView's width is set to fill_parent. 我尚未定义layout_gravity="right"因为我的理解是这与ImageView在其父级内部的位置有关,在这种情况下,由于ImageView的宽度设置为fill_parent,因此该设置无效。

Using the above layout code, the image scales to fit the ImageView. 使用上面的布局代码,图像可以缩放以适合ImageView。 The only way I can prevent the scaling is by setting android:scaleType="center" , which prevents scaling, but unfortunately centres the image. 我可以防止缩放的唯一方法是设置android:scaleType="center" ,它可以防止缩放,但不幸的是使图像居中。

Ay ideas? 有想法吗? Otherwise, I'm currently toying with the following alternatives: 否则,我目前正在尝试以下替代方法:

  • Subclass ImageView and implement my own onDraw() method. 子类化ImageView并实现我自己的onDraw()方法。
  • Place the full-image-sized ImageView into a ScrollView and fix the scrolling to the far right (and disable any indication that it is a scroll view). 将全图像大小的ImageView放到ScrollView中,并将滚动固定在最右边(并禁用任何指示它是滚动视图的指示)。

Instead of a scroll view - put the image inside of a LinearLayout or RelativeLayout. 而不是滚动视图-将图像放在LinearLayout或RelativeLayout内。 Make the layout's layout_width fill_parent and the the layout_height wrap content. 使布局的layout_width fill_parent和layout_height包装内容。 Then on the ImageView use layout gravity right and wrap_content for both the width and the 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: you have done a little error, this is the correct way: (sorry i can't comment your answer) @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