Issue
I have an arrow drawable that I simply want to rotate without it moving either in the x or y direction. To explain further, consider a ball spinning about a fixed point or the circular Android progressbar. Additionally, the drawable will rotate -180 degree when clicked and 180 degress when clicked again.
Please, is there anyway this can be done programmatically?
Update From Jason Wihardja's answer, I was able to achieve this:
@Override
public void onClick(View v) {
if (v.getId() == imageButton.getId()) {
int visibilty =newsBody.getVisibility();
if (visibilty == View.VISIBLE) {
Animation animation = new RotateAnimation(180.0f, 360.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setInterpolator(new DecelerateInterpolator());
animation.setRepeatCount(0);
animation.setFillAfter(true);
animation.setDuration(300);
imageButton.startAnimation(animation);
newsBody.setVisibility(View.GONE);
} else {
Animation animation = new RotateAnimation(0.0f, 180.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setInterpolator(new DecelerateInterpolator());
animation.setRepeatCount(0);
animation.setFillAfter(true);
animation.setDuration(300);
imageButton.startAnimation(animation);
newsBody.setVisibility(View.VISIBLE);
}
}
}
But is there anyway I can avoid repeating the animation building? Like just build it once and toggle the degrees in each click.
Solution
You could use RotateAnimation
class to do that. Here's an example on how to do that
RotateAnimation rotateAnimation = new RotateAnimation(180.0f, 0.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setInterpolator(new DecelerateInterpolator());
rotateAnimation.setRepeatCount(0);
rotateAnimation.setDuration(animationDuration);
rotateAnimation.setFillAfter(true);
arrowImageView.startAnimation(rotateAnimation);
To rotate in a different direction, just flip the angle parameters.
Answered By - user5383152
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.