2012-11-21 53 views
2

我有三个重叠的滚动视图。出于某种原因,当我设置其他两个View.Gone和一个滚动视图我想View.Visible,然后开始一个动画,它不会被触发。这些滚动视图在一个片段内 - 我知道一些功能在片段内不能完全工作。虽然动画看起来很基本。片段内的Android动画不能正常工作

这里是我的按钮侦听器的方法;

 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> 
+0

理查德,你有没有想过这个?我正在尝试做同样的事情。 – Badams

+0

是的,我会发布我如何最终做到这一点 - 让我知道如果类似的东西不适合你!如果确实如此,请告诉我,我会将其作为正确答案进行检查。 –

回答

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

希望这会有所帮助。

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中的可绘制文件夹。