ImageView的比例TOP_CROP比例、ImageView、TOP_CROP

2023-09-12 22:44:04 作者:猛男打天下

我有一个的ImageView 这是显示一个PNG具有比该设备的一个更大的纵横比(垂直说 - 这意味着它更长的时间)。我想显示本,同时保持高宽比,匹配父的宽度,并且钉扎imageview的到屏幕的顶部。

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 作为规模类型是,它会(可以理解)中心缩放后的图像,而不是对准的上边缘到顶部边缘f显示图像视图。

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(画布)键,手动使用画布HANDELING这解决了这个问题;这种方法的问题是,1)我担心有可能是一个简单的解决方案,2)我在呼唤超(AttributeSet中)时,得到一个VM纪念品例外构造函数尝试时设置330KB的IMG SRC时堆有3 Mb免费(以6 MB堆大小)和无法工作的原因。

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。我认为一个解决办法可能是使用一个矩阵的规模型,做我自己,但似乎是相同或更多的工作比我目前的解决方案!

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!

推荐答案

好吧,我有一个有效的解决方案。从达科提示让我再看看ImageView的类(感谢),并已申请使用矩阵变换(如我最初怀疑,但并没有对我的第一次尝试成功了!)。在我的自定义的ImageView类我称之为 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的super.setFrame()方法

Below is the super.setFrame() method from AOSP

 @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 here