Android “安卓如何连锁”;“爆炸”;文本视图中的文本动画?
我试图在textView中将3个“爆炸式”文本动画链接在一起,以便按顺序显示3个单词:“就绪”、“设置”和“开始!”。通过“分解”,我的意思是文本大小从默认值的0.25f变为默认值的1.00f,而alpha=0变为alpha=1 问题:我可以按预期将第一个单词“Ready”设置为“explode”,但下一个单词“Set”不会“explode”,即根本不会更改文本大小(只有动画的alpha部分有效) 我的MainActivity.java如下所示。我没有把第三次“爆炸”放进去,因为如果我能让第二次爆炸成功,那就是复制粘贴的问题Android “安卓如何连锁”;“爆炸”;文本视图中的文本动画?,android,textview,scale,objectanimator,animatorset,Android,Textview,Scale,Objectanimator,Animatorset,我试图在textView中将3个“爆炸式”文本动画链接在一起,以便按顺序显示3个单词:“就绪”、“设置”和“开始!”。通过“分解”,我的意思是文本大小从默认值的0.25f变为默认值的1.00f,而alpha=0变为alpha=1 问题:我可以按预期将第一个单词“Ready”设置为“explode”,但下一个单词“Set”不会“explode”,即根本不会更改文本大小(只有动画的alpha部分有效) 我的MainActivity.java如下所示。我没有把第三次“爆炸”放进去,因为如果我能让第二次
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
public void actionExplode(View view) {
// .setTextSize() defaults to sp but .getTextSize() defaults to px http://stackoverflow.com/a/3687385/1827488
String textReady = "Ready";
final String textSet = "Set";
final String textGo = "Go!";
final TextView questionDisplay = (TextView) findViewById(R.id.textView);
final float textSizePx = questionDisplay.getTextSize();
Log.i("actionExplode", "textSizePx=" + textSizePx);
final float scaleSmall = 0.25f;
float scaleFull = 1.0f;
final float fadeOut = 0f;
float fadeIn = 1f;
questionDisplay.setAlpha(fadeOut);
questionDisplay.setText(textReady);
questionDisplay.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSizePx * scaleSmall);
Log.i("actionExplode", ".getTextSize()=" + questionDisplay.getTextSize());
int animateDurationReadySetGo = 1000;
int animateDurationFudge = 100;
ObjectAnimator animateReadyFadeIn = ObjectAnimator.ofFloat(questionDisplay, "alpha", fadeOut, fadeIn);
animateReadyFadeIn.setDuration(animateDurationReadySetGo);
ObjectAnimator animateReadyX = ObjectAnimator.ofFloat(questionDisplay, "scaleX", scaleFull/scaleSmall);
animateReadyX.setDuration(animateDurationReadySetGo);
ObjectAnimator animateReadyY = ObjectAnimator.ofFloat(questionDisplay, "scaleY", scaleFull/scaleSmall);
animateReadyY.setDuration(animateDurationReadySetGo /* + animateDurationFudge */ );
animateReadyY.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
Log.i("onAnimationEnd", "before .getTextSize()=" + questionDisplay.getTextSize());
//questionDisplay.setAlpha(fadeOut);
questionDisplay.setText(textSet);
questionDisplay.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSizePx * scaleSmall);
Log.i("onAnimationEnd", "after .getTextSize()=" + questionDisplay.getTextSize());
}
});
ObjectAnimator animateSetFadeIn = ObjectAnimator.ofFloat(questionDisplay, "alpha", fadeOut, fadeIn);
animateSetFadeIn.setDuration(animateDurationReadySetGo);
ObjectAnimator animateSetX = ObjectAnimator.ofFloat(questionDisplay, "scaleX", scaleFull/scaleSmall);
animateSetX.setDuration(animateDurationReadySetGo);
ObjectAnimator animateSetY = ObjectAnimator.ofFloat(questionDisplay, "scaleY", scaleFull/scaleSmall);
animateSetY.setDuration(animateDurationReadySetGo /* + animateDurationFudge */ );
animateSetY.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
Log.i("onAnimationEnd", "before .getTextSize()=" + questionDisplay.getTextSize());
//questionDisplay.setAlpha(fadeOut);
questionDisplay.setText(textGo);
questionDisplay.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSizePx * scaleSmall);
Log.i("onAnimationEnd", "after .getTextSize()=" + questionDisplay.getTextSize());
}
});
AnimatorSet animateReadySetGo = new AnimatorSet();
animateReadySetGo.playTogether(animateReadyX, animateReadyY, animateReadyFadeIn);
animateReadySetGo.playTogether(animateSetX, animateSetY, animateSetFadeIn);
animateReadySetGo.playSequentially(animateReadyY, animateSetY);
animateReadySetGo.start();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
这是日志显示的内容。不合理的是:1)为什么“之前”行显示为43.75,而它们应该显示为175.0?2)为什么“后”行显示43.75,但文本大小不缩小
I/actionExplode: textSizePx=175.0
I/actionExplode: .getTextSize()=43.75
I/onAnimationEnd: before .getTextSize()=43.75
I/onAnimationEnd: after .getTextSize()=43.75
I/onAnimationEnd: before .getTextSize()=43.75
I/onAnimationEnd: after .getTextSize()=43.75
My activity_main.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"
tools:context="com.plaudev.explodingtext.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="@string/textView"
android:id="@+id/textView"
android:fontFamily="casual"
android:textSize="50sp"
android:textStyle="normal|bold"
android:textAlignment="center"
android:gravity="center" />
<Button
android:text="@string/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:id="@+id/button"
android:background="@color/colourTransparent"
android:textAllCaps="false"
android:textSize="25sp"
android:textStyle="normal|bold"
android:fontFamily="casual"
android:onClick="actionExplode"
android:textColor="@android:color/holo_green_dark" />
</RelativeLayout>
<?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"
tools:context="com.plaudev.explodingtext.MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/questionDisplay">
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="@string/textView"
android:id="@+id/question"
android:fontFamily="casual"
android:textSize="50sp"
android:textStyle="normal|bold"
android:textAlignment="center"
android:gravity="center"
android:layout_weight="1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="@string/textView"
android:id="@+id/ready"
android:fontFamily="casual"
android:textSize="50sp"
android:textStyle="normal|bold"
android:textAlignment="center"
android:gravity="center"
android:layout_weight="1"
android:visibility="gone"/>
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="@string/textView"
android:id="@+id/set"
android:fontFamily="casual"
android:textSize="50sp"
android:textStyle="normal|bold"
android:textAlignment="center"
android:gravity="center"
android:layout_weight="1"
android:visibility="gone"/>
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="@string/textView"
android:id="@+id/go"
android:fontFamily="casual"
android:textSize="50sp"
android:textStyle="normal|bold"
android:textAlignment="center"
android:gravity="center"
android:layout_weight="1"
android:visibility="gone"/>
<Button
android:text="@string/button"
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/button"
android:background="@color/colourTransparent"
android:textAllCaps="false"
android:textSize="25sp"
android:textStyle="normal|bold"
android:fontFamily="casual"
android:onClick="explodeThreeTextViews"
android:textColor="@android:color/holo_green_dark"
android:layout_weight="1" />
</LinearLayout>
</RelativeLayout>
上面更新的代码生成此日志:
I/startChainExplosion: explosionIndex=0, textSizeFullPx=175.0
I/actionExplode: textSizeFullPx=175.0, explosionIndex=0, textSizePx=175.0
I/actionExplode: .getTextSize()=43.75
I/onFinish: .getTextSize()=43.75
I/actionExplode: textSizeFullPx=175.0, explosionIndex=1, textSizePx=43.75
I/actionExplode: .getTextSize()=10.9375
I/onFinish: .getTextSize()=10.9375
I/actionExplode: textSizeFullPx=175.0, explosionIndex=2, textSizePx=10.9375
I/actionExplode: .getTextSize()=2.734375
I/onFinish: .getTextSize()=2.734375
更新2:按照@Xaver的建议,尝试将此作为onClick。但结果与我最初的尝试相同,即“准备”爆炸,但“设置”和“开始!”不爆炸。此外,在所有动画完成后,文本大小变得非常大(我猜是175px*4)。更新代码和日志如下。我有一种感觉,我需要每个单词都有自己的文本视图,以避免文本大小保留问题
public void explodeSequentially(View view) {
String textReady = "Ready";
final String textSet = "Set";
final String textGo = "Go!";
final TextView questionDisplay = (TextView) findViewById(R.id.textView);
float textSizePx = questionDisplay.getTextSize();
Log.i("explodeSequentially", "textSizePx=" + textSizePx);
float scaleSmall = 0.25f;
float scaleFull = 1.0f;
float fadeOut = 0f;
float fadeIn = 1f;
questionDisplay.setAlpha(fadeOut);
questionDisplay.setText(textReady);
questionDisplay.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSizePx * scaleSmall);
Log.i("explodeSequentially", ".getTextSize()=" + questionDisplay.getTextSize());
ObjectAnimator animateReadyFadeIn = ObjectAnimator.ofFloat(questionDisplay, "alpha", fadeOut, fadeIn);
animateReadyFadeIn.setDuration(animateDurationExplosion);
ObjectAnimator animateReadyX = ObjectAnimator.ofFloat(questionDisplay, "scaleX", scaleFull/scaleSmall);
animateReadyX.setDuration(animateDurationExplosion);
ObjectAnimator animateReadyY = ObjectAnimator.ofFloat(questionDisplay, "scaleY", scaleFull/scaleSmall);
animateReadyY.setDuration(animateDurationExplosion /* + animateDurationFudge */ );
animateReadyY.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
Log.i("onAnimationEnd", "before .getTextSize()=" + questionDisplay.getTextSize());
//questionDisplay.setAlpha(fadeOut);
questionDisplay.setText(textSet);
//questionDisplay.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSizePx * scaleSmall);
//Log.i("onAnimationEnd", "after .getTextSize()=" + questionDisplay.getTextSize());
}
});
AnimatorSet animateReady = new AnimatorSet();
animateReady.playTogether(animateReadyX, animateReadyY, animateReadyFadeIn);
ObjectAnimator animateSetFadeIn = ObjectAnimator.ofFloat(questionDisplay, "alpha", fadeOut, fadeIn);
animateSetFadeIn.setDuration(animateDurationExplosion);
ObjectAnimator animateSetX = ObjectAnimator.ofFloat(questionDisplay, "scaleX", scaleFull/scaleSmall);
animateSetX.setDuration(animateDurationExplosion);
ObjectAnimator animateSetY = ObjectAnimator.ofFloat(questionDisplay, "scaleY", scaleFull/scaleSmall);
animateSetY.setDuration(animateDurationExplosion /* + animateDurationFudge */ );
animateSetY.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
Log.i("onAnimationEnd", "before .getTextSize()=" + questionDisplay.getTextSize());
//questionDisplay.setAlpha(fadeOut);
questionDisplay.setText(textGo);
//questionDisplay.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSizePx * scaleSmall);
//Log.i("onAnimationEnd", "after .getTextSize()=" + questionDisplay.getTextSize());
}
});
AnimatorSet animateSet = new AnimatorSet();
animateSet.playTogether(animateSetX, animateSetY, animateSetFadeIn);
ObjectAnimator animateGoFadeIn = ObjectAnimator.ofFloat(questionDisplay, "alpha", fadeOut, fadeIn);
animateGoFadeIn.setDuration(animateDurationExplosion);
ObjectAnimator animateGoX = ObjectAnimator.ofFloat(questionDisplay, "scaleX", scaleFull/scaleSmall);
animateGoX.setDuration(animateDurationExplosion);
ObjectAnimator animateGoY = ObjectAnimator.ofFloat(questionDisplay, "scaleY", scaleFull/scaleSmall);
animateGoY.setDuration(animateDurationExplosion /* + animateDurationFudge */ );
animateGoY.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
Log.i("onAnimationEnd", "before .getTextSize()=" + questionDisplay.getTextSize());
//questionDisplay.setAlpha(fadeOut);
questionDisplay.setText("Here is the question!");
//questionDisplay.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSizePx * scaleSmall);
//Log.i("onAnimationEnd", "after .getTextSize()=" + questionDisplay.getTextSize());
}
});
AnimatorSet animateGo = new AnimatorSet();
animateGo.playTogether(animateGoX, animateGoY, animateGoFadeIn);
AnimatorSet animateReadySetGo = new AnimatorSet();
animateReadySetGo.playSequentially(animateReady, animateSet, animateGo);
animateReadySetGo.start();
}
日志呢
I/explodeSequentially: textSizePx=175.0
I/explodeSequentially: .getTextSize()=43.75
I/onAnimationEnd: before .getTextSize()=43.75
I/onAnimationEnd: before .getTextSize()=43.75
I/onAnimationEnd: before .getTextSize()=43.75
我的怀疑是正确的。每当在插座上使用
final
时,textView
s上的文本大小就会出现,如果我想链接动画,这种使用似乎是不可避免的。然而,通过将我想要“分解”的每个单词分解成它自己的textView
,至少我可以按照预期的顺序获得“分解”效果
下面的代码将只实现一次预期的动画序列,即下次单击按钮时,动画序列将从43.75px开始,而不是从最初的175.0px开始,并在每次单击时继续以0.25f系数减小。我将修改代码,动态创建并销毁textView
s,以便稍后解决此问题(现在更新如下)
新的MainActivity.java:
public class MainActivity extends AppCompatActivity {
public void explodeThreeTextViews (View view) {
int animateDurationExplosion = 1000;
float scaleSmall = 0.25f;
float scaleFull = 1.0f;
float fadeOut = 0f;
final float fadeIn = 1f;
final TextView tvQuestion = (TextView) findViewById(R.id.question);
final TextView tvReady = (TextView) findViewById(R.id.ready);
final TextView tvSet = (TextView) findViewById(R.id.set);
final TextView tvGo = (TextView) findViewById(R.id.go);
float tvReadySizePx = tvReady.getTextSize();
float tvSetSizePx = tvSet.getTextSize();
float tvGoSizePx = tvGo.getTextSize();
Log.i("explodeThreeTextViews", "tvReadySizePx=" + tvReadySizePx + ", tvSetSizePx=" + tvSetSizePx + ", tvGoSizePx=" + tvGoSizePx);
tvQuestion.setAlpha(fadeOut);
tvReady.setAlpha(fadeOut);
tvSet.setAlpha(fadeOut);
tvGo.setAlpha(fadeOut);
tvQuestion.setText("The question!");
tvReady.setText("Ready");
tvSet.setText("Set");
tvGo.setText("Go!");
tvQuestion.setVisibility(View.GONE);
tvReady.setVisibility(View.VISIBLE);
tvReady.setTextSize(TypedValue.COMPLEX_UNIT_PX, tvReadySizePx * scaleSmall);
tvSet.setTextSize(TypedValue.COMPLEX_UNIT_PX, tvSetSizePx * scaleSmall);
tvGo.setTextSize(TypedValue.COMPLEX_UNIT_PX, tvGoSizePx * scaleSmall);
Log.i("explodeThreeTextViews", "tvReady.getTextSize=" + tvReady.getTextSize() + ", tvSet.getTextSize=" + tvSet.getTextSize() + ", tvGo.getTextSize=" + tvGo.getTextSize());
ObjectAnimator animateReadyFadeIn = ObjectAnimator.ofFloat(tvReady, "alpha", fadeOut, fadeIn);
animateReadyFadeIn.setDuration(animateDurationExplosion);
ObjectAnimator animateReadyX = ObjectAnimator.ofFloat(tvReady, "scaleX", scaleFull/scaleSmall);
animateReadyX.setDuration(animateDurationExplosion);
ObjectAnimator animateReadyY = ObjectAnimator.ofFloat(tvReady, "scaleY", scaleFull/scaleSmall);
animateReadyY.setDuration(animateDurationExplosion /* + animateDurationFudge */ );
animateReadyY.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
Log.i("onAnimationEnd", "tvReady.getTextSize()=" + tvReady.getTextSize());
//questionDisplay.setAlpha(fadeOut);
tvReady.setVisibility(View.GONE);
tvSet.setVisibility(View.VISIBLE);
//questionDisplay.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSizePx * scaleSmall);
//Log.i("onAnimationEnd", "after .getTextSize()=" + questionDisplay.getTextSize());
}
});
AnimatorSet animateReady = new AnimatorSet();
animateReady.playTogether(animateReadyX, animateReadyY, animateReadyFadeIn);
ObjectAnimator animateSetFadeIn = ObjectAnimator.ofFloat(tvSet, "alpha", fadeOut, fadeIn);
animateSetFadeIn.setDuration(animateDurationExplosion);
ObjectAnimator animateSetX = ObjectAnimator.ofFloat(tvSet, "scaleX", scaleFull/scaleSmall);
animateSetX.setDuration(animateDurationExplosion);
ObjectAnimator animateSetY = ObjectAnimator.ofFloat(tvSet, "scaleY", scaleFull/scaleSmall);
animateSetY.setDuration(animateDurationExplosion /* + animateDurationFudge */ );
animateSetY.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
Log.i("onAnimationEnd", "tvSet.getTextSize()=" + tvSet.getTextSize());
//questionDisplay.setAlpha(fadeOut);
tvSet.setVisibility(View.GONE);
tvGo.setVisibility(View.VISIBLE);
//questionDisplay.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSizePx * scaleSmall);
//Log.i("onAnimationEnd", "after .getTextSize()=" + questionDisplay.getTextSize());
}
});
AnimatorSet animateSet = new AnimatorSet();
animateSet.playTogether(animateSetX, animateSetY, animateSetFadeIn);
ObjectAnimator animateGoFadeIn = ObjectAnimator.ofFloat(tvGo, "alpha", fadeOut, fadeIn);
animateGoFadeIn.setDuration(animateDurationExplosion);
ObjectAnimator animateGoX = ObjectAnimator.ofFloat(tvGo, "scaleX", scaleFull/scaleSmall);
animateGoX.setDuration(animateDurationExplosion);
ObjectAnimator animateGoY = ObjectAnimator.ofFloat(tvGo, "scaleY", scaleFull/scaleSmall);
animateGoY.setDuration(animateDurationExplosion /* + animateDurationFudge */ );
animateGoY.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
Log.i("onAnimationEnd", "tvGo.getTextSize()=" + tvGo.getTextSize());
//questionDisplay.setAlpha(fadeOut);
tvGo.setVisibility(View.GONE);
tvQuestion.setVisibility(View.VISIBLE);
tvQuestion.setAlpha(fadeIn);
//questionDisplay.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSizePx * scaleSmall);
//Log.i("onAnimationEnd", "after .getTextSize()=" + questionDisplay.getTextSize());
}
});
AnimatorSet animateGo = new AnimatorSet();
animateGo.playTogether(animateGoX, animateGoY, animateGoFadeIn);
AnimatorSet animateReadySetGo = new AnimatorSet();
animateReadySetGo.playSequentially(animateReady, animateSet, animateGo);
animateReadySetGo.start();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
新的activity_main.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"
tools:context="com.plaudev.explodingtext.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="@string/textView"
android:id="@+id/textView"
android:fontFamily="casual"
android:textSize="50sp"
android:textStyle="normal|bold"
android:textAlignment="center"
android:gravity="center" />
<Button
android:text="@string/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:id="@+id/button"
android:background="@color/colourTransparent"
android:textAllCaps="false"
android:textSize="25sp"
android:textStyle="normal|bold"
android:fontFamily="casual"
android:onClick="actionExplode"
android:textColor="@android:color/holo_green_dark" />
</RelativeLayout>
<?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"
tools:context="com.plaudev.explodingtext.MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/questionDisplay">
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="@string/textView"
android:id="@+id/question"
android:fontFamily="casual"
android:textSize="50sp"
android:textStyle="normal|bold"
android:textAlignment="center"
android:gravity="center"
android:layout_weight="1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="@string/textView"
android:id="@+id/ready"
android:fontFamily="casual"
android:textSize="50sp"
android:textStyle="normal|bold"
android:textAlignment="center"
android:gravity="center"
android:layout_weight="1"
android:visibility="gone"/>
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="@string/textView"
android:id="@+id/set"
android:fontFamily="casual"
android:textSize="50sp"
android:textStyle="normal|bold"
android:textAlignment="center"
android:gravity="center"
android:layout_weight="1"
android:visibility="gone"/>
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="@string/textView"
android:id="@+id/go"
android:fontFamily="casual"
android:textSize="50sp"
android:textStyle="normal|bold"
android:textAlignment="center"
android:gravity="center"
android:layout_weight="1"
android:visibility="gone"/>
<Button
android:text="@string/button"
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/button"
android:background="@color/colourTransparent"
android:textAllCaps="false"
android:textSize="25sp"
android:textStyle="normal|bold"
android:fontFamily="casual"
android:onClick="explodeThreeTextViews"
android:textColor="@android:color/holo_green_dark"
android:layout_weight="1" />
</LinearLayout>
</RelativeLayout>
更新:使用动态创建和销毁的视图,以支持基于先前尝试的递归方法重复使用
更新的MainActivity.java-您可以选择使用动画侦听器或倒计时来链接“爆炸”:
更新日志:
I/prepareChainExplosions: textSizeFullPx=175.0
I/prepareChainExplosions: questionDisplay.getChildCount()=7
I/startChainExplosions: explosionIndex=0
I/actionExplode: explosionIndex=0, textSizePx=175.0
I/actionExplode: .getTextSize()=43.75
I/onAnimationEnd: .getTextSize()=43.75
I/actionExplode: explosionIndex=1, textSizePx=175.0
I/actionExplode: .getTextSize()=43.75
I/onAnimationEnd: .getTextSize()=43.75
I/actionExplode: explosionIndex=2, textSizePx=175.0
I/actionExplode: .getTextSize()=43.75
I/startChainExplosions: questionDisplay.getChildCount()=4
I/onAnimationEnd: .getTextSize()=43.75
问题是您的
动画师集
。订购动画时,您过高估计了AnimatorSet
的智能程度。更直截了当地说:AnimatorSet
对您没有任何帮助,如果您告诉它两次并行运行,然后告诉它按顺序运行,那么将要发生的就是所有动画同时运行,然后只有顺序调用中引用的第二个动画再次运行(如果有)。您需要做的是为每个动画创建三个AnimatorSet
实例进入动画,然后在另一个AnimatorSet
@Xaver,thx中按顺序运行这些AnimatorSet
s,但仍然没有获得预期的行为
<?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"
tools:context="com.plaudev.explodingtext.MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/questionDisplay">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/textView"
android:id="@+id/questionView"
android:fontFamily="casual"
android:textSize="50sp"
android:textStyle="normal|bold"
android:layout_weight="1"
android:gravity="center" />
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="@string/textView"
android:id="@+id/readyView"
android:fontFamily="casual"
android:textSize="50sp"
android:textStyle="normal|bold"
android:textAlignment="center"
android:gravity="center"
android:layout_weight="1"
android:visibility="gone"/>
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="@string/textView"
android:id="@+id/setView"
android:fontFamily="casual"
android:textSize="50sp"
android:textStyle="normal|bold"
android:textAlignment="center"
android:gravity="center"
android:layout_weight="1"
android:visibility="gone"/>
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="@string/textView"
android:id="@+id/goView"
android:fontFamily="casual"
android:textSize="50sp"
android:textStyle="normal|bold"
android:textAlignment="center"
android:gravity="center"
android:layout_weight="1"
android:visibility="gone"/>
</LinearLayout>
<Button
android:text="@string/button"
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/button"
android:background="@color/colourTransparent"
android:textAllCaps="false"
android:textSize="25sp"
android:textStyle="normal|bold"
android:fontFamily="casual"
android:onClick="startChainExplosions"
android:textColor="@android:color/holo_green_dark"
android:layout_below="@+id/questionDisplay"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
I/prepareChainExplosions: textSizeFullPx=175.0
I/prepareChainExplosions: questionDisplay.getChildCount()=7
I/startChainExplosions: explosionIndex=0
I/actionExplode: explosionIndex=0, textSizePx=175.0
I/actionExplode: .getTextSize()=43.75
I/onAnimationEnd: .getTextSize()=43.75
I/actionExplode: explosionIndex=1, textSizePx=175.0
I/actionExplode: .getTextSize()=43.75
I/onAnimationEnd: .getTextSize()=43.75
I/actionExplode: explosionIndex=2, textSizePx=175.0
I/actionExplode: .getTextSize()=43.75
I/startChainExplosions: questionDisplay.getChildCount()=4
I/onAnimationEnd: .getTextSize()=43.75