manuelBetancurt
manuelBetancurt

Reputation: 16128

android frame by frame animation on fragment

I have an animation that works fine if I base my view on an Activity,

but if i want to base it on a fragment, i dont know what to do?,

here the code [working as activity]:

public class FramesAnimationActivity extends Activity {

    AnimationDrawable animation;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        animation = new AnimationDrawable();
        animation.addFrame(getResources().getDrawable(R.drawable.c_0), 100);
        animation.addFrame(getResources().getDrawable(R.drawable.c1), 100);
        animation.addFrame(getResources().getDrawable(R.drawable.c2), 100);
        animation.addFrame(getResources().getDrawable(R.drawable.c3), 100);
        animation.addFrame(getResources().getDrawable(R.drawable.c4), 100);
        animation.addFrame(getResources().getDrawable(R.drawable.c5), 100);
        animation.addFrame(getResources().getDrawable(R.drawable.c6), 100);
        animation.addFrame(getResources().getDrawable(R.drawable.c7), 100);


        animation.setOneShot(false);

        ImageView imageAnim =  (ImageView) findViewById(R.id.img);
        imageAnim.setBackgroundDrawable(animation);

        // run the start() method later on the UI thread
        imageAnim.post(new Starter());

    }

    class Starter implements Runnable {

        public void run() {
            animation.start();        
        }


    }

}

But as Fragment, How to make it work?

public class HomeTab extends Fragment {
    /* (non-Javadoc)
     * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
     */

    AnimationDrawable animation;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        if (container == null) {
            // We have different layouts, and in one of them this
            // fragment's containing frame doesn't exist.  The fragment
            // may still be created from its saved state, but there is
            // no reason to try to create its view hierarchy because it
            // won't be displayed.  Note this is not needed -- we could
            // just run the code below, where we would create and return
            // the view hierarchy; it would just never be used.
            return null;
        }


        animation = new AnimationDrawable();
        animation.addFrame(getResources().getDrawable(R.drawable.c_0), 100);
        animation.addFrame(getResources().getDrawable(R.drawable.c1), 100);
        animation.addFrame(getResources().getDrawable(R.drawable.c2), 100);
        animation.addFrame(getResources().getDrawable(R.drawable.c3), 100);
        animation.addFrame(getResources().getDrawable(R.drawable.c4), 100);
        animation.addFrame(getResources().getDrawable(R.drawable.c5), 100);
        animation.addFrame(getResources().getDrawable(R.drawable.c6), 100);
        animation.addFrame(getResources().getDrawable(R.drawable.c7), 100); 

animation.setOneShot(false);

        ImageView imageAnim =  (ImageView) findViewById(R.id.img);
        imageAnim.setBackgroundDrawable(animation);

        // run the start() method later on the UI thread
        imageAnim.post(new Starter());

        return (LinearLayout)inflater.inflate(R.layout.tab_home_layout, container, false);
    }

    class Starter implements Runnable {

        public void run() {
            animation.start();        
        }


    }

}

so I get an error in ImageView imageAnim = (ImageView) findViewById(R.id.img); with the method findViewById(id) is undefined...

so how to adapt this to make it work on my fragment?

thanks!

Upvotes: 1

Views: 2606

Answers (2)

Pedro Varela
Pedro Varela

Reputation: 2415

For an animation drawable to work in a fragment call the methods of the animation inside onViewCreated method instead of the onCreateView method, like this.

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
      ImageView animationImageView = (ImageView) view.findViewById(R.id.imageViewSearchAnimation);
      animationImageView.setBackgroundResource(R.drawable.search_animation);
      animation = (AnimationDrawable) animationImageView.getBackground();
      animation.start();
      super.onViewCreated(view, savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.dialog_progress_search,
      container, false);
      return view;
}

@Override
public void onPause() {
      animation.stop();
      super.onPause();
}

@Override
public void onResume() {
      super.onResume();
      animation.start();
}

Upvotes: 1

Steelight
Steelight

Reputation: 3357

First inflate your LinearLayout, then call the findViewById() method of the created view:

    LinearLayout ll = (LinearLayout)inflater.inflate(R.layout.tab_home_layout, container, false);
    ImageView imageAnim =  (ImageView) ll.findViewById(R.id.img);
    imageAnim.setBackgroundDrawable(animation);

    // run the start() method later on the UI thread
    imageAnim.post(new Starter());

    return ll;

Upvotes: 1

Related Questions