在Android中完成动画后移动下一个活动

时间:2018-07-23 15:22:18

标签: android android-activity android-animation

我在Android中使用 RotatingTextWrapper 进行文字动画处理。运行良好。但是我想通过指定时间间隔自动完成动画后移动下一个活动。我已经为此使用Thread并将动画放置在thread函数内部。不幸的是,动画不起作用,但是几秒钟后,“活动”自动打开。请帮我。 这是源代码

public class MainActivity extends AppCompatActivity {

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

        tv=findViewById(R.id.txt);

       final Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/Raleway-Light.ttf");
       final Typeface typeface2 = Typeface.createFromAsset(getAssets(), "fonts/Reckoner_Bold.ttf");

        tv.setTypeface(typeface2);

        timer=new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {

                RotatingTextWrapper rotatingTextWrapper = (RotatingTextWrapper) findViewById(R.id.custom_switcher);
                rotatingTextWrapper.setSize(30);
                rotatingTextWrapper.setTypeface(typeface2);

                Rotatable rotatable = new Rotatable(Color.parseColor("#af030f"), 3000, "Xafa", "Hazil", "Uzr","Sizni","Juda");
                rotatable.setSize(30);
                rotatable.setTypeface(typeface);
                rotatable.setInterpolator(new AccelerateInterpolator());
                rotatable.setAnimationDuration(600);

                Rotatable rotatable2 = new Rotatable(Color.parseColor("#123456"), 3000, "qimoqchimasdim", "Ediku", "So`rayman","Yoqtiraman","ham");
                rotatable2.setSize(30);
                rotatable2.setTypeface(typeface);
                rotatable2.setInterpolator(new DecelerateInterpolator());
                rotatable2.setAnimationDuration(600);

                rotatingTextWrapper.setContent("?  ?", rotatable, rotatable2);

                Intent i=new Intent(getApplicationContext(),Main2Activity.class);
                startActivity(i);
                finish();

            }
        },4000);

    }

}

2 个答案:

答案 0 :(得分:0)

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

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {

            //logic
        }
    }, 4000);

}

答案 1 :(得分:0)

将动画置于活动生命周期的onResume()阶段。根据{{​​3}},onStart()“使用户可见活动...”和onResume()“ ... app与用户交互...”。

由于您希望动画对用户可见,因此我将其放置在onStart()中,并使用设计逻辑使onResume()处理动画。

public class MainActivity extends AppCompatActivity{
 @OnCreate()
 // instantiations class member variables
 @OnStart()
 // timer code for animation
 @OnResume()
 Intent i=new Intent(getApplicationContext(),Main2Activity.class);
 this.startActivity(i);
}