1

アクティビティに基づいてビューを作成すると、正常に動作するアニメーションがあります。

しかし、フラグメントに基づいて作成したい場合、どうすればよいかわかりません。

ここにコード[アクティビティとして機能]:

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


    }

}

そのため、 ImageView imageAnim = (ImageView) findViewById(R.id.img); findViewById(id) is undefined というメソッドでエラーが発生します...

では、これを適応させてフラグメントで機能させるにはどうすればよいですか?

ありがとう!

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

アニメーション ドローアブルがフラグメント内で動作するようにするには、onCreateView メソッドではなく、onViewCreated メソッド内のアニメーションのメソッドを次のように呼び出します。

@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 に答える