代码之家  ›  专栏  ›  技术社区  ›  LiTTle

在ObjectAnimator启动之前,Android TextView对象是可见的

  •  0
  • LiTTle  · 技术社区  · 9 年前

    我正在尝试使用执行一个简单的动画 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的对象在屏幕上。当Anim2时间到来时 文本框 对象消失并开始动画,而Anim3和Anim4仍然可见,等待它们的到来! 我想要 文本框 对象在动画结束后进入屏幕之前是不可见的。

    2 回复  |  直到 9 年前
        1
  •  0
  •   LiTTle    9 年前

    最后我做到了:

    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);
            }
    

    我知道这有点“黑”,我仍在等待任何官方答复!

        2
  •  0
  •   starkshang    9 年前

    您的解决方案很好,您可以参考另一个解决方案:

    在for循环中,通过将所有TextView设置为不可见 setVisibility(View.Gone) ,然后设置 AnimatorListener 为每个动画师,并实现其 onAnimationStart ,其中您将相应的TextView设置为可见。