如何在父级布局中正确缩放imageview?

时间:2019-03-07 17:39:51

标签: android android-layout android-imageview android-relativelayout

在我的应用中。我有一个包含ImageView的RelativeLayout。 RelativeLayout水平设置为MATCH_PARENT,垂直设置为WRAP_CONTENT。

ImageView在水平和垂直方向均设置为WRAP_CONTENT。可绘制的图像是24x24的矢量图标。

由于图标太小而无法显示,因此我希望将其尺寸加倍。那是问题所在:

// Parent RelativeLayout
    RelativeLayout titleLayout = new RelativeLayout(MyApplication.getContext());
    titleLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//Icon
    RelativeLayout.LayoutParams information_icon_layout = new RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    information_icon_layout.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

    ImageView information_icon = new ImageView(MyApplication.getContext());
    information_icon.setLayoutParams(information_icon_layout);
    information_icon.setPadding(
            getResources().getDimensionPixelSize(R.dimen.schedule_element_title_layout_padding_left),
            getResources().getDimensionPixelSize(R.dimen.schedule_element_title_layout_padding_top),
            getResources().getDimensionPixelSize(R.dimen.schedule_element_title_layout_padding_right),
            getResources().getDimensionPixelSize(R.dimen.schedule_element_title_layout_padding_bottom)
    );
    information_icon.setImageDrawable(MyApplication.getContext().getResources().getDrawable(R.drawable.ic_info_outline_black_24dp));
    information_icon.setScaleX(2);
    information_icon.setScaleY(2);
    information_icon.setAdjustViewBounds(true);

图标按预期比例缩放,但是父级RelativeLayout似乎并未考虑此更改,并且仍使用Icon的原始大小(24x24)计算其垂直大小。 结果,该图标正在缩放到父级之外,并且只有一部分可见。

我的问题:如何告诉父级RelativeLayout考虑图标的缩放大小?

1 个答案:

答案 0 :(得分:0)

使用具有3个主要属性的imageview的scaletype属性 中央, 中心作物 居中 如果要完全刮擦图像,它还具有以下属性:fit center,fit end,fit start,fitxy。您可以在xml和Java中使用此属性。 这是链接

scaletyper

相关问题