请选择 进入手机版 | 继续访问电脑版
  • 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

android - 给定矩形边界以编程方式缩放和居中 imageView

[复制链接]
菜鸟教程小白 发表于 2022-12-9 06:51:37 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我的问题

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

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

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

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

另外,我想要一个 (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

enter image description here

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



Best Answer-推荐答案


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

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

    //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)。

关于比例,我推荐使用 setScaleX()setScaleY() ImageViews.

关于android - 给定矩形边界以编程方式缩放和居中 imageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48583001/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap