ImageView 调整视图边界不起作用

ImageView adjustViewBounds not working(ImageView 调整视图边界不起作用)
本文介绍了ImageView 调整视图边界不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 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 像素.这段代码生成了一个 100px 宽和 50px 高的图片,但 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 调整视图边界不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)