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.
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 s开发者_运维百科hrink 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).
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.
<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)
<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>
精彩评论