如果我将视图基于活动
,我的动画效果很好但是如果我想把它放在一个片段上,我不知道该怎么办?,
此处代码[作为活动工作]:
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)未定义...
那么如何调整它以使其适用于我的片段呢?
谢谢!
答案 0 :(得分: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;
答案 1 :(得分: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();
}