Android TextView 对象在 ObjectAnimator 启动之前可见
Android TextView object are visible before ObjectAnimator starts
我正在尝试使用 ObjectAnimator
执行一个简单的动画。这是来源:
TextView[] introTextViews = new TextView[] {(TextView) findViewById(R.id.intro_textView1_1),
(TextView) findViewById(R.id.intro_textView1_2),
(TextView) findViewById(R.id.intro_textView2_1),
(TextView) findViewById(R.id.intro_textView2_2),
(TextView) findViewById(R.id.intro_textView3_1),
(TextView) findViewById(R.id.intro_textView3_2),
(TextView) findViewById(R.id.intro_textView4_1),
(TextView) findViewById(R.id.intro_textView4_2)};
AnimatorSet animSet = new AnimatorSet();
ObjectAnimator[] animators = new ObjectAnimator[introTextViews.length];
for(int i=0; i<introTextViews.length; i++){
animators[i] = ObjectAnimator.ofFloat(introTextViews[i],
TextView.TRANSLATION_X, getResources().getDisplayMetrics().widthPixels,
0);
}
animSet.play(animators[0]).with(animators[1]); // Anim1
animSet.play(animators[2]).with(animators[3]).after(animators[0]); //Anim2
animSet.play(animators[4]).with(animators[5]).after(animators[2]); //Anim3
animSet.play(animators[6]).with(animators[7]).after(animators[4]); //Anim4
animSet.setDuration(6000);
animSet.setInterpolator(new AccelerateDecelerateInterpolator());
animSet.start();
问题是 TextView
对象在动画开始之前就在屏幕上。虽然 Anim1 是 运行,但属于 Anim2、Anim3、Anim4 的 TextView
对象在屏幕上。当 Anim2 时间到了 TextView
对象消失并开始动画,而 Anim3 和 Anim4 仍然可见,等待它们的时间到来!
我希望 TextView
对象不可见,直到它们在动画结束后进入屏幕。
最后我这样做了:
for(int i=0; i<introTextViews.length; i++){
//Add this line just to fix the animation initial X point
introTextViews[i].setX(getResources().getDisplayMetrics().widthPixels+10);
animators[i] = ObjectAnimator.ofFloat(introTextViews[i],
TextView.TRANSLATION_X, getResources().getDisplayMetrics().widthPixels,
0);
}
我知道有点"hacky",还在等官方答复!
你的方案很好,还有一个方案可以参考:
在你的 for 循环中,通过 setVisibility(View.Gone)
将所有 TextView 设置为不可见,然后为每个 Animator 设置 AnimatorListener
,并实现其 onAnimationStart
,在其中将相应的 TextView 设置为可见。
我正在尝试使用 ObjectAnimator
执行一个简单的动画。这是来源:
TextView[] introTextViews = new TextView[] {(TextView) findViewById(R.id.intro_textView1_1),
(TextView) findViewById(R.id.intro_textView1_2),
(TextView) findViewById(R.id.intro_textView2_1),
(TextView) findViewById(R.id.intro_textView2_2),
(TextView) findViewById(R.id.intro_textView3_1),
(TextView) findViewById(R.id.intro_textView3_2),
(TextView) findViewById(R.id.intro_textView4_1),
(TextView) findViewById(R.id.intro_textView4_2)};
AnimatorSet animSet = new AnimatorSet();
ObjectAnimator[] animators = new ObjectAnimator[introTextViews.length];
for(int i=0; i<introTextViews.length; i++){
animators[i] = ObjectAnimator.ofFloat(introTextViews[i],
TextView.TRANSLATION_X, getResources().getDisplayMetrics().widthPixels,
0);
}
animSet.play(animators[0]).with(animators[1]); // Anim1
animSet.play(animators[2]).with(animators[3]).after(animators[0]); //Anim2
animSet.play(animators[4]).with(animators[5]).after(animators[2]); //Anim3
animSet.play(animators[6]).with(animators[7]).after(animators[4]); //Anim4
animSet.setDuration(6000);
animSet.setInterpolator(new AccelerateDecelerateInterpolator());
animSet.start();
问题是 TextView
对象在动画开始之前就在屏幕上。虽然 Anim1 是 运行,但属于 Anim2、Anim3、Anim4 的 TextView
对象在屏幕上。当 Anim2 时间到了 TextView
对象消失并开始动画,而 Anim3 和 Anim4 仍然可见,等待它们的时间到来!
我希望 TextView
对象不可见,直到它们在动画结束后进入屏幕。
最后我这样做了:
for(int i=0; i<introTextViews.length; i++){
//Add this line just to fix the animation initial X point
introTextViews[i].setX(getResources().getDisplayMetrics().widthPixels+10);
animators[i] = ObjectAnimator.ofFloat(introTextViews[i],
TextView.TRANSLATION_X, getResources().getDisplayMetrics().widthPixels,
0);
}
我知道有点"hacky",还在等官方答复!
你的方案很好,还有一个方案可以参考:
在你的 for 循环中,通过 setVisibility(View.Gone)
将所有 TextView 设置为不可见,然后为每个 Animator 设置 AnimatorListener
,并实现其 onAnimationStart
,在其中将相应的 TextView 设置为可见。