LolloAAA
LolloAAA

Reputation: 110

Rotate and scale image at same time

I have this code and I want to rotate and scale an ImageView at the same time:

public class LayoutPunteggio extends RelativeLayout {

TextView ok;
LayoutInflater inflater;
RotateAnimation rotateAnimation1;

public LayoutPunteggio(Context context) {
    super(context);
    inflater = LayoutInflater.from(context);
    init();
}


public void init() {
    mano = new ImageView(getContext());
    mano.setImageResource(R.drawable.mano);
    mano.setX(100);
    mano.setY(100);
    addView(mano);

    startScale(mano);
    rotate();
}

public void rotate() {
    rotateAnimation1.setInterpolator(new LinearInterpolator());
    rotateAnimation1.setDuration(1000);
    rotateAnimation1.setRepeatCount(-1);
    mano.startAnimation(rotateAnimation1);
}

public void startScale(View view){
    ScaleAnimation animation;
    animation=new ScaleAnimation(1,2,1,2,1000, 1000);
    animation.setDuration(1000);
    view.startAnimation(animation);
}
}

I tried to apply the method rotate() then startScale(), but this doesn't work for both.

Does anyone have a solution?

Upvotes: 1

Views: 1686

Answers (4)

Adhish Lal
Adhish Lal

Reputation: 151

You can use this library: https://github.com/Yalantis/uCrop

Just pick the image or code the image path(if you don't want the user to change the image).

Upvotes: 1

Alex
Alex

Reputation: 54

You can use a library called NineOldAndroids. There you have playTogether function of the AnimatorSet.

AnimatorSet animation = new AnimatorSet();
animation.playTogether(
   ObjectAnimator.ofFloat(yourImageView, "rotation", 0, 360),
   ObjectAnimator.ofFloat(yourImageView, "scaleX", 1, 2f),
   ObjectAnimator.ofFloat(yourImageView, "scaleY", 1, 2f)
);
animation.setDuratio(1000);
animation.start();

You can also add a listener

animation.addListener(new AnimationListener(){
   onAnimationStart....
   onAnimationRepeat...
   onAnimationEnd...
   onAnimationCancel...
});

Upvotes: 3

Konstantin Berkov
Konstantin Berkov

Reputation: 1212

Starting from Honeycomb animations are lot easier to implement, source: ViewPropertyAnimator

For instance changing coordinates of view:

ObjectAnimator animX = ObjectAnimator.ofFloat(myView, "x", 50f);
ObjectAnimator animY = ObjectAnimator.ofFloat(myView, "y", 100f);
AnimatorSet animSetXY = new AnimatorSet();
animSetXY.playTogether(animX, animY);
animSetXY.start();

Upvotes: 0

Alex S. Diaz
Alex S. Diaz

Reputation: 2667

I guess you should use AnimationSet:

AnimationSet as = new AnimationSet(true);

// config rotation animation
RotateAnimation ra = new RotateAnimation(...);
ra.setDuration(1000);
...

// config scale animation
ScaleAnimation sa = new ScaleAnimation(...);
sa.setDuration(1000);
...

// Add animations
as.addAnimation(ra);
as.addAnimation(sa);

as.start();

Upvotes: 0

Related Questions