Make bounce animation

后端 未结 3 1958
无人共我
无人共我 2021-02-05 22:15

I would like to do bounce animation of layer.

I have done that layer comes from right to center, now I would like to move it a little back and then back to center. That

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-05 22:26

    Add code on button or image click

        final Animation myAnim = AnimationUtils.loadAnimation(getActivity(), R.anim.bounce);
        // Use bounce interpolator with amplitude 0.1 and frequency 15
        MyBounceInterpolator interpolator = new MyBounceInterpolator(0.1, 15);
        myAnim.setInterpolator(interpolator);
        imgVoiceSearch.startAnimation(myAnim);
    

    Add this class

    public class MyBounceInterpolator implements android.view.animation.Interpolator {
    private double mAmplitude = 1;
    private double mFrequency = 10;
    
    public MyBounceInterpolator(double amplitude, double frequency) {
        mAmplitude = amplitude;
        mFrequency = frequency;
    }
    
    public float getInterpolation(float time) {
        return (float) (-1 * Math.pow(Math.E, -time / mAmplitude) *
                Math.cos(mFrequency * time) + 1);
    }
    }
    

提交回复
热议问题