Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了查看Android中的动画折叠/展开LinearLayout中的视图大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我将图像动态添加到我的线性布局,

我想实现

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

主要活动

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://scheR_608_11845@as.android.com/apk/res/android"
    xmlns:tools="http://scheR_608_11845@as.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/collapsesmooth expand / collapse ,但他们无法帮助我扩展线性布局中的所有视图.

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

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

请推荐我

解决方法

我已经分开了课.检查它是否适用于您:
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中的视图所遇到的程序开发问题。

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。