ImageView adjustViewBounds 不起作用 [英] ImageView adjustViewBounds not working

查看:38
本文介绍了ImageView adjustViewBounds 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 android:layout_width=100dpandroid:layout_height=wrap_contentandroid:adjustViewBounds=true

I have an ImageView with android:layout_width=100dp, android:layout_height=wrap_content and android:adjustViewBounds=true

它的来源是一张 50 x 50 像素的图片.但是纵横比没有保留 - ImageView 的高度是 50 像素,而不是 100 像素(即 adjustViewBounds 不起作用).如果我有一张 200x200 像素的图片,它可以工作——宽度和高度都是 100 像素.此代码生成 100 像素宽和 50 像素高的图片,但 src 图像是方形的:

It's source is a 50 x 50 px picture. But the aspect ratio is not preserved - height of the ImageView is 50px, not 100px (i.e. adjustViewBounds is not working). If I have a 200x200px picture it works - width and height are 100px. This code results in a 100px wide and 50px tall picture but the src image is square:

<?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">

    <ImageView
        android:id="@+id/photo"
        android:src="@drawable/icon"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        android:adjustViewBounds="true" />

</LinearLayout>

推荐答案

问题在于adjustViewBounds 不会增加ImageView> 超出可绘制对象的自然尺寸.它只会缩小视图以保持纵横比;如果您提供 500x500 的图片而不是 50x50 的图片,这应该可行.

The issue is that adjustViewBounds will not increase the size of the ImageView beyond the natural dimensions of the drawable. It will only shrink the view to maintain aspect ratio; if you provide a 500x500 image instead of a 50x50 image, this should work.

如果您对实现此行为的位置感兴趣,请参阅 ImageView.java 的 onMeasure 实现.

If you're interested in the spot where this behavior is implemented, see ImageView.java's onMeasure implementation.

一种解决方法是实现一个自定义 ImageView 来改变 onMeasure 中的这种行为.

One workaround is to implement a custom ImageView that changes this behavior in onMeasure.

这篇关于ImageView adjustViewBounds 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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