3

我正在使用这里描述的技术。以下行适用于我的所有设备,除了我的Nexus 4

int imageWidth = horizontalScrollView.getChildAt(0).getMeasuredWidth();

布局很简单:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <HorizontalScrollView
        android:id="@+id/main_scroller"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <ImageView
            android:id="@+id/main_image"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"
            android:scaleType="centerCrop" />
    </HorizontalScrollView>
</RelativeLayout>

为此HorizontalScrollView,我以编程方式创建了一个大约 500x1000 像素的位图。因此,图像将被拉伸以填充大约1200x2400(请参阅centerCrop)。因此,宽度ImageView应该是 2400。它是!在我的 Nexus 7、Samsung S3 Mini 和其他一些低密度设备上。但不是在我的 Nexus 4 上!在 Nexus 4 上,它的高度确实被拉伸了,但宽度却没有(是的,难以置信):1000x1000 像素。我都试过了getWidth()getMeasuredWidth()。当我尝试滚动它时,我只能水平看到 50% 的图像(但垂直显示 100%)。

更新 1:

查看mDrawMatrixmy ImageView,我可以看到它有所不同:

Nexus 7:矩阵{[2.2979167, 0.0, 0.0][0.0, 2.2979167, 0.0][0.0, 0.0, 1.0]}

Nexus 4:矩阵{[2.1458333, 0.0, -623.0 ][0.0, 2.1458333, 0.0][0.0, 0.0, 1.0]}

更新 2:

这是ImageView.onMeasure()Android 4.2.2 中的一个错误,已在 4.3 中修复。现在的问题是,如何在 4.2.2 中添加此修复程序?覆盖onMeasure()不是 100% 微不足道的,因为正在调用许多私有函数。

4

1 回答 1

3

我在三星 Galaxy S4 4.2.2 和 S3 4.1.1 上遇到了同样的问题,无论我做什么,以编程方式设置的位图都不会缩放以填充其父 ViewGroup 的宽度。我尝试了所有 scaleType 组合均无济于事。

对我来说,解决方案是在显示之前将位图上的密度设置为特定值。

        Bitmap myBitmap = ... 

        // S3 and S4 get confused about displaying bitmaps without a specific density,
        // and fail to scale them properly. Setting the density to this value helps (is possible ANY density helps, but haven't tested this)
        myBitmap.setDensity(DisplayMetrics.DENSITY_HIGH);

        myImageView.setImageBitmap(myBitmap);
于 2014-04-10T14:48:31.260 回答