为什么我的矢量可绘制缩放不符合预期? [英] Why isn't my vector drawable scaling as expected?

查看:20
本文介绍了为什么我的矢量可绘制缩放不符合预期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的 Android 应用中使用矢量可绘制对象.来自

将矢量 drawable 定义中的宽度和高度更改为 200dp,显着改善了 400dp 渲染大小的情况.但是,将其设置为 TextView 元素的可绘制对象(即文本左侧的图标)现在会创建一个巨大的图标.

我的问题:

1) 为什么矢量 drawable 中有宽度/高度规范?我认为这些的全部意义在于它们无损地放大和缩小使宽度和高度在其定义中毫无意义?

2) 是否可以使用单个矢量可绘制对象,它可以在 TextView 上用作 24dp 可绘制对象,但可以很好地扩展以使用更大的图像?例如.我如何避免创建多个不同大小的矢量可绘制对象,而是使用可缩放到我的渲染要求的​​对象?

3) 如何有效地使用宽度/高度属性,与视口宽度/高度有什么区别?

其他详细信息:

  • 设备正在运行 API 22
  • 将 Android Studio v1.5.1 与 Gradle 1.5.0 版结合使用
  • 清单是编译和目标级别 23,最低级别 15.我也尝试将最低级别移动到 21,但这没有任何区别.
  • 反编译 APK(最小级别设置为 21)会在 drawable 文件夹中显示单个 XML 资源.不生成光栅化图像.

解决方案

这里有关于这个问题的新信息:

https://code.google.com/p/android/issues/detail?id=202019

看起来像使用android:scaleType="fitXY" 将使其在 Lollipop 上正确缩放.

来自 Google 工程师:

<块引用>

让我知道 scaleType='fitXY' 是否可以解决您的问题,在以使图像看起来清晰.

棉花糖 Vs Lollipop 是由于特殊的缩放处理添加到棉花糖中.

另外,对于您的评论:正确的行为:矢量可绘制应该在没有质量损失的情况下进行扩展.所以如果我们想使用相同的资产在我们的应用程序中有 3 种不同的尺寸,我们不必重复vector_drawable.xml 3 次,具有不同的硬编码大小."

尽管我完全同意应该是这样,但实际上,Android 平台存在我们尚未达到的性能问题理想世界呢.所以实际上建议使用 3 种不同的如果您确定需要,vector_drawable.xml 以获得更好的性能同时在屏幕上绘制 3 个不同的尺寸.

技术细节基本上是我们在钩子下使用位图缓存复杂的路径渲染,这样我们就可以得到最好的重绘性能,与重绘位图 drawable 相当.

I am attempting to use vector drawables in my Android app. From http://developer.android.com/training/material/drawables.html (emphasis mine):

In Android 5.0 (API Level 21) and above, you can define vector drawables, which scale without losing definition.

Using this drawable:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:width="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path android:fillColor="@color/colorPrimary" android:pathData="M14,20A2,2 0 0,1 12,22A2,2 0 0,1 10,20H14M12,2A1,1 0 0,1 13,3V4.08C15.84,4.56 18,7.03 18,10V16L21,19H3L6,16V10C6,7.03 8.16,4.56 11,4.08V3A1,1 0 0,1 12,2Z" />

and this ImageView:

<ImageView
    android:layout_width="400dp"
    android:layout_height="400dp"
    android:src="@drawable/icon_bell"/>

produces this blurry image when attempting to display the icon at 400dp (on a largish high-res circa 2015 mobile device running lollipop):

Changing the width and height in the definition of the vector drawable to 200dp significantly improves the situation at the 400dp rendered size. However, setting this as a drawable for a TextView element (i.e. icon to the left of the text) now creates a huge icon.

My questions:

1) Why is there a width/height specification in the vector drawable? I thought the entire point of these is that they scale up and down losslessly making width and height meaningless in its definition?

2) Is it possible to use a single vector drawable which works as a 24dp drawable on a TextView but scales up well to use as much larger images too? E.g. how do I avoid creating multiple vector drawables of different sizes and instead use one which scales to my rendered requirements?

3) How do I effectively use the width/height attributes and what is the difference with viewportWidth/Height?

Additional details:

  • Device is running API 22
  • Using Android Studio v1.5.1 with Gradle version 1.5.0
  • Manifest is compile and target level 23, min level 15. I've also tried moving min level to 21, but this made no difference.
  • Decompiling the APK (with min level set to 21) shows a single XML resource in the drawable folder. No rasterized images are produced.

解决方案

There is new info about this issue here:

https://code.google.com/p/android/issues/detail?id=202019

It looks like using android:scaleType="fitXY" will make it scale correctly on Lollipop.

From a Google engineer:

Hi, Let me know if scaleType='fitXY' can be a workaround for you , in order to get the image look sharp.

The marshmallow Vs Lollipop is due to a special scaling treatment added into marshmallow.

Also, for your comments: " Correct behavior: The vector drawable should scale without quality loss. So if we want to use the same asset in 3 different sizes in our application, we don't have to duplicate vector_drawable.xml 3 times with different hardcoded sizes. "

Even though I totally agree this should be the case, in reality, the Android platform has performance concern such that we have not reach the ideal world yet. So it is actually recommended to use 3 different vector_drawable.xml for better performance if you are sure you want to draw 3 different size on the screen at the same time.

The technical detail is basically we are using a bitmap under the hook to cache the complex path rendering, such that we can get the best redrawing performance, on a par with redrawing a bitmap drawable.

这篇关于为什么我的矢量可绘制缩放不符合预期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆