在TextView

时间:2017-05-17 15:29:06

标签: android android-animation

我有Button可以被无数次按下。按下后,我会在+1上方显示Button,然后使用AnimationTextView浮起并消失。这一切都很好。当用户反复点击Button时,我的问题就出现了。如果Animation仍在进行中并且正在浮动,则TextView的位置将重置,Animation将重新启动。

如何在同一个Animation上启动另一个Animation时继续允许TextView完成?我想我想要的最好的例子就是你收集硬币的游戏,每次收集硬币时你会看到+1漂浮在角色上方。

float_up.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="1500"
        android:fromYDelta="90%"
        android:toYDelta="0" />

    <alpha
        android:duration="1500"
        android:fromAlpha="1.0"
        android:toAlpha="0.0" />
</set>

MainActivity.Class

public class MainActivity extends AppCompatActivity  {

private long voteMultiplier = 1;
private TextView txtNoNumber;
private Animation fadeInAnimation;
private Button buttonNo;


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

    fadeInAnimation = AnimationUtils.loadAnimation(this, R.anim.float_up);

    buttonNo = (Button) findViewById(R.id.button_no);

    buttonNo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            voteNo();

        }
    });
}

private void voteNo() {
    txtNoNumber.setText(String.format("%s%s", "+ ", voteMultiplier));
    txtNoNumber.setTextColor(getResources().getColor(R.color.text_color_add));
    txtNoNumber.startAnimation(fadeInAnimation);

 }
}

我的ButtonTextView已在activity_main.xml中定义。他们没什么特别的。

谢谢。

2 个答案:

答案 0 :(得分:1)

以下有效,但我不能说这是最好的方法。我认为CanvasSurfaceView建议会更加健全。

您遇到的问题是动画发生在视图上,并且在动画期间只有一个视图被移动。这就是它重置的原因,你一次只能看到一个上升的数字。以下解决方案创建了一堆gone个视图,这些视图位于显示的基础视图的顶部。这些视图的ID保存在循环的数组中。单击该按钮后,下一个gone视图将变为可见和动画。在动画结束时,视图再次成为gone

这是一个影响的小视频:

Demo video

我夸大了动作,以便在视频中更好地展示。

<强> MainActivity.java

public class MainActivity extends AppCompatActivity {

    private long voteMultiplier = 1;
    private int[] txtNoNumber = new int[5];
    private Button buttonNo;
    private int animIndex = 0;

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

        buttonNo = (Button) findViewById(R.id.button_no);
        txtNoNumber[0] = R.id.txtNoNumber1;
        txtNoNumber[1] = R.id.txtNoNumber2;
        txtNoNumber[2] = R.id.txtNoNumber3;
        txtNoNumber[3] = R.id.txtNoNumber4;
        txtNoNumber[4] = R.id.txtNoNumber5;

        buttonNo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                voteNo();

            }
        });
    }

    private void voteNo() {
        TextView tv;
        Animation fadeInAnimation;

        if (animIndex >= txtNoNumber.length) {
            animIndex = 0;
        }
        tv = (TextView) findViewById(txtNoNumber[animIndex]);
        tv.setVisibility(View.VISIBLE);
        tv.setText(String.format("%s%s", "+ ", voteMultiplier++));
//        txtNoNumber.setTextColor(getResources().getColor(R.color.text_color_add));
        fadeInAnimation = AnimationUtils.loadAnimation(this, R.anim.float_up);
        fadeInAnimation.setAnimationListener(new MyAnimationListener(tv));
        tv.startAnimation(fadeInAnimation);
        animIndex++;
    }

    private class MyAnimationListener implements Animation.AnimationListener {
        View mView;

        MyAnimationListener(View view) {
            mView = view;
        }

        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            mView.setVisibility(View.GONE);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    }
}

<强> activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.floatanimation.MainActivity">

    <TextView
        android:id="@+id/txtNoNumber1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.319" />

    <TextView
        android:id="@+id/txtNoNumber2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.319" />

    <TextView
        android:id="@+id/txtNoNumber3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.319" />

    <TextView
        android:id="@+id/txtNoNumber4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.319" />

    <TextView
        android:id="@+id/txtNoNumber5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.319" />

    <Button
        android:id="@+id/button_no"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="HardcodedText" />

</android.support.constraint.ConstraintLayout>

答案 1 :(得分:0)

1。AnimationListener添加到动画fadeInAnimation

2。添加额外的变量voteCount以计算button点击次数。

3。onClick()方法中,仅在voteNo()时间调用first方法开始动画,而其他方法则从onAnimationEnd()方法处理

3。onAnimationEnd()方法内,检查voteCount的值。如果value大于0,请调用voteNo()方法再次启动动画。

以下是工作代码:

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements Animation.AnimationListener{

    private long voteMultiplier = 1;
    private TextView txtNoNumber;
    private Animation fadeInAnimation;
    private Button buttonNo;

    int voteCount  = 0;

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

        fadeInAnimation = AnimationUtils.loadAnimation(this, R.anim.float_up);
        fadeInAnimation.setAnimationListener(this);

        txtNoNumber = (TextView) findViewById(R.id.text_no_number);
        buttonNo = (Button) findViewById(R.id.button_no);

        buttonNo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                voteCount++;

                if (voteCount == 1)
                    voteNo();
            }
        });
    }

    private void voteNo() {
        txtNoNumber.setText(String.format("%s%s", "+ ", voteMultiplier));
        txtNoNumber.setTextColor(Color.GREEN);
        txtNoNumber.startAnimation(fadeInAnimation);
    }

    @Override
    public void onAnimationStart(Animation animation) {

    }

    @Override
    public void onAnimationEnd(Animation animation) {

        if (animation == fadeInAnimation) {
            voteCount--;

            if (voteCount > 0)
                voteNo();
        }
    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }
}

希望这会有所帮助〜