代码之家  ›  专栏  ›  技术社区  ›  Rishav Singla

震动动画不适用于API 19

  •  0
  • Rishav Singla  · 技术社区  · 6 年前

    <?xml version="1.0" encoding="utf-8"?>
    <rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="70"
    android:fromDegrees="-5"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="infinite"
    android:repeatMode="reverse"
    android:toDegrees="5" />
    
    
    Animation animation = AnimationUtils.loadAnimation(this, R.anim.shake);    
    itemView.startAnimation(animation);
    

    上面的代码在Android5.0上可以正常工作,但在Android4.4上却不能正常工作(API 19)

    2 回复  |  直到 6 年前
        1
  •  1
  •   Anisuzzaman Babla Jayesh Elamgodil    6 年前

    你可以用 ObjectAnimator

    ObjectAnimator rotate = ObjectAnimator.ofFloat(itemView, "rotation", 0f, 20f, 0f, -20f, 0f);
        rotate.setRepeatCount(ValueAnimator.INFINITE);
        rotate.setDuration(100);
        rotate.start();
    
        2
  •  0
  •   Rishav Singla    6 年前

    沙金动画:

    设置 android:repeatMode="reverse" 达到振动效果。它也可以很好地工作 API 19标准

    <?xml version="1.0" encoding="utf-8"?>
    <rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="100"
    android:fromDegrees="-5"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="infinite"
    android:repeatMode="reverse"
    android:toDegrees="5" />