视差效果与折叠工具栏不起作用,标头中的图像被压缩。

3
在博客文章Design Support Library: Collapsing Toolbar Layout中,有一张带有漂亮视差效果的页头图片:

app screenshot

GitHub上的一个简单测试项目中,我试图实现类似的效果,但出现了图像被挤压的问题:

app screenshot

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" />

但是这并没有帮助——页眉图片被压缩了: 应用程序截图1 应用程序截图2

2
你应该尝试在ImageView中写入layout_height = wrap_content - Apurva
1
很遗憾,这并没有帮助:https://github.com/afarber/android-newbie/blob/master/MyCoordinator/screenshot.png - Alexander Farber
你看过这个吗:https://github.com/chrisbanes/cheesesquare/blob/master/app/src/main/res/layout/activity_detail.xml 和 https://github.com/chrisbanes/cheesesquare/blob/master/app/src/main/java/com/support/android/designlibdemo/CheeseDetailActivity.java - Jared Burrows
2个回答

7

默认情况下,背景将被拉伸以适应。在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);
}

这个类的实现在这里



不,我已经在该应用程序中(在activity_detail.xml中)将SquareImageView替换为ImageView,它仍然可以正常工作,并且标题图像没有被压缩。但在我的测试应用程序中,它被压缩了 - 我无法弄清楚原因。 - Alexander Farber

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接