如何在活动启动时立即启动动画,没有任何启动延迟

时间:2016-06-29 04:28:41

标签: android animation

我的MainAactivity从背景音乐(通过服务)开始,我已经实现了一个动画,该动画应该在活动与背景音乐一起启动时开始,但动画在活动启动10秒后开始(音乐开始于活动启动),即使我没有实施任何启动延迟。有人能帮我吗?我是android的新手。

我已经按照以下教程进行了动画制作: https://www.javacodegeeks.com/2012/10/android-leaf-fall-like-animation-using.html

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

1 个答案:

答案 0 :(得分:0)

当应用程序启动时,android会创建一个新进程,在此过程中根据应用程序主题或作为入口点的活动主题来充电并创建黑/白屏幕。 如果我们的应用程序很复杂并且会覆盖通常用于初始化分析的应用程序对象,则可以增加此负载。 阅读这篇文章了解更多信息。 http://saulmm.github.io/avoding-android-cold-starts