Java-在Android中将ImageView扩展到最大可能的保持纵横比

Java-在Android中将ImageView扩展到最大可能的保持纵横比,第1张

概述我在此站点上找到了很多答案,但它们不起作用或仅适用于单个案例.就我而言:>我想加载图像并将其显示到ImageView中.>ImageView的宽度必须是最大可能的宽度:父布局的宽度.>高度必须是允许重新缩放图像并保持纵横比的高度.如何做到这一点?这是我的布局,里面有ImageView(当前我一直在

我在此站点上找到了很多答案,但它们不起作用或仅适用于单个案例.就我而言:

>我想加载图像并将其显示到ImageVIEw中.
> ImageVIEw的宽度必须是最大可能的宽度:父布局的宽度.
>高度必须是允许重新缩放图像并保持纵横比的高度.

如何做到这一点?这是我的布局,里面有ImageVIEw(当前我一直在使用fitXY,但是即使使用AdjustVIEwBounds,图像也不会保留长宽比:

@H_404_12@<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:tools="http://schemas.androID.com/tools"    androID:ID="@+ID/layoutPicturegalleryItem"    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content"    androID:padding="5dp"    androID:background="#E0E0DE"    androID:orIEntation="vertical" >    <ImageVIEw        androID:ID="@+ID/imageVIEw"        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent"        androID:scaleType="fitXY"        tools:ignore="ContentDescription" />    <linearLayout        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        androID:orIEntation="vertical"        androID:background="#FFFFFF" >        <TextVIEw            androID:ID="@+ID/textname"            androID:layout_wIDth="match_parent"            androID:layout_height="wrap_content"            androID:text="esto es una prueba para ver cómo queda el layout"            androID:textcolor="#000000"            androID:textSize="12sp"            androID:typeface="serif"            androID:padding="2dp" />        <TextVIEw            androID:ID="@+ID/textDescription"            androID:layout_wIDth="match_parent"            androID:layout_height="wrap_content"            androID:visibility="gone"            androID:padding="2dp" />    </linearLayout></linearLayout>

解决方法:

使用此小部件而不是您的imagevIEw,并使其宽度与父级匹配:

@H_404_12@package com.myapp.Widgets;import androID.content.Context;import androID.graphics.drawable.Drawable;import androID.util.AttributeSet;import androID.Widget.ImageVIEw;public class ResizableImageVIEw extends ImageVIEw {public ResizableImageVIEw(Context context){    super(context);}public ResizableImageVIEw(Context context, AttributeSet attrs){    super(context, attrs);}public ResizableImageVIEw(Context context, AttributeSet attrs,        int defStyle){    super(context, attrs, defStyle);}@OverrIDeprotected voID onMeasure(int wIDthMeasureSpec, int heightmeasureSpec){    Drawable drawable = getDrawable();    if (drawable != null)    {        int wIDth =  MeasureSpec.getSize(wIDthMeasureSpec);        int diw = drawable.getIntrinsicWIDth();        if (diw > 0)        {            int height = wIDth * drawable.getIntrinsicHeight() / diw;            setMeasuredDimension(wIDth, height);        }        else            super.onMeasure(wIDthMeasureSpec, heightmeasureSpec);    }    else        super.onMeasure(wIDthMeasureSpec, heightmeasureSpec);}}
总结

以上是内存溢出为你收集整理的Java-在Android中将ImageView扩展到最大可能的保持纵横比全部内容,希望文章能够帮你解决Java-在Android中将ImageView扩展到最大可能的保持纵横比所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1087611.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存