简体   繁体   English

Android单击按钮时展开动画

[英]Android expand animation on Button click

I try to make some tests with UX and animation. 我尝试使用UX和动画进行一些测试。 I want at a click on a button to expand it until the button has the size of his parent layout. 我想单击一个按钮将其展开,直到该按钮具有其父布局的大小为止。 Here is my very simple interface : 这是我非常简单的界面:

在此处输入图片说明

I succeed to create my own animation, it's just a simple class that takes the begin X, the end X, the begin Y and the end Y. Here is the class : 我成功创建了自己的动画,它只是一个简单的类,它包含开始X,结束X,开始Y和结束Y。这是该类:

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

public class ExpandAnimation extends Animation {
    protected final View view;
    protected float perValueH, perValueW;
    protected final int originalHeight, originalWidth;

    public ExpandAnimation(View view, int fromHeight, int toHeight, int fromWidth, int toWidth) {
        this.view = view;
        this.originalHeight = fromHeight;
        this.originalWidth = fromWidth;
        this.perValueH = (toHeight - fromHeight);
        this.perValueW = (toWidth - fromWidth);
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        view.getLayoutParams().height = (int) (originalHeight + perValueH * interpolatedTime);
        view.getLayoutParams().width = (int) (originalWidth + (perValueW * interpolatedTime)*2);
        view.requestLayout();
    }

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

The animation is pretty good and works as expected : 动画非常好,可以按预期工作:

在此处输入图片说明

Here is my main loop : 这是我的主循环:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private Button left;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        left = (Button) findViewById(R.id.left);
        left.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ViewGroup parent = (ViewGroup) view.getParent();
                ExpandAnimation animation = new ExpandAnimation(view, view.getBottom(), parent.getTop(), view.getLeft(), parent.getRight());
                animation.setDuration(3000);
                animation.setFillAfter(true);
                view.startAnimation(animation);
            }
        });
    }
}

But I have 3 major issues : 但是我有3个主要问题:

  • First, the text is gone and I don't know why... 首先,文字不见了,我也不知道为什么...
  • On the other hand, I want that my animation come over others views, but when I tried it wasn't working how can I achieve this ? 另一方面,我希望动画可以覆盖其他视图,但是当我尝试动画不起作用时,如何实现此目的?
  • And last but not least, at the end of my animation, the button is gone and there is just an ugly artefact... How can I avoid that ? 最后但并非最不重要的一点是,在动画结束时,按钮消失了,只有一个丑陋的假象……我该如何避免呢?

在此处输入图片说明

Edit : my layout : 编辑 :我的布局:

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="MainActivity">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:weightSum="3"
        android:padding="15dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:gravity="bottom">

        <Button
            android:id="@+id/left"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Retenter"
            android:textSize="15dp"
            android:textColor="#FFF"
            android:background="#FF33CC44" />

    </LinearLayout>
</RelativeLayout>

Edit2 : The artefact which appears at the end of the animation is due to the method setLayerType(View.LAYER_TYPE_SOFTWARE, null). Edit2:出现在动画末尾的伪影是由于setLayerType(View.LAYER_TYPE_SOFTWARE,null)方法引起的。

@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
    view.getLayoutParams().height = (int) (originalHeight + perValueH * interpolatedTime);
    view.getLayoutParams().width = (int) (originalWidth + (perValueW * interpolatedTime));
    view.requestLayout();
}

remove *2 will result below. 删除* 2将在下面显示。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM