为什么矢量可绘制比例未达到预期的缩放比例? [英] Why isn't my vector drawable scaling as expected?

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

问题描述

我正在尝试在我的Android应用中使用矢量可绘制对象.来自 http://developer.android.com/training/material/drawables.html(强调我的):

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

在Android 5.0(API级别21)及更高版本中,您可以定义矢量可绘制对象,可缩放而不会丢失定义.

使用此可绘制对象:

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

和此ImageView:

and this ImageView:

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

尝试以400dp(在运行棒棒糖的大约2015年移动设备上较大的高分辨率)上显示图标时,会产生此模糊的图像:

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

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

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.

我的问题:

1)为什么可绘制矢量中存在宽度/高度规范?我认为这些的全部目的是它们无损地进行缩放,从而使宽度和高度的定义毫无意义吗?

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

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)如何有效地使用width/height属性,以及viewportWidth/Height有什么区别?

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

其他详细信息:

  • 设备正在运行API 22
  • 将Android Studio v1.5.1与Gradle版本1.5.0一起使用
  • 清单是编译的,目标级别是23,最低级别是15.我也尝试过将最低级别提高到21,但这没什么区别.
  • 反编译APK(最低级别设置为21)显示了drawable文件夹中的单个XML资源.没有产生光栅图像.

推荐答案

此处有关于此问题的新信息:

There is new info about this issue here:

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

好像使用 android:scaleType="fitXY"将使其在棒棒糖上正确缩放.

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

来自Google工程师:

From a Google engineer:

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

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

棉花糖Vs棒棒糖是由于特殊的洗牙处理 加到棉花糖中.

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

此外,您的评论是:正确的行为:矢量可绘制 应该扩展而不会损失质量.因此,如果我们要使用相同的资产 在我们的应用程序中有3种不同的尺寸,我们不必重复 使用不同的硬编码大小的vector_drawable.xml 3次.

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

即使我完全同意这种情况,实际上 Android平台对性能的关注使我们无法达到 理想的世界呢.所以实际上建议使用3种不同的 如果确定要使用vector_drawable.xml以获得更好的性能 同时在屏幕上绘制3种不同的大小.

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天全站免登陆