AppCompat v23.2  -- Vectors的时代

简介: 表示速度真快啊,刚发布的23.2,就有人写blog了 原文:https://medium.com/@chrisbanes/appcompat-v23-2-age-of-the-vectors-91cbafa87c88#.

这里写图片描述

表示速度真快啊,刚发布的23.2,就有人写blog了
原文:https://medium.com/@chrisbanes/appcompat-v23-2-age-of-the-vectors-91cbafa87c88#.90g203w5g
23.2:http://android-developers.blogspot.com/2016/02/android-support-library-232.html

相信你已经看到了app23.2的支持库的更新日志了,现在在支持库中已经兼容了矢量图:VectorDrawableCompat和Animated VectorDrawableCompat

它被实现成了一个独立的功能模块。众所周知:开发者想使用资源图片,我们已经把支持矢量图绘制添加到了APP中。
一下是几个整合的原因,包括:

  • 允许开发者在所有的Android2.1以上的设备更早的用上图
  • 允许APP本身能够用矢量图。它本身已经减少了APP AAR的70KB左右。这听起来不算很大,但是对于设备来说节省是很重要的。快速的节省是为了存储和传输。

*首先
VectorDrawableCompat依赖与AAPT的一些功能,它能保持最近矢量图使用的添加的属性ID,以便他们可以被v21之前的引用。

如果你没有启用这个标志,当你在KitKat上运行你的APP你会看到以下(或者类似)错误:

Caused by: android.content.res.Resources$NotFoundException: File res/drawable-v19/abc_ic_ab_back_material.xml from drawable resource ID #0x7f020016
at android.content.res.Resources.loadDrawable(Resources.java:2097)
at android.content.res.Resources.getDrawable(Resources.java:700)

启用标志位
我猜你们中的大多数都正在用Gradle。
如果你正在用Gradle插件v2.0或者更高,我有一个简洁方法去启用它:

android {
  defaultConfig {
    vectorDrawables.useSupportLibrary = true
  }
}

如果你还没有更新,在用v1.5后者更低的版本,你需要在你的build.gradle文件里添加以下内容:

android {
  defaultConfig {
    // Stops the Gradle plugin’s automatic rasterization of vectors
    generatedDensities = []
  }
  // Flag to tell aapt to keep the attribute ids around
  aaptOptions {
    additionalParameters "--no-version-vectors"
  }
}

怎么在项目中用我自己的矢量图资源?
在我们开始之前应该做一些准备。当我们在用AppCompat时,VectorDrawableCompat不仅用在Api20或者更低。这意味着你当你在Api21或者更高时你在用framework层的VectorDrawable类。在用api21+的framework create()方法是不同的,是一个代理项目。

所以如果你的minSdkVersion<21而且你想在你的APP中用一个矢量资源的时候,首先你应该检查这个资源应该用在api21+的设备上,只是为了一个全面的检查。

起作用之后,我们该怎么用?
在api7+有两种使用矢量图的方法:

  1. AppCompatImageView
    AppCompat的“injects”代替了许多框架中的组件。我们已经加入了app:srcCompat属性去支持VectorDrawableCompat。它代替了android:src属性,并且你也可以安全的使用那些不属于矢量图的资源。下面是一个我们已经用过的矢量图的例子:
    res/drawable/ic_search.xml
<vector xmlns:android="..."
        android:width="24dp"
        android:height="24dp"
        android:viewportWidth="24.0"
        android:viewportHeight="24.0"
        android:tint="?attr/colorControlNormal">
    <path
        android:pathData="..."
        android:fillColor="@android:color/white"/>
</vector>

一个ImageView的声明:

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/ic_search"/>

你也可以这样设置它:

ImageView iv = (ImageView) findViewById(...);
iv.setImageResource(R.drawable.ic_search);

这样和ImageButton相同的用法。

“魔法方式”
AppCompat能够拦截一些图片引用,你在工作的时候能够用所有的标准的属性,每一个都会工作。
让我告诉你工作什么:
其中引用只包含一个矢量资源等资源可绘容器可绘制。
例如,一个StateListDrawable它引用其中包含的载体的其他文件。

res/drawable/state_list_icon.xml

<selector xmlns:android="...">
    <item android:state_checked="true"   
            android:drawable="@drawable/checked_icon" />
    <item android:drawable="@drawable/icon" />
</selector>

res/drawable/checked_icon.xml

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="32dp"
    android:viewportWidth="32"
    android:height="32dp"
    android:viewportHeight="32">

    ...

</vector>

作为一个TextView的复合图:

<TextView
    android:drawableLeft="@drawable/state_list_icon" />

作为一个RadioButton的背景图:

<RadioButton
    android:button="@drawable/state_list_icon" />

作为一个ImageView的背景图:

<ImageView
    android:src="@drawable/state_list_icon" />

你不得不去用StateListDrawable,它也和InsetDrawable,LayerDrawable,LevelListDrawable,RotateDrawable容器一起工作。规则仅仅是矢量图需要在一个分离的文件中。

矢量动画是什么?
到目前为止我们仅仅讨论了“static”的矢量图,所以让我们谈谈矢量动画。他们用同样的方式工作,但是他们仅仅在api 11+可用。如果你尝试在api 10或者更低去用矢量动画,你会得到一个”null”或者什么都不显示。

下面是一些矢量图能够运行在小于api 21平台下的限制:

  • Path Morphing (PathType evaluator)
  • 路径拦截。被用于代替系统定义的类似LinearInterpolator的拦截器
  • 沿路径移动。很少备用,这可以左右移动,沿着任何路径。

总之,动画的声明已经可用,并且这个功能是基于你平台运行的APP。

“魔法方式”怎么工作?
如果对它的实现不感兴趣你可以跳过这段。
目前没有方法在Android平台使用从资源的自定义绘制的实现,所以下面这样是不可以的:
res/drawable/my_awesome_drawable.xml

<my.package.SuperAwesomeDrawable xmlns:app="..."
    app:customAttr1="32dp"
    app:customAttr2="32dp">

    ...

</my.package.SuperAwesomeDrawable>

为了迭代:之前的代码在当前是不可以的
所以你可能会问普通的drawable是怎么工作的?当你设置一个resource的属性时,manifest本身会作为一个TypedArray去实现View,Drawable,etc,去调用obtainStyledAttributes()方法。

下面是一个InsetDrawable的例子:

Resources r = ...;
int drawableRes =
     a.getResourceId(android.R.styleable.InsetDrawable_drawable, 0);        
if (drawableRes != 0) {
    dr = r.getDrawable(drawableRes);
}

有趣么?正如我说的,这是所有的实现细节所以不要担心不不理解。

相关文章
|
28天前
|
Android开发 开发者
Error:Could not find com.android.support:appcompat-v7:27.0.2.
Error:Could not find com.android.support:appcompat-v7:27.0.2.
16 0
|
1月前
|
Java 数据库连接 Apache
Correct the classpath of your application so that it contains compatible versions of the classes com
Correct the classpath of your application so that it contains compatible versions of the classes com
43 0
|
8月前
|
人工智能 编解码 自然语言处理
论文解读:Inpaint Anything: Segment Anything Meets Image Inpainting
论文解读:Inpaint Anything: Segment Anything Meets Image Inpainting
259 0
|
9月前
|
存储 JSON 缓存
译 | Packages as layers, not groups
译 | Packages as layers, not groups
48 0
Could not find com.android.support:appcompat-v7:25.3.1.
Could not find com.android.support:appcompat-v7:25.3.1.
62 0
Re1:读论文 C&S (Correct and Smooth) Combining Label Propagation and Simple Models Out-performs Graph Ne
Re1:读论文 C&S (Correct and Smooth) Combining Label Propagation and Simple Models Out-performs Graph Ne
Re1:读论文 C&S (Correct and Smooth) Combining Label Propagation and Simple Models Out-performs Graph Ne
|
Android开发
【错误记录】Android Studio 编译报错 ( AppCompat does not support the current theme features )
【错误记录】Android Studio 编译报错 ( AppCompat does not support the current theme features )
267 0
【错误记录】Android Studio 编译报错 ( AppCompat does not support the current theme features )
成功解决model_selection\_search.py:584: DeprecationWarning: "fit_params" as a constructor argument was d
成功解决model_selection\_search.py:584: DeprecationWarning: "fit_params" as a constructor argument was d
|
TensorFlow 算法框架/工具 Python
Taking advantage of context features
In the featurization tutorial we incorporated multiple features beyond just user and movie identifiers into our models, but we haven't explored whether those features improve model accuracy.
170 0