具有右对齐、裁剪、未缩放内容的 ImageView
我已经搜索、摆弄和哭泣了几个小时,但不知道如何将图像放入 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 中,并将滚动固定到最右侧(并禁用任何表明它是滚动视图的指示)。
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.
The ImageView is defined as such:
<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_height
is set to wrap_content
because the ImageView will always have a fixed height, determined from the image.
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.
Using the above layout code, the image scales to fit the ImageView. The only way I can prevent the scaling is by setting android:scaleType="center"
, which prevents scaling, but unfortunately centres the image.
Ay ideas? Otherwise, I'm currently toying with the following alternatives:
- Subclass ImageView and implement my own onDraw() method.
- 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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将图像放置在 LinearLayout 或relativelayout 中,而不是滚动视图。让布局的layout_width fill_parent 和layout_height 包裹内容。然后在 ImageView 上使用布局重力权和wrap_content作为宽度和高度。
Instead of a scroll view - put the image inside of a LinearLayout or RelativeLayout. Make the layout's layout_width fill_parent and the the layout_height wrap content. Then on the ImageView use layout gravity right and wrap_content for both the width and the height.
@harmanjd:你犯了一个小错误,这是正确的方法:
(抱歉我无法评论你的答案)
@harmanjd: you have done a little error, this is the correct way:
(sorry i can't comment your answer)