简体   繁体   English

无法使用动画android / java开始活动

[英]can't start activity with animation android/java

The animation on the first activity does not work when I use startActivity and I don't know it's strange. 当我使用startActivity并且不知道这很奇怪时,第一个活动的动画不起作用。

public class MainActivity extends AppCompatActivity {

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

        getSupportActionBar().hide();

        ImageView img = (ImageView) findViewById(R.id.splash);
        img.animate().scaleX(0.6f).scaleY(0.6f).rotation(1080f).setDuration(2000);
        Intent intent = new Intent(getApplicationContext(), NextActivity.class);
        startActivity(intent);
    }
}

Use this. 用这个。

    private Handler handlerImage;
    private Runnable runnableImage;

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

    getSupportActionBar().hide();

    ImageView img= (ImageView) findViewById(R.id.splash);
    img.animate().scaleX(0.6f).scaleY(0.6f).rotation(1080f).setDuration(2000);
    handlerImage = new Handler();
    runnableImage = new Runnable() {
        @Override
        public void run() {
            Intent intent=new Intent(getApplicationContext(),NextActivity.class);
            startActivity(intent);
        }
    };
    handlerImage.postDelayed(runnableImage, 3000);

}



@Override
protected void onDestroy() {
    super.onDestroy();
    if (handlerImage != null) {
        handlerImage.removeCallbacks(runnableImage);
    }

}

I hope you are trying to show a scale and rotation animation of your APP logo for sometime and then trying to launch Another Activity. 希望您尝试一段时间显示APP徽标的比例和旋转动画,然后尝试启动“另一个活动”。

Since you are starting Another activity ,Before Starting Animation on the Imageview, ActivityManager will switch to the Next Animation, and hence you are not able to see the Animation, Solution is to set an Animationlistner to the ViewPropertyAnimator object given to ImageView and Start the main Activity on onAnimationEnd() callback. 由于您正在启动另一个活动,因此在Imageview上启动Animation之前,ActivityManager将切换到Next Animation,因此您将无法看到Animation,解决方案是将Animationlistner设置为赋予ImageView的ViewPropertyAnimator对象并启动主onAnimationEnd()回调上的活动。

code snippet is given below: 代码段如下所示:

    img.animate().scaleX(0.6f).scaleY(0.6f).rotation(1080f).setDuration(2000).setListener(new AnimatorListener() {

        @Override
        public void onAnimationStart(Animator animation) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationRepeat(Animator animation) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationEnd(Animator animation) {
            Intent intent=new Intent(getApplicationContext(),NextActivity.class);
            startActivity(intent);

        }

        @Override
        public void onAnimationCancel(Animator animation) {
            // TODO Auto-generated method stub

        }
    });

Check this, 检查一下

Intent intent=new Intent(this,NextActivity.class);
startActivity(intent);

@Zakaria Ait Ouchrif, @Zakaria Ait Ouchrif,

You can use the method of Activity overridePendingTransition() with two xml of in and out Animation. 您可以将ActivityoverPendingTransition()方法与in和out动画的两个xml一起使用。

You can define simple transition animations in an XML resource file. 您可以在XML资源文件中定义简单的过渡动画。

Also you can refer this tutorial for same. 您也可以参考本教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM