【问题标题】:Programatically scaling and centering imageView given rectangular bounds给定矩形边界以编程方式缩放和居中 imageView
【发布时间】:2018-07-12 23:21:53
【问题描述】:

我的问题

假设我有一个位图 - 称它为 bmap,尺寸为 100x75 (bmap.getWidth() x bmap.getHeight())

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

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

注意:因为我使用的是ContraintLayout,所以没有父母或相对论的概念。

此外,我想要(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 代表红色圆圈

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

【问题讨论】:

  • ConstraintLayout 里面的圆和矩形都在吗?
  • 如何以及何时获得宽度、高度、x 和 y 值?
  • 圆形和矩形都在 ConstraintLayout 中。宽度和高度是从矩形获得的。 x 和 y 是从矩形中获得的
  • 我从来没有这样做过,没有

标签: android bitmap imageview android-constraintlayout


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我假设灰色矩形是普通的 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)。

    关于规模,我推荐使用ImageViews中的setScaleX()setScaleY()

    【讨论】:

    • 灰色矩形是以编程方式创建的,没有 ID
    • 所以在调用 set.connect() 时使用 grayRectangle.getId() 而不是 R.id.grey_rectangle。
    • 好的,我试一试
    • 检查编辑后的答案,您必须为您的ImageViewsgreyRectangles 设置ID 才能让ConstraintLayout 正常工作。如果它不起作用,请告诉我 - 我创建了一个示例项目,它似乎按照您的意愿居中。如果需要,我会分享代码。
    【解决方案3】:

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

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

    【讨论】:

    • getWidth 和 getHeight 来自位图,而不是 imageView
    • @GregPeckory 当我编写 getWidth() 和 getHeight() 时,我正在谈论如何获取宽度和高度变量的值。我解释您的描述的方式是它们来自灰色矩形视图。
    猜你喜欢
    • 1970-01-01
    • 2019-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-05
    • 2021-04-07
    相关资源
    最近更新 更多