Thursday, February 21, 2019

Make view bobbing like a teeter

We want a view move bobbing like a teeter.
Copy to below findViewByid.
final RotateAnimation rotate = new RotateAnimation(-10, 10, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(100);
rotate.setInterpolator(new LinearInterpolator());
rotate.setFillAfter(true);
rotate.setRepeatMode(Animation.REVERSE);       
rotate.setRepeatCount(Animation.INFINITE);           button.startAnimation(rotate);

If want it moving some time, change setRepeatCount to:
rotate.setRepeatCount(10);
Second way, in folder layout, create a file name bobbing.
<?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="10"
    android:repeatMode="reverse"
    android:toDegrees="5" />
In code, add this lines.
Animation bob = AnimationUtils.loadAnimation(this, R.drawable.bobbing);
image.startAnimation(bob);

No comments:

Post a Comment