同时旋转和缩放图像

时间:2015-08-27 15:11:13

标签: java android rotateanimation scaleanimation

我有这段代码,我想同时轮换和缩放ImageView

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

我尝试应用方法rotate()然后startScale(),但这对两者都不起作用。

有没有人有解决方案?

4 个答案:

答案 0 :(得分:3)

您可以使用名为NineOldAndroids的库。 在那里你玩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();

您还可以添加监听器

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

答案 1 :(得分:1)

您可以使用此库:https://github.com/Yalantis/uCrop

只需选择图像或编码图像路径(如果您不希望用户更改图像)。

答案 2 :(得分:0)

我猜你应该使用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();

答案 3 :(得分:0)

从Honeycomb动画开始更容易实现,来源:ViewPropertyAnimator

例如更改视图的坐标:

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