android– 视图转换后的图像超出界限

android– 视图转换后的图像超出界限,第1张

概述我在显示图像时遇到问题.我有一个想要全屏显示的图像.所以我有这个带有match_parent和20dp填充的Imageview.它看起来不错但是当我对它应用旋转时,似乎视图的边界不会改变,并且图像可能会被截断出屏幕!完全不希望发生这种情况!如何重新缩放图像,以便在旋转90度时图像也适合ImageVie

我在显示图像时遇到问题.

我有一个想要全屏显示的图像.所以我有这个带有match_parent和20dp填充的ImagevIEw.

它看起来不错但是当我对它应用旋转时,似乎视图的边界不会改变,并且图像可能会被截断出屏幕!完全不希望发生这种情况!如何重新缩放图像,以便在旋转90度时图像也适合ImageVIEw.

这是我的XML WITH旋转.

编辑:

如何修复图像的边界,使文本在图像上方对齐?


解决方法:

在测量视图和计算比例时,不考虑旋转.一个可能的解决方案是自己做:

public class RotatedImageVIEw extends ImageVIEw {    ...    constructors    ...    private double mRotateDWIDth;    private double mRotatedHeight;    private boolean update() {        Drawable d = getDrawable();        if (d == null) {            return false;        }        int drawableWIDth = d.getIntrinsicWIDth();        int drawableHeight = d.getIntrinsicHeight();        if (drawableWIDth <= 0 || drawableHeight <= 0) {            return false;        }        double rotationRad = getRotation() / 180 * Math.PI;        // calculate intrinsic rotated size        // see diagram        mRotateDWIDth = (Math.abs(Math.sin(rotationRad)) * drawableHeight                + Math.abs(Math.cos(rotationRad)) * drawableWIDth);        mRotatedHeight = (Math.abs(Math.cos(rotationRad)) * drawableHeight                + Math.abs(Math.sin(rotationRad)) * drawableWIDth);        return true;    }    protected voID onMeasure(int wIDthMeasureSpec, int heightmeasureSpec) {        if (update()) {            double ratio = mRotateDWIDth / mRotatedHeight;            int wMax = Math.min(getDefaultSize(Integer.MAX_VALUE, wIDthMeasureSpec), getMaxWIDth());            int hMax = Math.min(getDefaultSize(Integer.MAX_VALUE, heightmeasureSpec), getMaxHeight());            int w = (int) Math.min(wMax, hMax * ratio);            int h = (int) Math.min(hMax, wMax / ratio);            setMeasuredDimension(w, h);        } else {            super.onMeasure(wIDthMeasureSpec, heightmeasureSpec);        }    }    private final float[] values = new float[9];    protected voID onDraw(Canvas canvas) {        if (update()) {            int availableWIDth = getMeasureDWIDth();            int availableHeight = getMeasuredHeight();            float scale = (float) Math.min(availableWIDth / mRotateDWIDth, availableHeight / mRotatedHeight);            getimageMatrix().getValues(values);            setScaleX(scale / values[Matrix.MSCALE_X]);            setScaleY(scale / values[Matrix.MSCALE_Y]);        }        super.onDraw(canvas);    }    @OverrIDe    public voID setRotation(float rotation) {        super.setRotation(rotation);        requestLayout();    }}

adjustVIEwBounds必须为true:

<com.mypackage.RotatedImageVIEw    androID:layout_wIDth="wrap_content"    androID:layout_height="wrap_content"    androID:layout_margin="20dp"    androID:adjustVIEwBounds="true"    androID:rotation="90"    androID:maxWIDth="100dp"    androID:maxHeight="100dp"    androID:scaleType="fitCenter"    androID:src="@drawable/test" />

计算的一个很好的解释,礼貌Cheticamp:

更新:现在尝试调整边界. wrap_content和match_parent之间没有区别(基于图像方面,两者都增长得越多).您应该使用maxWIDth和/或maxHeight,或者将其放在具有0大小和权重的linearLayout中.

它也是不可动画的,调整边界虽然动画需要每帧的布局通过,这是非常低效的.查看可用于VIEw.animate()的版本的其他答案

总结

以上是内存溢出为你收集整理的android – 视图转换后的图像超出界限全部内容,希望文章能够帮你解决android – 视图转换后的图像超出界限所遇到的程序开发问题。

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

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

原文地址:https://54852.com/web/1111322.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存