简体   繁体   中英

How to start an animation immediately on activity launch without any start delay

My MainAactivity starts with a background music( via service) and I have implemented an animation which should start as soon as the activity is launched along with the background music, but the animation starts after 10 seconds of activity launch(music starts with activity launch), even though I have not implemented any start delay. Can someone help me? I am new to android.

I have followed the following tutorial for animation: https://www.javacodegeeks.com/2012/10/android-leaf-fall-like-animation-using.html

My MainActivity.java:

private int[] PICTURES = {
        R.mipmap.f1, R.mipmap.f2, R.mipmap.f3, R.mipmap.f4,
};

private Rect display_Size = new Rect();
private RelativeLayout root_Layout;
private ArrayList<View> all_imageViews = new ArrayList<View>();
private float scale;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent i = new Intent(this,MusicService.class);
    startService(i);

    Display display = getWindowManager().getDefaultDisplay();
    display.getRectSize(display_Size);

    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);
    scale = metrics.density;

    root_Layout = (RelativeLayout) findViewById(R.id.main_layout);

    new Timer().schedule(new ExecTime(),0 , 1000);
}

private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        int viewId = new Random().nextInt(PICTURES.length);
        Drawable d = getResources().getDrawable(PICTURES[viewId]);
        LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
        ImageView imageView = (ImageView) inflater.inflate(R.layout.animated_image_view, null);
        imageView.setImageDrawable(d);
        root_Layout.addView(imageView);

        all_imageViews.add(imageView);

        RelativeLayout.LayoutParams animationLayout = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
        animationLayout.setMargins(0, (int)(-3000*scale), 0, 0);
        animationLayout.width = (int) (50*scale);
        animationLayout.height = (int) (50*scale);

        startAnimation(imageView);
    }
};

public void startAnimation(final ImageView animView) {

    animView.setPivotX(animView.getWidth()/2);
    animView.setPivotY(animView.getHeight()/2);

    final ValueAnimator animator = ValueAnimator.ofFloat(0, 1);
    animator.setDuration(15000);

    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

        int angle = 50 + (int)(Math.random() * 101);
        int move = new Random().nextInt(display_Size.right);

        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            float value = (Float) (animation.getAnimatedValue());
            animView.setRotation(angle*value);
            animView.setTranslationX((move-100)*value);
            animView.setTranslationY((display_Size.bottom + (3000*scale))*value);

        }
     });

     animator.start();

 }

private class ExecTime extends TimerTask {
    @Override
    public void run() {
        handler.sendEmptyMessage(Constants.EMPTY_MESSAGE);
    }
}

When application is launched android creates a new process that during it charge and create a black/white screen based on the application theme or the theme of the activity that is entry point. This load can be increased if our application is complex and overwrites the application object, which is normally used to initialize the analytics. Read this article for more info. http://saulmm.github.io/avoding-android-cold-starts

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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