在给定矩形边界的情况下以编程方式缩放和居中 imageView

Programatically scaling and centering imageView given rectangular bounds

我的问题

假设我有一个位图 - 将其命名为 bmap,尺寸为 100x75 (bmap.getWidth() x bmap.getHeight())

假设我有一个矩形 rect,位于 phone 屏幕 (x,y) 上的某个点,尺寸为 500x350 (width x height)

我如何编写一段代码,使该位图在边界矩形内居中。

注:因为我用的是ContraintLayout所以没有parents或相对论的概念。

此外,我想要 (0,1] 范围内的 scale 变量,它缩放 bmap 但它保持在边界矩形中心的位置 rect .

我可怕的尝试:

ImageView imgView = new ImageView(this.context);
imgView.setMaxWidth((int)(width*scale));
imgView.setMinimumWidth((int)(width*scale));
imgView.setMaxHeight((int)(height*scale));
imgView.setMinimumHeight((int)(height*scale));

imgView.setImageBitmap(bmap);

imgView.setX(x+((width-bmap.getWidth())/2));
imgView.setY(y+((height-bmap.getHeight())/2));

imgView.setAdjustViewBounds(true);

constraintLayout.addView(imgView);

这给出了以下结果(绿色圆圈为 scale = 1,红色圆圈为 scale = 0.6

有什么想法吗?我真的卡住了。

对于这个问题,我看到的最简单的解决方案是使用 android 提供的容器来帮助我们获得这种行为。首先,您可以在约束布局中使用相对布局。这没有问题。使用两个相对布局,里面有一个图像视图,一个 us centerInParent 属性 来完成这个技巧。我强烈建议不要以编程方式调整视图的大小。使用您拥有的容器和工具。

我假设灰色矩形是正常的 Android Views,它们是您的 ConstraintLayout 的子项。它们是通过编程方式创建和添加的,因此您必须使用 setId() 方法为它们提供 ID。 ids 必须是正数并且在此视图层次结构中必须是唯一的。

要置于其中的图像的大小并不重要。你只需要给他们适当的约束。

    //Set the id for the greyRectangleLeft
    greyRectangleLeft.setId(1)
    //Create a new constraint set
    ConstraintSet set = new ConstraintSet();

    //Clone the current constraints from your ConstraintsLayout to avoid losing them
    set.clone(constraintLayout);

    //Create the ImageView and set its Bitmap
    ImageView imageView = new ImageView(this);
    imageView.setImageBitmap(bmap);
    //The imageView should have an id as well
    imageView.setId(2);

    //Add the constraints which will center the ImageView within the bounds of the grey_rectangle_left
    set.connect(imageView.getId(), ConstraintSet.START, greyRectangleLeft.getId(), ConstraintSet.START);
    set.connect(imageView.getId(), ConstraintSet.END, greyRectangleLeft.getId(), ConstraintSet.END);
    set.connect(imageView.getId(), ConstraintSet.TOP, greyRectangleLeft.getId(), ConstraintSet.TOP);
    set.connect(imageView.getId(), ConstraintSet.BOTTOM, greyRectangleLeft.getId(), ConstraintSet.BOTTOM);

    //Set the width and height of your ImageView to the desired width and height
    set.constrainWidth(imageView.getId(), bmap.getWidth());
    set.constrainHeight(imageView.getId(), bmap.getHeight());

    //Add the newly created ImageView to the ConstraintLayout
    constraintLayout.addView(imageView);
    //Apply the created constraints
    set.applyTo(constraintLayout);

重复 greyRectangleRight 和第二个 ImageView(但给它们不同的 ID)。

关于比例,我推荐使用ImageViewssetScaleX() and setScaleY()

您的代码确实按照您的预期执行,但您可能使用 getHeight()getWidth() 而不是 getMeasuredHeight()getMeasuredHeight()。这将导致获取根据 xml 计算的宽度和高度,而不是在布局阶段后设置的测量宽度和高度。

检查 this post 以了解更多关于它们之间的区别。