I simply want to use this working animation code I have in a more efficient manner. The code below works exactly as I want it to but I don't want to copy and paste this everywhere in the thirty places across several activities in which I'll be using this specific fadeout animation. How can I set up a class to do this? The primary problem is setting the view to invisible in onAnimationEnd.

    final TextView ph = (TextView) findViewById(R.id.textPlaceholder);
    final Animation fadeOut = new AlphaAnimation(1.0f, 0.0f);
    fadeOut.setStartOffset(1000);
    fadeOut.setDuration(500);

    fadeOut.setAnimationListener(new AnimationListener() {

        @Override
        public void onAnimationEnd(Animation animation) {
            ph.setVisibility(View.INVISIBLE);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationStart(Animation animation) {
            // TODO Auto-generated method stub

        }
    });

    ph.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            ph.startAnimation(fadeOut);
        }
    });
有帮助吗?

解决方案

Create a helper class:

Class ClickToFadeOutAnimation

  private View view; 

  public static void attachTo(View view) {
    new FadeOutAnimation(View view);
  }

  public FadeOutAnimation(View view) {
    this.view  = view;
    view.setOnCLickListsner(new OnClickListener() {
      animate();
    )
  }

  public void animate() {
    Animation fadeOut = ...
    .. rest of your animation code
  } 

}

And to use it:

TextView ph = (TextView) findViewById(R.id.textPlaceholder);
ClickToFadeOutAnimation.attachTo(ph);

其他提示

Extend the AlphaAnimation class where you set the duration, offset and AnimationListener in the constructor.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top