アクティビティに基づいてビューを作成すると、正常に動作するアニメーションがあります。
しかし、フラグメントに基づいて作成したい場合、どうすればよいかわかりません。
ここにコード[アクティビティとして機能]:
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 というメソッドでエラーが発生します...
では、これを適応させてフラグメントで機能させるにはどうすればよいですか?
ありがとう!