- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试使用 svgs 路径和动画矢量制作像 youtube 播放/暂停这样的动画。
Strings.xml
<resources>
<string name="app_name">AnimatedSvgTest</string>
<string name="svg_triangle">
M0,0 L0,24 12,12 0,0
</string>
<string name="svg_pause">
M0,0 L0,24 M12,0 L12,24
</string>
</resources>
anim/path_morph.xml
:
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:duration="4000"
android:propertyName="pathData"
android:valueFrom="@string/svg_triangle"
android:valueTo="@string/svg_pause"
android:valueType="pathType" />
</set>
drawable/avd.xml
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/ic_play_arrow_24dp">
<target
android:name="play"
android:animation="@anim/path_morph" />
</animated-vector>
drawable/ic_pause_arrow.xml
:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<path
android:pathData="@string/svg_pause"
android:strokeColor="#000"
android:strokeWidth="1.0" />
</vector>
drawable/ic_play_arrow.xml
:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="100dp"
android:height="100dp"
android:viewportHeight="100.0"
android:viewportWidth="100.0">
<path
android:fillColor="#FF000000"
android:pathData="@string/svg_triangle" />
</vector>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<ImageButton
android:id="@+id/button"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:src="@drawable/avd" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageButton button = (ImageButton) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Animatable animatable = (Animatable) button.getDrawable();
animatable.start();
}
});
}
}
但我仍然得到 Caused by: android.view.InflateException: Binary XML file line #2 Can't morph from
崩溃:/
最佳答案
为此,您需要使路径兼容。将有多种方法可以做到这一点,具体取决于您希望实际转换看起来像什么,您基本上可以使用一些技巧来确保 pathData
这两个 drawable 具有相同数量和相同顺序的命令类型。在这种情况下,这意味着一个三角形是使用与两个矩形相同的路径格式制作的。
这里的另一个问题是,在三角形的情况下,您使用的是 fillColor,所以您在屏幕上看到的是在您绘制的路径内填充的内容,但是暂停符号使用的是粗细为 1.0 的线条,所以呢您在屏幕上看到的实际上是两条粗线,而不是填充形状。
我建议您更改此设置,以便两者都使用 fillColor 而不是彩色粗线,然后使路径兼容。
这是一个如何使它们兼容的示例。目前不兼容的路径如下所示:
<string name="svg_triangle">
M0,0 L0,24 12,12 0,0
</string>
<string name="svg_pause">
M0,0 L0,24 M12,0 L12,24
</string>
这些不兼容,因为它们没有相同数量的 M
& L
命令或相同数量的坐标。兼容路径如下所示:
<string name="svg_triangle">
M0,0 L0,24 12,12 0,0 M12,0 L12,0 12,0 12,0
</string>
<string name="svg_pause">
M0,0 L0,24 1,24 1,0 M12,0 L12,24 13,24 13,0
</string>
你应该注意到它们现在有相同的数字 M
和 L
具有相同数量坐标的命令。我添加了 M12,0 L12,0 12,0 12,0
到三角形的路径;这部分在屏幕上不可见,因为它并没有真正勾勒出一个形状,所有的点都在坐标 12,0 处。对于暂停按钮,我对其进行了更多更改,之前它只有两行,现在我改为勾勒出形状。为了使它看起来正确,请更改 <path>
节drawable/ic_pause_arrow.xml
使用与播放相同的 fillColor,所以:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<path
android:fillColor="#FF000000"
android:pathData="@string/svg_pause"/>
</vector>
您现在应该有两个兼容变形的矢量可绘制对象。请注意,我没有专门尝试过转换,它可能看起来不像您想要的那样,因此可能需要进行一些实验。 I wrote a tutorial on my blog我认为这是相关的,我详细介绍了一些更复杂的例子,以及更多关于问题和技术的背景,包括源代码。希望这会有所帮助。
关于android - Youtube Play/pause Animated Vector Drawable on android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33727819/
我知道如果 Android 在所需的文件夹中找不到图标,它会尽力而为,但我看到有些地方他们建议上述所有内容,而在其他地方他们不包括 drawable ? 那么,如果所有其他的都充满了我的标签图标图像,
这个问题在这里已经有了答案: I am confused with drawable-hdpi and drawable-mdpi (4 个答案) 关闭 9 年前。 我真的很困惑,res下的andr
我正在进行一个非常大的项目,该项目是在 android 1.5 时代开始的。 这个项目随着 android 的发展而发展,目前支持 2.3 及更高版本。 现在我遇到了这个问题,我在 drawable-
所有 Android 文档都提到了“drawable”目录。但是,当我在 Eclipse 中创建 Android 项目时,它不会创建“/res/drawable”目录,但会创建“/res/drawab
我正在使用 Android 1.5,但我现在已迁移到最新版本。所以 Android 1.5 中只有一个“drawable”文件夹,但现在 Android 项目中有三个不同的文件夹用于存储图像。 我发现
当我创建一个新项目时,我的 Android studio 1.0 不显示 drawable-hdpi、drawable-xhdpi、drawable-mdpi 和 drawable-xxhdpi。 我
我将 9-patch 文件从 /drawable 移到了 /drawable-hdpi 因为错误日志推荐使用 drawable-* 提供密度优化图像。 问题是,AppWidget 现在看起来不一样了。
我可以知道这个错误是什么以及对此的解决方案吗?此错误在 Debug模式和 APK 中随机 react 原生应用程序时发生。重现这个错误很困难。 我在 github github-https://git
我能够将不同分辨率的图像保存在 res 文件夹的 drawable-hdpi、drawable-mdpi、drawable-ldpi 中,但不知道如何从 assets/www 文件夹访问图像。请帮忙。
我想为不同的本地化使用不同的图像。但是,我有所有分辨率和所有语言的图像。有没有办法做到这一点? 最佳答案 是的,有可能。可绘制-de-rDE-ldpi,可绘制-de-rDE-mdpi。检查一下。 关于
我的应用程序中有这个可绘制文件夹: 可绘制 drawable-hdpi、mdpi、ldpi、xhdpi 和 xxhdpi。 可绘制-sw240dp-land-xxxhdpi 可绘制-sw360dp-l
背景 我知道可以创建 Drawable(或位图)的旋转版本,因此(关于它的文章 here): @JvmStatic fun getRotateDrawable(d: Drawable, angle:
我有一个可绘制的状态列表,我想从可绘制的状态列表中获取一个特定的可绘制对象: 我为每个键选择正确的可绘制
如果您将可绘制对象放在/drawables 目录中,它们将被缩放,如果您想要不同密度的特定可绘制对象,您需要将它们放在特定目录中(/drawables-hdpi、/drawables-mdpi 等)
我熟悉@drawable,但在我克隆的 repo 中,我发现在引用项目中的可绘制对象时使用了@r$drawable。 对比 最佳答案 “$”(美元符号)用于引用 Android
我使用下面的代码通过手动给每个名称从 drawable 文件夹中获取 9(bird1 到 bird9)图像。 我如何使用数组或将 R.drawable.bird1 替换为变量(因为下面的语句只接受实际
据我了解,Android 的默认 DPI 设置等同于 MDPI。是否有任何理由同时拥有 drawable 和 drawable-mdpi 文件夹,或者如果我只是将它们放在 drawable 中,它们的
有什么区别吗 android:src="@andriod:drawable/filename" 和 android:src="@drawable/filename" 我可以在 xml 文件中以这种方式
我们在项目中提供各种密度的drawables是很常见的 drawable-mdpi drawable-hdpi drawable-xhdpi drawable-xxhdpi drawable-xxxh
我有一个应用程序可以加载用户安装的应用程序并在 recyclerview 中显示列表(应用程序名称和图标)。 此异常在 android 8+ 设备上引发,也不是在所有设备型号上(主要在三星和华为设备上
我是一名优秀的程序员,十分优秀!