片段中的Android动画无法正常工作

时间:2012-11-21 23:10:24

标签: android animation fragment

我有三个重叠的滚动视图。出于某种原因,当我将另外两个设置为View.Gone和一个滚动视图时我想要View.Visible,然后启动动画,它不会被触发。这些滚动视图位于片段内 - 我知道某些功能在片段中不能完全发挥作用。动画看起来很基本。

这是我的按钮监听器的方法;

        sv2.setVisibility(View.GONE);
        sv3.setVisibility(View.GONE);
        sv1.setVisibility(View.VISIBLE);
        Animation fadeInAnimation = AnimationUtils.loadAnimation(getActivity(), R.anim.fade_in_scollview);
        //set your animation
        sv1.startAnimation(fadeInAnimation);

还尝试设置不可见的加载动画,然后使其可见;

        sv1.setVisibility(View.INVISIBLE);
        Animation fadeInAnimation = AnimationUtils.loadAnimation(getActivity(), R.anim.fade_in_scollview);  
        //set your animation
        sv1.startAnimation(fadeInAnimation);
        sv1.setVisibility(View.VISIBLE);

这是我的动画片xml;

<?xml version="1.0" encoding="UTF-8"?>
  <set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha android:fromAlpha="0.0" android:toAlpha="1.0" 
     android:interpolator="@android:anim/accelerate_interpolator" 
   android:duration="500"
    android:repeatCount="infinite"/>
 </set>

3 个答案:

答案 0 :(得分:1)

要在Fragment中使用动画,请尝试以下代码

这是我的布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/iv_banner"
        android:layout_width="fill_parent"
        android:layout_height="250dp"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

这是我的片段java类

    public class Fragment_Home extends Fragment {
    public int currentimageindex = 0;
    Handler mHandler = new Handler();
    Runnable mUpdateResults;

    //Array of drawable images
    private int[] IMAGE_IDS = {
            R.drawable.home_slider_stemer, R.drawable.home_slider_plane
    };

    //image view
    private ImageView iv_banner;
    private View rootView;

    public Fragment_Home() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        rootView = inflater.inflate(R.layout.fragment_home, container, false);

        LoadUIElements();



        return rootView;
    }

    private void LoadUIElements() {
        iv_banner = (ImageView) rootView.findViewById(R.id.iv_banner);
        int delay = 1000; // delay for 1 sec.

        int period = 2000; // repeat every 4 sec.
        Timer timer = new Timer();

        timer.scheduleAtFixedRate(new TimerTask() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                mHandler.post(mUpdateResults);
            }
        }, delay, period);

        mUpdateResults = new Runnable() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                try {
                    AnimateandSlideShow();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        };
    }

    /**
     * Helper method to start the animation on the splash screen
     */
    protected void AnimateandSlideShow() {
        // TODO Auto-generated method stub
        try {
            iv_banner.setImageResource(IMAGE_IDS[currentimageindex
                    % IMAGE_IDS.length]);

            currentimageindex++;
            Animation rotateimage = AnimationUtils.loadAnimation(getActivity()
                    .getBaseContext().getApplicationContext(), R.anim.fade_in);
            iv_banner.startAnimation(rotateimage);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

不要忘记将图像放入res中的Drawable文件夹中。

答案 1 :(得分:0)

通过设置动画监听器并管理其中的所有可见性内容,我解决了这个问题。

    sv1.setVisibility(View.INVISIBLE);
    //grab animation from anim folder in res/
    Animation fadeInAnimation = AnimationUtils.loadAnimation(getActivity(), R.anim.push_up_anim);
    fadeInAnimation.setAnimationListener(new AnimationListener() {
          //set other scroll views to invisible once done
          public void onAnimationEnd(Animation animation) {
            sv2.setVisibility(View.INVISIBLE);
            sv3.setVisibility(View.INVISIBLE);
            }

            public void onAnimationRepeat(Animation animation) {
            }
            //once our animation starts, we set our view to visible
            public void onAnimationStart(Animation animation) {
                sv1.setVisibility(View.VISIBLE);
            }
        });
        scrollViewAnimationActive = true;
        //start our animations for views that need to be removed. 
        //We know one of these views were showing by checking if it was "visible".
        if (sv2.getVisibility() == View.VISIBLE)
              sv2.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.pushed_out_anim));
        else if (sv3.getVisibility() == View.VISIBLE) {
              sv3.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.pushed_out_anim));
        }else if (wikiParentLL.getChildCount() > 1) {
                wikiParentLL.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.pushed_out_anim));
            }
//finally, start our "animation"
    sv1.startAnimation(fadeInAnimation);

希望这有帮助。

答案 2 :(得分:0)

我确实有问题,我通过使用 onWindoFocusChangeListener Handler 来解决了。

mview.getViewTreeObserver().addOnWindowFocusChangeListener(new ViewTreeObserver.OnWindowFocusChangeListener() {
        @Override
        public void onWindowFocusChanged(final boolean hasFocus) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    startSeedAnimation();
                }
            }, 600);

        }
    });

在哪里可以在onViewCreated中获取视图对象或直接调用view? / getView()随处可见。