【问题标题】:ImageView fills different screen sizes, but maintaing the aspect ratioImageView 填充不同的屏幕尺寸,但保持纵横比
【发布时间】:2012-08-23 16:04:19
【问题描述】:

我在图像视图中有一个矩形图像。我想在保持纵横比的同时,使图像适合不同的屏幕尺寸。

在较小的屏幕上它可以正常工作,但在较大的屏幕上它不会被拉伸。图片底部仍有一些空隙。

这是我的代码:

LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT); 
LayoutParams params1 = new LayoutParam(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);


mMainLayout = new LinearLayout(context);
mMainLayout.setLayoutParams(params);


backgroundImage = new ImageView(context);
backgroundImage.setId(0);
backgroundImage.setAdjustViewBounds(true);
backgroundImage.setScaleType(ScaleType.FIT_CENTER);
backgroundImage.setLayoutParams(params1);
backgroundImage.setImageResource(R.drawable.background_circles_en); 

mMainLayout.addView(backgroundImage);

我使用了fill_parent、wrap_content 与多个 scaleTypes 的许多组合:fitCenter、centerInside,它们都以正确的纵横比绘制图像,但它们都没有真正放大图像并且ImageView 本身,导致 TextView 一直被推离屏幕、ImageView 内有空白区域或图像未缩放。

请给出正确的组合,使其适用于不同的屏幕尺寸。

【问题讨论】:

    标签: android android-layout fullscreen


    【解决方案1】:

    Privide ScaleType FIT_XY,它将图像缩放到 x 和 y 维度,与纵横比无关。

    【讨论】:

    • 它没用。我以前也尝试过,但没有改变。在更大的屏幕中,页面底部也有相同的空间
    • 这是因为您将包装内容布局参数设置为图像视图,而不是将布局参数设置为参数。
    • thnx jeet ..它工作了..但图像被拉伸到 y 轴。我们该如何纠正呢?
    • 不是你想要的吗,你想将图像缩放到目标宽度和高度,而不考虑纵横比,不确定但你可以使用 ScaleType 矩阵,我认为
    猜你喜欢
    • 1970-01-01
    • 2012-03-26
    • 1970-01-01
    • 2012-10-06
    • 2012-06-05
    • 2016-11-27
    • 2014-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多