ImageView的:填写水平保持宽高比 [英] ImageView: fill horizontal maintaining aspect ratio

查看:179
本文介绍了ImageView的:填写水平保持宽高比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个ImageView的规模是图像,直到填满水平的父。

I need an ImageView scale it image until fill the parent horizontally.

如果该ImageView的背景为红色,内容(图片下方),其余是我要找的图片所示的结果绿1 即可。该结果被自动获得,如果图像宽度比屏幕的宽度高。

If the imageview background is red, and the rest of the content (below the image) is the green I'm looking for the result shown by the Picture 1. That result is obtained automatically if the image width is higher than the screen width.

但是,如果是一个小画面像在画面2.最好的结果,我可以得到的是在图片3 (设置ImageView的宽度和高度为 FILL_PARENT 和scaleType为 FitStart

But if is a small picture like in the picture 2. the best result I can get is the picture 3 (setting the ImageView width and height to fill_parent and the scaleType to FitStart

获得的图像设置4高= WRAP_CONTENT,宽= FILL_PARENT,scaleType = CenterCrop。它应该垂直缩放作秀整个图像,但由于scaleType说,庄稼了。

The picture 4 is obtained setting height= wrap_content, width= fill_parent, scaleType= CenterCrop. It should scale vertically for show the entire image, but as the scaleType says it crops it.

获取照片1即使图像是小的任何想法?

Any ideas for getting the picture 1 even if the image is small?

将授予50赏金到工作答案

Will grant a bounty of 50 to a working answer

推荐答案

这是没有自定义类是可行的。这是我的小图像获得的结果:

It's doable without custom classes. This is the result I've gained with small image:

通过大图片:

With large image:

您必须使用 RelativeLayout的为了这个工作,但你可以用的LinearLayout 结合起来,以实现无论你需要什么。这里是我的xml:

You have to use RelativeLayout in order for this to work, but you can combine it with LinearLayout to achieve whatever you need. Here is my xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <LinearLayout
            android:id="@+id/button_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_alignParentBottom="true"
            >
            <Button
                android:text="button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <Button
                android:text="button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <Button
                android:text="button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
        </LinearLayout>
        <ImageView 
            android:src="@drawable/cat"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"
            android:scaleType="centerCrop"
            android:layout_above="@id/button_container"/>
    </RelativeLayout>
</ScrollView>

的技巧是,你将其设置为填满整个屏幕,但它必须是其他布局上面。这样,你达到你所需要的一切。

The trick is that you set it to fill the screen but it has to be above the other layouts. This way you achieve everything you need.

这篇关于ImageView的:填写水平保持宽高比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆