How to programmatically animate an ImageView

前端 未结 3 1014
天命终不由人
天命终不由人 2021-02-14 09:19

I am trying to animate an ImageView when the said image view is clicked.

Specifically I want the size of the ImageView gets bigger (say .20 bigger) and the immediately s

3条回答
  •  南旧
    南旧 (楼主)
    2021-02-14 10:03

    If you want to implement this without XML you could do so as follows

    final float growTo = 1.2f;
    final long duration = 1200;
    
    ScaleAnimation grow = new ScaleAnimation(1, growTo, 1, growTo, 
                                             Animation.RELATIVE_TO_SELF, 0.5f,
                                             Animation.RELATIVE_TO_SELF, 0.5f);
    grow.setDuration(duration / 2);
    ScaleAnimation shrink = new ScaleAnimation(growTo, 1, growTo, 1,
                                               Animation.RELATIVE_TO_SELF, 0.5f,
                                               Animation.RELATIVE_TO_SELF, 0.5f);
    shrink.setDuration(duration / 2);
    shrink.setStartOffset(duration / 2);
    AnimationSet growAndShrink = new AnimationSet(true);
    growAndShrink.setInterpolator(new LinearInterpolator());
    growAndShrink.addAnimation(grow);
    growAndShrink.addAnimation(shrink);
    thumbLike.startAnimation(growAndShrink);
    

    Of course, you could also use NineOldAndroids and use the new animation methods.

    I think your original error is this line, it removes the animation you just started from the view again.

    thumbLike.setAnimation(null);
    

提交回复
热议问题