Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android two animations at once

I used ValueAnimator to move an ImageView through a parabolic way, and it worked like a charm. Now i'm trying to add another animation to the same ImageView, a rotation arround itself using XML code. When i use the animation of rotation alone (without the ValueAnimator), it works fine, but when i launch the both animation at once, the result is completely wrong, the image goes through an wrong way, sometimes it blocks. Here's the codes:

this.ValueAnimator1 = ValueAnimator.ofFloat(0.0f,1.0f);
this.ValueAnimator1.setDuration(4000);
this.ValueAnimator1.addUpdateListener(new AnimatorUpdateListener(){
    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
        float t = ((Float)ValueAnimator1.getAnimatedValue()).floatValue();
        float xPos = p0.x*(1-t)*(1-t) + 
            p1.x*2*t*(1-t) + p2.x*t*t;
        float yPos = p0.y*(1-t)*(1-t) + 
            p1.y*2*t*(1-t) + p2.y*t*t;
        ImageView1.setTranslationX(xPos); 
        ImageView1.setTranslationY(yPos);
    }
});
this.Animation1 = AnimationUtils.loadAnimation(this,R.anim.rotation_projectile);
ImageView1.startAnimation(rotationProjectile);
ValueAnimator1.start();

<?xml version="1.0" encoding="utf-8"?>
<rotate 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0" 
    android:toDegrees="90"
    android:pivotX="50%"
    android:pivotY="50%"
    android:fillEnabled="true"
    android:fillAfter="true"
    android:duration="4000"
/>
like image 779
Mssm Avatar asked Feb 06 '23 08:02

Mssm


1 Answers

You can use an AnimatorSet to play your animations together.

However, you'd have to use only animators then. This can easily be achieved using ObjectAnimator.

    ObjectAnimator rotateAnimator = ObjectAnimator.ofFloat(imageview, "rotation", 0f, 90f);
    rotateAnimator.setDuration(4000); 

Then play both animators using AnimatorSet.playTogether(Animator... items)

If you want to use XML for the animator

    <objectAnimator
        android:propertyName="rotation"
        android:duration="4000"
        android:valueType:"floatType"
        android:valueFrom="0f"
        android:valueTo="90f" />

EDIT

    final ImageView imageview = (ImageView) findViewById(R.id.imageView);
    final ValueAnimator translateAnimator = ValueAnimator.ofFloat(0.0f, 1.0f);
    ObjectAnimator rotateAnimator = ObjectAnimator.ofFloat(imageview, "rotation", 0f, 90f);

    final AnimatorSet animatorSet = new AnimatorSet();
    animatorSet.playTogether(translateAnimator, rotateAnimator);
    animatorSet.setDuration(4000);

    final float x = imageview.getX();
    final float y = imageview.getY();
    translateAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            float t = (Float) translateAnimator.getAnimatedValue();
            imageview.setTranslationX(x + t*100);    // do your own
            imageview.setTranslationY(y + t*100);    // thing here

        }
    });

    imageview.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            animatorSet.start();
        }
    });
like image 143
Wisph Avatar answered Feb 13 '23 05:02

Wisph