`
yhz61010
  • 浏览: 558005 次
  • 来自: -
博客专栏
63c13ecc-ef01-31cf-984e-de461c7dfde8
libgdx 游戏开发
浏览量:12175
社区版块
存档分类
最新评论

[转] Android: Expand/collapse animation

阅读更多
原文地址:https://stackoverflow.com/a/13381228/6091500

亲测可用。

import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Transformation;

public class ViewAnimation {
    public static void expand(final View v) {
        v.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        final int targetHeight = v.getMeasuredHeight();

        // Older versions of android (pre API 21) cancel animations for views with a height of 0.
        v.getLayoutParams().height = 1;
        v.setVisibility(View.VISIBLE);
        Animation a = new Animation()
        {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                v.getLayoutParams().height = interpolatedTime == 1
                        ? ViewGroup.LayoutParams.WRAP_CONTENT
                        : (int)(targetHeight * interpolatedTime);
                v.requestLayout();
            }

            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };

        // 1dp/ms
        a.setDuration((int)(targetHeight / v.getContext().getResources().getDisplayMetrics().density));
        v.startAnimation(a);
    }

    public static void collapse(final View v) {
        final int initialHeight = v.getMeasuredHeight();

        Animation a = new Animation()
        {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                if(interpolatedTime == 1){
                    v.setVisibility(View.GONE);
                }else{
                    v.getLayoutParams().height = initialHeight - (int)(initialHeight * interpolatedTime);
                    v.requestLayout();
                }
            }

            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };

        // 1dp/ms
        a.setDuration((int)(initialHeight / v.getContext().getResources().getDisplayMetrics().density));
        v.startAnimation(a);
    }
}
分享到:
评论

相关推荐

    短网址生成网站ShortURL.zip

    ShortURL 是一个能缩短网址,并生成二维码的...Return: JSONExamples:$ curl 3sd.me/j/expand -d "shorten=Jh8x3" {"shorten": "http://3sd.me/Jh8x3", "expand": "http://baidu.com"} 标签:ShortURL

    ExpandableTextView

    在代码中,我们可以通过调用ExpandableTextView的方法来控制其状态,如`expand()`用于展开文本,`collapse()`用于收起文本,以及`toggle()`用于切换展开/收起状态。 接下来,深入了解一下ExpandableTextView的工作...

    精彩编程与编程技巧-往列表框中填入文件目录信息...

    ### 知识点详解 #### 一、列表框填充文件目录信息的方法介绍 在软件开发过程中,有时我们需要向用户展示特定目录下的文件列表或者提供文件选择功能。传统的`FileListBox`控件虽然能够实现基本的功能需求,但在某些...

    Comment Reply Notification,WordPress留言回复通知插件.rar

    This will allow the users to follow up the comment and expand the conversation if desired. 评论回复通知插件, 当评论被回复时会email通知评论的作者. Author URI: http://fairyfish.net/ Donate link: ...

    jes邮件服务器,纯java实现

    2. Expand the distribution archive. 3. Modify the included script files for your local file system (if neccessary). 4. Edit the mail.conf, user.conf and log.conf files. 5. Run the mail server ...

    可以滑动页面的项目

    xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:slider="http://schemas.android.com/apk/res-auto" android:id="@+id/xml_slider_layout...

    简单模拟菜单栏展开收缩效果

    在res/anim目录下创建两个XML文件,例如`expand_menu.xml`和`collapse_menu.xml`,分别表示菜单展开和收缩的动画效果。展开动画通常涉及增加视图的高度,而收缩则是减小高度。在XML文件中,我们可以使用`<translate>...

    LABVIEW 资源,AES128加密子VI:Expand Key

    LABVIEW 资源,AES128加密子VI:Expand Key

    [原创]自己工作中常用的模板库,简化你的工作

    Get::finalValue接口提供了这种自动的转: template<typename T> void transmit(T p) { receive(Get::finalValue(p)); } ☆ Convert type qualifiers,e.g. addConst/removeConst.. , keep same output qualifier ...

    werval-sample-urlshortener:网址缩短器-werval示例应用程序

    http http://localhost:23023/api/list 缩短给定的URL: http "http://localhost:23023/api/shorten?url=http://example.com/" 扩展给定URL的哈希值: http "http://localhost:23023/api/expand?has

    Python button选取本地图片并显示的实例

    frame.pack(fill=BOTH, expand=1) ``` - **初始化窗口**:创建了一个 `Tk` 对象 `root`,作为整个图形用户界面的根窗口。 - **设置框架**:创建了一个 `Frame`,用作容器,同时配置了水平和垂直方向上的滚动条。 - ...

    智能开发平台 DOROODO

    mysql5+(系统默认使用mysql,你需要将mysql的数据转到oracle,sqlserver里),需要注意的是修改mysql的【max_connections=1000】 2.在mysql中建库doroodo,在库中执行doroodo.sql 3.在eclipse中引入doroodo,修改...

    Windows下制作Python便携版--Portable Python.md

    powershell -Command "Expand-Archive -Path \"%tmp%\python-3.7.9.zip\" -DestinationPath \"%destDir%\" -Force" ``` **步骤三:下载并安装pip** 为了方便管理第三方库,通常还需要安装pip工具。可以通过以下...

    如何在精简版的OS安装IIS

    EXPand d:\iis51\iis.dl_ c:\Windows\system32\setup\iis2.dll EXPand d:\iis51\iis.in_ c:\Windows\inf\iis2.inf ``` 按照实际情况调整盘符,例如如果文件位于F盘,则替换所有`d:`为`f:`。 #### 步骤四:通过...

    DSM_DS918+_25426.pat,和synoboot.img是一个群晖的引导的镜像,不知道怎么用

    DSM_DS918+_25426.pat,和synoboot.img是一个群晖的引导的镜像教程及来源https://post.smzdm.com/p/aqx07xmk/

    最新版java

    Java Developer Newsletter: From your Oracle account, select Subscriptions, expand Technology, and subscribe to Java. Java Developer Day hands-on workshops (free) and other events Java Magazine

    listviewanimationdemo

    基于最新的AndroidStudio实现。Appearance animations for items in ListViews, GridViews, other AbsListViews; Built in animations include Alpha, SwingRightIn, SwingLeftIn, SwingBottomIn, SwingRightIn and ...

    IBMPC服务器日志收集方法

    IBM PC服务器硬件日志收集工具安装及收集方式 目录 第一章日志类型、用途及收集方式

    textview 伸缩效果,可多行才显示伸缩否则隐藏伸缩

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> android:id="@+...

Global site tag (gtag.js) - Google Analytics