【问题标题】:Android UI design automatic ImageView re-sizeAndroid UI设计自动ImageView re-size
【发布时间】:2013-07-18 21:06:11
【问题描述】:

ImageView 获取图像并包装内容,因此如果图像的尺寸为 100x100,则 ImageView 具有相同的尺寸。我只想指定此 ImageViewlayout_height 以及当它与图像匹配以正确调整 ImageView 的 layout_width 大小时

这是一个例子:

图像尺寸 = 100x100;

ImageViewlayout_height = 200;

ImageViewlayout_width 应该变成 200 而不是 100。

这可能吗?

【问题讨论】:

    标签: android user-interface imageview android-imageview ui-design


    【解决方案1】:

    您需要创建一个自定义 ImageView 并覆盖 onMeasure 以使视图始终为正方形。

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    
        SQUARE_DIMENSION  = this.getMeasuredHeight(); // whatever height you want here
    
        this.setMeasuredDimension(SQUARE_DIMENSION, SQUARE_DIMENSION);
    }
    

    【讨论】:

    • 你把我的例子当作私人案例。您还可以拥有尺寸为 100x300 的图像,然后如果 layout_width = 200,layout_height 应该是 600 --> ImageView 200x600。我给出的例子很容易计算整数,但它可能更复杂。我希望你现在明白我的意思了。
    • 好吧,我的示例仍然是,如果您想准确控制视图的大小,则需要覆盖 onMeasure。您使用什么来确定大小取决于您。在您的情况下,听起来您在向上或向下缩放图像视图时试图保持图像的纵横比。如果是这种情况,那么您需要获取图像大小并相应地调整代码。
    猜你喜欢
    • 2020-11-25
    • 1970-01-01
    • 2012-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-12
    • 1970-01-01
    • 2012-04-21
    相关资源
    最近更新 更多