开发者

Android initialize Animation

I couldn't find examples of how to initialize the animation object.

example Animation ticketAnim;

well new Animation(); is not a valid object it seems so开发者_JAVA技巧 I can't just do Animation ticketAnim = new Animation(); but I would like to. I take the suggested initialization route that the IDE offers which is Animation ticketAnim = null;

of course, accessing this will result in a null pointer exception

what is the right way to do this?


When declaring a new animation, you need to use the constructor of an animation type. Here's some sample code for one of the animation controllers I use in my code:

private void addDeleteDropAnimation() {
    AnimationSet set = new AnimationSet(true);

    Animation animation = new AlphaAnimation(0.0f, 1.0f);
    animation.setDuration(150);
    set.addAnimation(animation);

    animation = new TranslateAnimation(
        Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF, 0.0f,
        Animation.RELATIVE_TO_SELF, -1.0f,Animation.RELATIVE_TO_SELF, 0.0f
    );
    animation.setDuration(300);
    set.addAnimation(animation);

    controllerDel = new LayoutAnimationController(set, 0.5f);
    vw_delLinearLayout.setLayoutAnimation(controllerDel);
}

The Animation class itself is just an abstraction. To use an animation, implement one of Animation's direct know subclasses (also specified in the link to the Animation API).

These include:

  • AlphaAnimation
  • TranslateAnimation
  • RotateAnimation
  • ScaleAnimation

If you want, you can also create your own custom animation by extending the Animation class. A good example of creating a custom animation can be found here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜