如何围绕中心旋转箭头 Drawable
How to Rotate an Arrow Drawable about it's Center
我有一个可绘制的箭头,我只想旋转它而不在 x 或 y 方向上移动。
为了进一步解释,考虑一个围绕固定点旋转的球或圆形 Android 进度条。
此外,drawable 将在单击时旋转 -180 度,再次单击时旋转 180 度。
请问,这是否可以通过编程方式完成?
更新
根据 Jason Wihardja 的回答,我能够做到这一点:
@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);
}
}
}
但是有没有办法避免重复制作动画呢?就像只构建一次并在每次点击时切换度数。
您可以使用 RotateAnimation
class 来做到这一点。这是一个关于如何做到这一点的例子
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);
要朝不同的方向旋转,只需翻转角度参数即可。
我有一个可绘制的箭头,我只想旋转它而不在 x 或 y 方向上移动。 为了进一步解释,考虑一个围绕固定点旋转的球或圆形 Android 进度条。 此外,drawable 将在单击时旋转 -180 度,再次单击时旋转 180 度。
请问,这是否可以通过编程方式完成?
更新 根据 Jason Wihardja 的回答,我能够做到这一点:
@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);
}
}
}
但是有没有办法避免重复制作动画呢?就像只构建一次并在每次点击时切换度数。
您可以使用 RotateAnimation
class 来做到这一点。这是一个关于如何做到这一点的例子
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);
要朝不同的方向旋转,只需翻转角度参数即可。