ImageView 缩放 TOP_CROP

ImageView scaling TOP_CROP(ImageView 缩放 TOP_CROP)

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

问题描述

我有一个 ImageView ,它显示的 png 比设备的宽高比更大(垂直地说 - 意味着它更长).我想在保持纵横比、匹配父级宽度并将图像视图固定到屏幕顶部的同时显示它.

I have an ImageView which is displaying a png that has a bigger aspect ratio than that of the device (vertically speaking - meaning its longer). I want to display this while maintaining aspect ratio, matching the width of the parent, and pinning the imageview to the top of the screen.

我使用 CENTER_CROP 作为缩放类型时遇到的问题是,它将(可以理解)使缩放图像居中,而不是将顶部边缘与图像视图的顶部边缘对齐.

The problem i have with using CENTER_CROP as the scale type is that it will (understandable) center the scaled image instead of aligning the top edge to the top edge f the image view.

FIT_START 的问题是图像将适合屏幕高度而不是填充宽度.

The problem with FIT_START is that the image will fit the screen height and not fill the width.

我已经通过使用自定义 ImageView 并覆盖 onDraw(Canvas) 并使用画布手动处理这个问题来解决这个问题;这种方法的问题在于 1)我担心可能有一个更简单的解决方案,2)我在尝试设置时在构造函数中调用 super(AttributeSet)当堆有 3 mb 可用空间(堆大小为 6 mb)并且无法找出原因时,src img 为 330kb.

I have solved this problem by using a custom ImageView and overriding onDraw(Canvas) and handeling this manually using the canvas; the problem with this approach is that 1) I'm worried there may be a simpler solution, 2) I am getting a VM mem exception when calling super(AttributeSet) in the constructor when trying to set a src img of 330kb when the heap has 3 mb free (with a heap size of 6 mb) and cant work out why.

非常欢迎任何想法/建议/解决方案:)

Any ideas / suggestions / solutions are much welcome :)

谢谢

附言我认为一个解决方案可能是使用矩阵比例类型并自己做,但这似乎与我当前的解决方案相同或更多!

p.s. i thought that a solution may be to use a matrix scale type and do it myself, but that seems to to be the same or more work than my current solution!

推荐答案

好的,我有一个可行的解决方案.Darko 的提示让我再次查看 ImageView 类(谢谢)并使用 Matrix 应用了转换(正如我最初怀疑的那样,但在我的第一次尝试中没有成功!).在我的自定义 imageView 类中,我在构造函数中的 super() 之后调用 setScaleType(ScaleType.MATRIX),并具有以下方法.

Ok, I have a working solution. The prompt from Darko made me look again at the ImageView class (thanks) and have applied the transformation using a Matrix (as i originally suspected but did not have success on my first attempt!). In my custom imageView class I call setScaleType(ScaleType.MATRIX) after super() in the constructor, and have the following method.

    @Override
    protected boolean setFrame(int l, int t, int r, int b)
    {
        Matrix matrix = getImageMatrix(); 
        float scaleFactor = getWidth()/(float)getDrawable().getIntrinsicWidth();    
        matrix.setScale(scaleFactor, scaleFactor, 0, 0);
        setImageMatrix(matrix);
        return super.setFrame(l, t, r, b);
    }

我已将 int 放在 setFrame() 方法中,因为在 ImageView 中,对 configureBounds() 的调用在此方法中,这是所有缩放和矩阵内容的地方发生了,所以对我来说似乎是合乎逻辑的(如果你不同意)

I have placed int in the setFrame() method as in ImageView the call to configureBounds() is within this method, which is where all the scaling and matrix stuff takes place, so seems logical to me (say if you disagree)

下面是来自 AOSP(Android 开源项目)的 super.setFrame() 方法

Below is the super.setFrame() method from the AOSP (Android Open Source Project)

    @Override
    protected boolean setFrame(int l, int t, int r, int b) {
        boolean changed = super.setFrame(l, t, r, b);
        mHaveFrame = true;
        configureBounds();
        return changed;
    }

找到完整的类 src 这里

Find the full class src here

这篇关于ImageView 缩放 TOP_CROP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:ImageView 缩放 TOP_CROP

基础教程推荐