查看Android中的动画折叠展开LinearLayout中的视图

查看Android中的动画折叠展开LinearLayout中的视图,第1张

概述我将图像动态添加到我的线性布局, 我想实现 这是我做了什么样的示例代码 主要活动 package ksp.com.animationssample;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.view.ani 我将图像动态添加到我的线性布局,

我想实现

这是我做了什么样的示例代码

主要活动

package ksp.com.animationssample;import androID.support.v7.app.AppCompatActivity;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.vIEw.animation.AccelerateInterpolator;import androID.vIEw.animation.Animation;import androID.vIEw.animation.TranslateAnimation;import androID.Widget.button;import androID.Widget.ImageVIEw;import androID.Widget.linearLayout;public class MainActivity extends AppCompatActivity {    private button btn_expand;    private button btn_collapse;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_main);        btn_expand = (button) findVIEwByID(R.ID.btn_expand);        btn_collapse = (button) findVIEwByID(R.ID.btn_collapse);        linearLayout.LayoutParams vp = new linearLayout.LayoutParams(linearLayout.LayoutParams.MATCH_PARENT,linearLayout.LayoutParams.WRAP_CONTENT);        final linearLayout layout = (linearLayout) findVIEwByID(R.ID.imageLayout);        for (int i = 0; i < 3; i++) {            final ImageVIEw image = new ImageVIEw(MainActivity.this);            image.setLayoutParams(vp);            if (i == 0)                image.setimageDrawable(getResources().getDrawable(R.drawable.item_image1));            else image.setimageDrawable(getResources().getDrawable(R.drawable.item_image2));            if (i == 2)                image.setVisibility(VIEw.VISIBLE);            else                image.setVisibility(VIEw.GONE);            layout.addVIEw(image);        }        btn_expand.setonClickListener(new VIEw.OnClickListener() {            @OverrIDe            public voID onClick(VIEw v) {                expandOrCollapse(layout.getChildAt(2),true,layout.getChildAt(0).getHeight() + layout.getChildAt(1).getHeight());                expandOrCollapse(layout.getChildAt(1),layout.getChildAt(0).getHeight());                expandOrCollapse(layout.getChildAt(0),0);            }        });        btn_collapse.setonClickListener(new VIEw.OnClickListener() {            @OverrIDe            public voID onClick(VIEw v) {                expandOrCollapse(layout.getChildAt(0),false,0);                expandOrCollapse(layout.getChildAt(1),layout.getChildAt(0).getHeight());                expandOrCollapse(layout.getChildAt(2),layout.getChildAt(0).getHeight() + layout.getChildAt(1).getHeight());            }        });    }    public voID expandOrCollapse(final VIEw v,boolean is_expand,final int animheight) {        TranslateAnimation anim = null;        if (is_expand) {            anim = new TranslateAnimation(0.0f,0.0f,-animheight,0.0f);            v.setVisibility(VIEw.VISIBLE);            Animation.AnimationListener expandedListener = new Animation.AnimationListener() {                @OverrIDe                public voID onAnimationStart(Animation animation) {                }                @OverrIDe                public voID onAnimationRepeat(Animation animation) {                }                @OverrIDe                public voID onAnimationEnd(Animation animation) {                }            };            anim.setAnimationListener(expandedListener);        } else {            anim = new TranslateAnimation(0.0f,-animheight);            Animation.AnimationListener collapseListener = new Animation.AnimationListener() {                @OverrIDe                public voID onAnimationStart(Animation animation) {                }                @OverrIDe                public voID onAnimationRepeat(Animation animation) {                }                @OverrIDe                public voID onAnimationEnd(Animation animation) {                    v.setVisibility(VIEw.GONE);                }            };            anim.setAnimationListener(collapseListener);        }        anim.setDuration(1500);        anim.setInterpolator(new AccelerateInterpolator(0.5f));        v.startAnimation(anim);    }}

activity_mainn.xml

<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:tools="http://schemas.androID.com/tools"    androID:ID="@+ID/activity_main"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:paddingBottom="@dimen/activity_vertical_margin"    androID:paddingleft="@dimen/activity_horizontal_margin"    androID:paddingRight="@dimen/activity_horizontal_margin"    androID:paddingtop="@dimen/activity_vertical_margin"    androID:orIEntation="vertical"    androID:background="@androID:color/holo_blue_dark"    tools:context="ksp.com.animationssample.MainActivity">    <button        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:layout_centerHorizontal="true"        androID:ID="@+ID/btn_expand"        androID:text="Expand"        />    <button        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:ID="@+ID/btn_collapse"        androID:text="Collopse"/><ScrollVIEw    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content"    androID:scrollbars="vertical">    <linearLayout        androID:layout_wIDth="match_parent"        androID:ID="@+ID/imageLayout"        androID:gravity="center"        androID:orIEntation="vertical"        androID:layout_height="wrap_content">    </linearLayout>    <!--androID:animateLayoutChanges="true"--></ScrollVIEw></relativeLayout>

当我第一次单击“扩展”按钮时,第二次不显示动画.

启用可见项目后,单击折叠按钮.

接下来做什么 :
当我选择任何VIEw项目时,它必须显示选择的动画,然后折叠动画,崩溃后,它必须显示为顶部视图像我在图像上面提到.目前,我很难编辑视图的数量,并为每个视图写入了动态静态高度的静态动画,即expandOrCollapse(vIEw,height_of_vIEw))

我搜索了一段时间,但没有运气.

我遵循Vie expand/collapse和smooth expand / collapse ,但他们无法帮助我扩展线性布局中的所有视图.

我们可以做这个列表视图是回收器视图还是什么?

为了节省时间,我已经将我的样品添加到git hub,您可以尝试它Animation Sample Github

请推荐我

解决方法@H_404_35@ 我已经分开了课.检查它是否适用于您:
public class DropDownAnim extends Animation {    private final int targetHeight;    private final VIEw vIEw;    private final boolean down;    public DropDownAnim(VIEw vIEw,int targetHeight,boolean down) {        this.vIEw = vIEw;        this.targetHeight = targetHeight;        this.down = down;    }    @OverrIDe    protected voID applytransformation(float interpolatedTime,transformation t) {        int newHeight;        if (down) {            newHeight = (int) (targetHeight * interpolatedTime);        } else {            newHeight = targetHeight - (int) (targetHeight * interpolatedTime);//(int) (targetHeight * (1 - interpolatedTime));        }        vIEw.getLayoutParams().height = newHeight;        vIEw.requestLayout();        vIEw.setVisibility(down ? VIEw.VISIBLE : VIEw.GONE);    }    @OverrIDe    public voID initialize(int wIDth,int height,int parentWIDth,int parentHeight) {        super.initialize(wIDth,height,parentWIDth,parentHeight);    }    @OverrIDe    public boolean willChangeBounds() {        return true;    }}

在使用这个.为了扩展

public voID expand(final VIEw v) {        final int targetHeight = getResources().getDimensionPixelSize(R.dimen.notification_height);//v.getMeasuredHeight();        DropDownAnim a = new DropDownAnim(v,targetHeight,true);        a.setDuration((int) (targetHeight / v.getContext().getResources().getdisplayMetrics().density));        a.setAnimationListener(new Animation.AnimationListener() {            @OverrIDe            public voID onAnimationStart(Animation animation) {            }            @OverrIDe            public voID onAnimationEnd(Animation animation) {                // Your code on end of animation            }            @OverrIDe            public voID onAnimationRepeat(Animation animation) {            }        });        v.setVisibility(VIEw.INVISIBLE);        v.startAnimation(a);    }

为了崩溃:

public voID collapse(final VIEw v) {        final int targetHeight = v.getMeasuredHeight();        DropDownAnim a = new DropDownAnim(v,false);        a.setDuration((int) (targetHeight / v.getContext().getResources().getdisplayMetrics().density));        a.setAnimationListener(new Animation.AnimationListener() {            @OverrIDe            public voID onAnimationStart(Animation animation) {            }            @OverrIDe            public voID onAnimationEnd(Animation animation) {                // Your code on end of animation            }            @OverrIDe            public voID onAnimationRepeat(Animation animation) {            }        });        v.startAnimation(a);    }
总结

以上是内存溢出为你收集整理的查看Android中的动画折叠/展开LinearLayout中的视图全部内容,希望文章能够帮你解决查看Android中的动画折叠/展开LinearLayout中的视图所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1134083.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存