1

如果我的视图基于 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();        
        }


    }

}

但作为片段,如何使它工作?

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();        
        }


    }

}

所以我在 findViewById(id) is undefined 方法中遇到错误 ImageView imageAnim = (ImageView) findViewById(R.id.img); ...

那么如何调整它以使其适用于我的片段?

谢谢!

4

2 回答 2

1

首先为您的 LinearLayout 充气,然后调用已创建视图的 findViewById() 方法:

    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;
于 2012-08-15T09:17:45.990 回答
1

要使动画可绘制在片段中工作,请调用 onViewCreated 方法中的动画方法,而不是 onCreateView 方法,就像这样。

@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();
}
于 2014-12-30T22:46:14.930 回答