2

我有三个重叠的滚动视图。出于某种原因,当我将其他两个设置为 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>
4

3 回答 3

1

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

这是我的布局文件

<?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 文件夹中。

于 2016-06-30T04:22:14.847 回答
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);

希望这可以帮助。

于 2013-04-02T07:22:23.140 回答
0

我确实遇到了问题,我通过使用onWindoFocusChangeListenerHandler解决了它。

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 中的视图对象或简单地调用视图?/ getView() 来自任何地方。

于 2019-05-17T17:35:31.150 回答