【问题标题】:Slide expand animation in androidandroid中的幻灯片展开动画
【发布时间】:2010-04-24 15:56:59
【问题描述】:

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

任何帮助将不胜感激。

【问题讨论】:

    标签: android animation android-listview expand


    【解决方案1】:

    这里是来自乌迪尼克的例子。它的 listview 项目通过动画展开,并且只需要 API 级别 4+ 基本上你需要一个动画类

    /**
    * 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;
            }
        }
    }
    

    并使用它:

    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

    【讨论】:

    • 要拒绝投票,您应该留下拒绝投票的原因。我知道我的答案可以被视为仅链接的答案,但是您想要什么?您希望我将该仓库中的所有文件复制到此处。因此 Github 链接是可靠的
    • 注意,这个例子还是有问题的。 github.com/Udinic/SmallExamples/issues/6 当您上下滚动列表视图时,展开/折叠状态会混乱。
    • @CheokYanCheng 有什么解决混乱状态的办法吗?显然,当我们向上滚动时存在问题,因此视图现在应该向下滑动以使我们更改可见性以便动画。启动但有闪烁,视图关闭然后再次打开以向下滑动:(
    • @beerBear 因为我几乎找不到正确实现可扩展 ListView 的开源库。我决定通过使用 LinearLayout + ScrollView + 自定义 View 来实现我自己的,而不使用 ListView。它对我有用,因为我只有约 10 件物品。我相信新的RecyclerView,更好的版本ListView,可以更轻松地达到同样的效果。但是,我还没有尝试过。
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      最简单的方法是使用ObjectAnimator

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

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

      当心使用 yourTextView.getLineCount() 来确定要扩展的行数,因为在布局通过之前它不会给出准确的数字。我建议您硬编码一个比您预期的文本更长的 maxLines 值。您还可以使用 yourTextView.length() 除以每行所期望的最低字符数来估计它。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-12-06
        • 2015-01-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-12
        相关资源
        最近更新 更多