我可以帮您翻译成中文。下面是翻译的结果:
我正在尝试使用SVG路径和动画向量实现类似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
的崩溃:/
请注意,路径必须兼容于变形。更具体地说,路径应该具有完全相同的命令长度和每个命令的参数的完全相同的长度。
- pskink"M0,0 L0,24 12,12 0,0"
变形为"Mx,x Lx,x x,x x,x"
,其中x是任何数字。 - pskink