2010-04-24 87 views
5

我在android中有一个简单的ListView列表结果。点击每个项目后,我希望它向下滑动展开并显示内容。有没有一种简单的方法在android中做到这一点?在android中滑动展开动画

任何帮助将不胜感激。

回答

3

看看这个answer。不仅如此,你必须使用粗花呢动画。检查ApiDemos/Animation2示例。并且还可以在ApiDemos中查看anim文件夹。它对我很有帮助。根据你的问题slide_top_to_bottom将有所帮助。

5

这是来自Udinic的例子。它已经ListView项扩大与动画,并要求你需要一个动画类

/** 
* This animation class is animating the expanding and reducing the size of a view. 
* The animation toggles between the Expand and Reduce, depending on the current state of the view 
* @author Udinic 
* 
*/ 
public class ExpandAnimation extends Animation { 
    private View mAnimatedView; 
    private LayoutParams mViewLayoutParams; 
    private int mMarginStart, mMarginEnd; 
    private boolean mIsVisibleAfter = false; 
    private boolean mWasEndedAlready = false; 

    /** 
* Initialize the animation 
* @param view The layout we want to animate 
* @param duration The duration of the animation, in ms 
*/ 
    public ExpandAnimation(View view, int duration) { 

     setDuration(duration); 
     mAnimatedView = view; 
     mViewLayoutParams = (LayoutParams) view.getLayoutParams(); 

     // decide to show or hide the view 
     mIsVisibleAfter = (view.getVisibility() == View.VISIBLE); 

     mMarginStart = mViewLayoutParams.bottomMargin; 
     mMarginEnd = (mMarginStart == 0 ? (0- view.getHeight()) : 0); 

     view.setVisibility(View.VISIBLE); 
    } 

    @Override 
    protected void applyTransformation(float interpolatedTime, Transformation t) { 
     super.applyTransformation(interpolatedTime, t); 

     if (interpolatedTime < 1.0f) { 

      // Calculating the new bottom margin, and setting it 
      mViewLayoutParams.bottomMargin = mMarginStart 
        + (int) ((mMarginEnd - mMarginStart) * interpolatedTime); 

      // Invalidating the layout, making us seeing the changes we made 
      mAnimatedView.requestLayout(); 

     // Making sure we didn't run the ending before (it happens!) 
     } else if (!mWasEndedAlready) { 
      mViewLayoutParams.bottomMargin = mMarginEnd; 
      mAnimatedView.requestLayout(); 

      if (mIsVisibleAfter) { 
       mAnimatedView.setVisibility(View.GONE); 
      } 
      mWasEndedAlready = true; 
     } 
    } 
} 

API级别只4+ 基本上并使用此:

View toolbar = view.findViewById(R.id.toolbar); 

       // Creating the expand animation for the item 
       ExpandAnimation expandAni = new ExpandAnimation(toolbar, 500); 

       // Start the animation on the toolbar 
       toolbar.startAnimation(expandAni); 

ExpandAnimationExample

+0

对于downvoter,你应该离开你投下我的答案的原因。我知道我的答案可以被视为仅链接答案,但你想要什么?您希望我将该回购中的所有文件复制到此处。因此,Github链接是可靠的 – 2013-08-06 01:25:55

+1

请注意,这个例子仍然是越野车。 https://github.com/Udinic/SmallExamples/issues/6当你向上和向下滚动列表视图时,展开/折叠状态会混乱。 – 2013-10-10 18:18:27

+0

@CheokYanCheng任何解决混乱的状态?很明显,当我们滑动时存在一个问题,使得视图滑落,现在我们改变可见性以便动画。但是有一个闪烁,视图关闭,然后再次打开向下滑动:( – beerBear 2014-12-10 11:54:31

1

最简单的方法是使用一个ObjectAnimator

ObjectAnimator animation = ObjectAnimator.ofInt(yourTextView, "maxLines", 40); 
animation.setDuration(200).start(); 

这会将maxLines从TextView更改为40,超过200毫秒。

要小心使用yourTextView.getLineCount()来确定要扩展到多少行,因为直到布局传递后它才会给出准确的数字。我建议你只编写一个maxLines值,这个值比你期望的文本长。您还可以使用yourTextView.length()除以每行所期望的最小字符数来估计它。