【问题标题】:Strange issue with vector drawables. Jagged edges in ImageView矢量绘图的奇怪问题。 ImageView 中的锯齿状边缘
【发布时间】:2019-05-15 23:55:53
【问题描述】:

我在同一布局中有两个 ImageView,一个的宽度和高度设置为 56dp,另一个设置为 64dp。我在项目中添加了一个矢量资产(我从 Android Studio 中选择了一个预定义的矢量资产)。在为两个 ImageView 设置相同的可绘制矢量时,我使用 ImageView.setImageResource(int resId)

由于某种原因,这会导致最小的 ImageViews 具有锯齿状边缘。我将 Drawables 设置为 ImageViews 的顺序并不重要。哪个 ImageView 是最小的或它们有什么尺寸都没有关系。只要一个比另一个小,最小的总是有锯齿状的边缘。

编译SdkVersion 28
minSdkVersion 28

Screenshot

【问题讨论】:

    标签: java android


    【解决方案1】:

    这是因为Drawable 实例从同一资源加载共享 ConstantState 实例。我不确定ConstantState 的哪一部分对此负责,但您在功能上获得了两个不同的 64x64 可绘制对象,其中一个(严重)缩小了。

    您可以通过使用Drawable.mutate() 方法来解决此问题,以确保您的两个 ImageView 获得具有不同 ConstantState 的 Drawable。你只需要mutate()两个drawable之一,你选择哪一个并不重要。

    当然,这意味着您将不得不获得一个实际的 Drawable 实例,而不是使用 setImageResource()

    ImageView large = findViewById(R.id.large);
    large.setImageResource(R.drawable.ic_arrow_drop_down_circle_black_24dp);
    
    ImageView small = findViewById(R.id.small);
    small.setImageDrawable(AppCompatResources.getDrawable(this, R.drawable.ic_arrow_drop_down_circle_black_24dp).mutate());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-14
      相关资源
      最近更新 更多