【问题标题】:Vector animation on pre lollipop前棒棒糖上的矢量动画
【发布时间】:2016-10-09 15:18:50
【问题描述】:

我正在尝试在棒棒糖前设备上为点设置动画,但我仍然得到:

java.lang.NullPointerException
at android.animation.PropertyValuesHolder.setupSetterAndGetter(PropertyValuesHolder.java:505)

这是我的代码:

活动:

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>

和点_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>

此崩溃与主要活动中的onClickListener 相关,并且仅在棒棒糖前设备上发生。如何在 API 16 上正确创建矢量比例动画?另外问题是在棒棒糖设备上这也不起作用,当我点击它时什么也没有发生。我正在使用库设计:23.4.0

【问题讨论】:

  • 我的错误消息与setupSetterAndGetter 完全相同。从答案中我不清楚你的问题到底是什么。你能解释一下你做了什么来让你的代码正常工作吗?

标签: android android-animation android-vectordrawable


【解决方案1】:

我认为这是因为“错误”的演员阵容。您需要将 DrawableImageView 转换为 AnimatedVectorDrawableAnimatedVectorDrawableCompat

 androidImageView.setOnClickListener(v -> ((AnimatedVectorDrawable) androidImageView.getDrawable()).start());

您需要用group 包围您的path 以执行scalerotate 之类的动画,并且您需要在` 中的&lt;target&gt; 中引用该组的名称。

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

【讨论】:

  • 你知道我应该在我的代码中改变什么来从底线的中心缩放矢量吗?我的意思是它只是在成长而不是改变它的位置。它现在从右下角放大。
  • 添加了行:)
  • 我提出了新问题,因为它没有按预期工作:stackoverflow.com/questions/37724059/…
猜你喜欢
  • 2018-10-08
  • 2015-12-07
  • 1970-01-01
  • 1970-01-01
  • 2015-08-12
  • 2016-02-11
  • 2018-09-01
  • 2016-05-02
  • 2016-10-14
相关资源
最近更新 更多