ImageView的ListView中一个可扩展最大宽度 [英] ImageView in ListView that expands maximum width

查看:160
本文介绍了ImageView的ListView中一个可扩展最大宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的ListView ,其项目的ImageView S,为了讨论的缘故。我想要的图像缩放到的宽度的的的ListView 的行,并保持它的纵横比。这里的的ListView的一个简化版本的项目布局,

i have a ListView whose items are ImageViews, for the sake of discussion. i want the image to scale to the width of the ListView's rows, and maintain it's aspect ratio. here's a simplified version of the ListView's item layout,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:padding="4dp" >

        <FrameLayout
            android:id="@+id/page_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="visible" >

            <ImageView
                android:id="@+id/image"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:scaleType="fitCenter"
                android:src="@drawable/some_image" />
        </FrameLayout>
    </FrameLayout>

</LinearLayout>

的ImageView 的宽度 match_parent ,和它的所有祖先都一样。布局设计显示了的ImageView 的尺寸跨越整个宽度,但在图像内没有。

the ImageView's width is match_parent, and all ancestors of it are the same. layout designer shows that the ImageView's dimensions span the entire width, but the image inside does not.

编辑:图像缩放正确。问题是,列表视图行的高度是这样的宽度不能扩展到正确的大小。如果我使用 fitXY ,它缩放宽度,但随后的纵横比不正确。

the image is scaling correctly. the problem is that the height of the list view row is such that the width can't scale to the right size. if i use fitXY, it does scale the width, but then the aspect ratio is incorrect.

什么想法?
谢谢。

any ideas? thanks.

推荐答案

回答我的问题...

我找不到任何方式做到这一点,而无需创建一个自定义视图。简而言之,自定义视图设置它自己的尺寸,它背后的绘制真正的(不缩放)尺寸。

i couldn't find any way to accomplish this without creating a custom view. in a nutshell, the custom view sets it's own dimensions to the real (not scaled) dimensions of the drawable behind it.

public class ResizableImageView extends ImageView {

    public static interface OnSizeChangedListener {

        void onSizeChanged(int width, int height);
    }

    private OnSizeChangedListener onSizeChangedListener = null;

    public ResizableImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = width * getDrawable().getIntrinsicHeight() / getDrawable().getIntrinsicWidth();
        setMeasuredDimension(width, height);
        if (onSizeChangedListener != null) {
            onSizeChangedListener.onSizeChanged(width, height);
        }
    }

    public void setOnSizeChangedListener(OnSizeChangedListener listener) {
        this.onSizeChangedListener = listener;
    }
}

在我来说,我需要的,以便获得调整值大小的其他意见,因此监听器接口。

in my case i needed to obtain the resized value in order to size the other views, hence the listener interface.

这篇关于ImageView的ListView中一个可扩展最大宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆