Jetpack Compose - 缩放时图像抗锯齿不如原生 ImageView 平滑

问题描述 投票:0回答:1

我正在重构屏幕以使用 jetpack compose。它有一个位于背景顶部的

ImageView
,其中包含一个大的 1920x795dp PNG 图像(mdpi 和 hdpi 存储桶),该图像会按比例缩小。更改为使用
Image
后,它现在具有明显锯齿状的边缘。

ImageView

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:scaleType="fitXY"
    android:layout_marginBottom="-90dp"
    android:src="@drawable/ic_swoosh"/>

Image

Image(
    modifier = Modifier
        .fillMaxWidth()
        .offset(y = 90.dp)
        .align(Alignment.BottomCenter),
    painter = painterResource(R.drawable.ic_swoosh),
    contentDescription = null,
    contentScale = ContentScale.Fit,
)

深入研究

Image
的源代码,它使用
AndroidPaint
,据说它已启用:

internal fun makeNativePaint() =
    android.graphics.Paint(android.graphics.Paint.ANTI_ALIAS_FLAG)

如果我将

ImageView
包裹在
AndroidView
中,则问题不存在。我还可以验证它的位置和大小是否与
Image
版本完全相同,但它已正确消除锯齿。

AndroidView(
    modifier = Modifier
        .fillMaxWidth()
        .offset(y = 90.dp)
        .align(Alignment.BottomCenter),
    factory = {
        ImageView(it).apply {
            scaleType = android.widget.ImageView.ScaleType.FIT_XY
            setImageResource(R.drawable.ic_swoosh)
        }
    }
)

如果我用

contentScale = ContentScale.None
删除缩放,图像也会平滑,所以它看起来确实是由于缩小所致。删除
contentScale
完全会出现同样的问题,因为图像会自动缩小以适合视图。

编辑:如果我删除 hdpi 变体,它也会变得平滑!

android-jetpack-compose
1个回答
0
投票

作为解决方法,删除图像的 hdpi 变体可以解决该问题。

© www.soinside.com 2019 - 2024. All rights reserved.