前棒棒糖上的矢量动画
Vector animation on pre lollipop
我正在尝试在棒棒糖之前的设备上为点设置动画,但我仍然得到:
java.lang.NullPointerException
at android.animation.PropertyValuesHolder.setupSetterAndGetter(PropertyValuesHolder.java:505)
这是我的代码:
Activity:
public class MyActivity extends BaseActivity {
static {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
ImageView androidImageView = (ImageView) findViewById(R.id.animated);
androidImageView.setOnClickListener(v -> ((Animatable) androidImageView.getDrawable()).start());
}
my_activity布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="@+id/animated"
android:layout_width="wrap_content"
app:srcCompat="@drawable/animated_dot"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>
</RelativeLayout>
animated_dot.xml:
<?xml version="1.0" encoding="utf-8"?>
<animated-vector
xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/dot_1">
<target
android:animation="@anim/dot_animator"
android:name="dot_name" />
</animated-vector>
dot_animator:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:propertyName="scaleX"
android:duration="250"
android:valueFrom="1.0"
android:valueTo="3.0"
/>
<objectAnimator
android:propertyName="scaleY"
android:duration="250"
android:valueFrom="1.0"
android:valueTo="3.0"
/>
</set>
和dot_1:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:viewportHeight="81.875"
android:viewportWidth="65.59"
android:width="24dp"
>
<path
android:fillColor="#FF000000"
android:pathData="M-0.01,32.82a32.81,32.81 0,1 1,65.61 0c0,28.94 -32.8,49.04 -32.8,49.04S-0.01,61.77 -0.01,32.82Z"
/>
</vector>
此崩溃与主要 activity 中的 onClickListener
有关,并且仅在 lolipop 之前的设备上发生。如何在 API 16 上正确创建矢量比例动画?还有一个问题是,在棒棒糖设备上这也不起作用,当我点击它时什么也没有发生。我正在使用库 design:23.4.0
我假设这是因为 "wrong" 转换。您需要将 Drawable
从 ImageView
转换为 AnimatedVectorDrawable
或 AnimatedVectorDrawableCompat
。
androidImageView.setOnClickListener(v -> ((AnimatedVectorDrawable) androidImageView.getDrawable()).start());
您需要用 group
包围您的 path
才能制作像 scale
或 rotate
这样的动画,并且您需要在<target>
在你的`.
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:viewportHeight="81.875"
android:viewportWidth="65.59"
android:width="24dp"
>
<group name=""dot_name
pivotX="viewportHeight / 2"
pivotY="viewportWidth / 2"> <!--Calculate the values by yourself, write the calucalted values in there. This moves the reference point to the middle -->
<path
android:fillColor="#FF000000"
android:pathData="M-0.01,32.82a32.81,32.81 0,1 1,65.61 0c0,28.94 -32.8,49.04 -32.8,49.04S-0.01,61.77 -0.01,32.82Z"
/>
</group>
</vector>
我正在尝试在棒棒糖之前的设备上为点设置动画,但我仍然得到:
java.lang.NullPointerException
at android.animation.PropertyValuesHolder.setupSetterAndGetter(PropertyValuesHolder.java:505)
这是我的代码:
Activity:
public class MyActivity extends BaseActivity {
static {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
ImageView androidImageView = (ImageView) findViewById(R.id.animated);
androidImageView.setOnClickListener(v -> ((Animatable) androidImageView.getDrawable()).start());
}
my_activity布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="@+id/animated"
android:layout_width="wrap_content"
app:srcCompat="@drawable/animated_dot"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>
</RelativeLayout>
animated_dot.xml:
<?xml version="1.0" encoding="utf-8"?>
<animated-vector
xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/dot_1">
<target
android:animation="@anim/dot_animator"
android:name="dot_name" />
</animated-vector>
dot_animator:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:propertyName="scaleX"
android:duration="250"
android:valueFrom="1.0"
android:valueTo="3.0"
/>
<objectAnimator
android:propertyName="scaleY"
android:duration="250"
android:valueFrom="1.0"
android:valueTo="3.0"
/>
</set>
和dot_1:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:viewportHeight="81.875"
android:viewportWidth="65.59"
android:width="24dp"
>
<path
android:fillColor="#FF000000"
android:pathData="M-0.01,32.82a32.81,32.81 0,1 1,65.61 0c0,28.94 -32.8,49.04 -32.8,49.04S-0.01,61.77 -0.01,32.82Z"
/>
</vector>
此崩溃与主要 activity 中的 onClickListener
有关,并且仅在 lolipop 之前的设备上发生。如何在 API 16 上正确创建矢量比例动画?还有一个问题是,在棒棒糖设备上这也不起作用,当我点击它时什么也没有发生。我正在使用库 design:23.4.0
我假设这是因为 "wrong" 转换。您需要将 Drawable
从 ImageView
转换为 AnimatedVectorDrawable
或 AnimatedVectorDrawableCompat
。
androidImageView.setOnClickListener(v -> ((AnimatedVectorDrawable) androidImageView.getDrawable()).start());
您需要用 group
包围您的 path
才能制作像 scale
或 rotate
这样的动画,并且您需要在<target>
在你的`.
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:viewportHeight="81.875"
android:viewportWidth="65.59"
android:width="24dp"
>
<group name=""dot_name
pivotX="viewportHeight / 2"
pivotY="viewportWidth / 2"> <!--Calculate the values by yourself, write the calucalted values in there. This moves the reference point to the middle -->
<path
android:fillColor="#FF000000"
android:pathData="M-0.01,32.82a32.81,32.81 0,1 1,65.61 0c0,28.94 -32.8,49.04 -32.8,49.04S-0.01,61.77 -0.01,32.82Z"
/>
</group>
</vector>