【问题标题】:Parallax effect with collapsing toolbar not working, image in header squished折叠工具栏的视差效果不起作用,标题中的图像被压扁
【发布时间】:2015-10-24 18:40:02
【问题描述】:

Design Support Library: Collapsing Toolbar Layout博客文章中有一个很好的视差效果的标题图片:

a simple test project at GitHub 我试图达到类似的效果 - 但由于某种原因,图像被压扁了:

activity_main.xml 中,我尝试了scaleType 的所有可能值,但图像仍然失真:

        <ImageView
            android:id="@+id/header_image_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/header"
            android:scaleType="centerCrop"
            app:layout_collapseMode="parallax" />

请问我在这里错过了什么?

更新:

我已尝试按照 Apurva 的建议更改为 match_parent(感谢 +1):

        <ImageView
            android:id="@+id/header_image_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/header2"
            android:scaleType="centerCrop"
            app:layout_collapseMode="parallax" />

但这无济于事 - 标题图像被压扁:

【问题讨论】:

标签: android parallax android-toolbar android-design-library android-coordinatorlayout


【解决方案1】:

默认情况下,背景会拉伸以适应。您应该在ImageView 上设置android:src 而不是android:background

<ImageView
    android:id="@+id/header_image_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/header2"
    android:scaleType="centerCrop"
    app:layout_collapseMode="parallax" />

【讨论】:

    【解决方案2】:

    你有没有提到教程作者使用SquareImageView

    他重写了onMeasure方法:

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    
        int width = getMeasuredWidth();
        setMeasuredDimension(width, width);
    }
    

    类实现here

    【讨论】:

    • 不,我已在该应用程序(activity_detail.xml)中将SquareImageView 替换为ImageView,它仍然有效,并且标题图像没有被压扁。在我的测试应用程序中,它被压扁了——我不知道为什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多