martes, 22 de septiembre de 2015

Android: Animation with Animators and Animation Set

Heart animating
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
     params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
     params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
     
     Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.heart_icon);
     
     final ImageView image = new ImageView(getActivity());
     image.setLayoutParams(params);
     image.setImageBitmap(bitmap);
     
     heartsContainer.addView(image);
     params.leftMargin = bitmap.getWidth()/2;
     params.topMargin = bitmap.getHeight();
        
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(image, "scaleX", 0.5f, 1.0f);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(image, "scaleY", 0.5f, 1.0f);
        scaleX.setDuration(ANIMATION_APPEAR_TIME);
        scaleY.setDuration(ANIMATION_APPEAR_TIME);
        
        ObjectAnimator translateY = ObjectAnimator.ofFloat(image, "translationY", -(getResources().getDimension(R.dimen.hearts_container_height) - bitmap.getHeight()));
        translateY.setDuration(ANIMATION_TIME);
        
        ObjectAnimator horizontal = ObjectAnimator.ofFloat(image, "translationX", getRandomValueAllowNegative(bitmap.getWidth() * 1.5f, true));
        horizontal.setDuration(ANIMATION_TIME - 500);
        horizontal.setStartDelay(ANIMATION_APPEAR_TIME + 300);
        
        ObjectAnimator rotation = ObjectAnimator.ofFloat(image , "rotation", getRandomValueAllowNegative(60f, true));
        rotation.setDuration((long)getRandomValueAllowNegative(ANIMATION_TIME, false));
        
        ObjectAnimator alpha = ObjectAnimator.ofFloat(image , "alpha", 0f);
        alpha.setDuration(ANIMATION_TIME/2);
        alpha.setStartDelay(ANIMATION_TIME/2 + ANIMATION_APPEAR_TIME);
        
        AnimatorSet animationSet = new AnimatorSet();
        animationSet.play(scaleX).with(scaleY).with(translateY).with(horizontal).with(rotation).with(alpha);

No hay comentarios:

Publicar un comentario