将图像缩放到最大可用宽度,保持宽高比并避免ImageView多余的空间 [英] Scaling an image to max available width, keeping aspect ratio and avoiding ImageView extra space

查看:80
本文介绍了将图像缩放到最大可用宽度,保持宽高比并避免ImageView多余的空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个简单的布局XML和一个30X30像素的green_square.png文件

I have this simple layout XML, and a green_square.png file which is 30X30 pixels

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:background="@color/red"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:adjustViewBounds="true"
        android:scaleType="fitStart"
        android:src="@drawable/green_square"/>

</LinearLayout>

我想要实现的是缩放图像以填充父级宽度,并保持其长宽比,而没有ImageView的额外空间.

What I'm trying to achieve is scaling the image to fill the parent width, keeping it's aspect ratio with no extra space of the ImageView.

我在ImageView中添加了一个红色背景,仅供参考,我想避免这些额外的空间.

I've added a red background to the ImageView just for reference of the extra space I want to avoid.

这是我的目标

推荐答案

<ImageView
        android:id="@+id/game"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scaleType="centerCrop"
        android:adjustViewBounds="true"
        android:src="@drawable/ic_game" 
        />

这对我有用.父本和中心作物的全宽以保持宽高比

this works for me. Full width of parent and center crop to keep aspect ratio

这篇关于将图像缩放到最大可用宽度,保持宽高比并避免ImageView多余的空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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