android - 如何在其位置动态旋转图像?

标签 android android-layout animation android-animation

我想动态旋转图像(不是通过 XML 代码)。我正在使用此代码来旋转图像:

public void rotateAnimation(){
    // Create an animation instance
    Animation an = new RotateAnimation(30, 360);

    // Set the animation's parameters
    an.setDuration(2000);               // duration in ms
    an.setRepeatCount(0);                // -1 = infinite repeated
    an.setRepeatMode(Animation.REVERSE); // reverses each repeat
    an.setFillAfter(true);               // keep rotation after animation


    // Aply animation to image view
    scanCircle.setAnimation(an);
}

使用这段代码,我的 ImageView 得到了旋转,但它不是在它的位置上而是在它的位置之外。我想要的是在它自己的位置旋转它。

那么,如何让它成为可能?

最佳答案

我认为定位 pivotx 和 pivotY 会有所帮助 http://developer.android.com/reference/android/view/animation/RotateAnimation.html#RotateAnimation%28float,%20float,%20float,%20float%29

RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY)

假设你想从中间开始旋转

RotateAnimation(30, 360, totalWidth/2, totalHeight/2)

关于android - 如何在其位置动态旋转图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10278288/

相关文章:

android - 如何为 API 级别 < 23 的项目设置重力

java - 乔达 - 打印日期时间忽略时区

android - 无法访问 'java.lang.Object' 这是...的父类(super class)型检查您的模块类路径是否存在缺失或冲突的依赖项

android - Listview 不触发 onItemClickListener 事件

java - 将 TextView 动态添加到相对布局。

android - 通过代码在Android中创建子菜单

objective-c - 流畅的 UITextView 自动滚动到框架底部

angular - Ionic 2 中的页面过渡动画

python - 注释箭头 Prop 中间的动画列表

android - 尝试在两个 fragment 之间进行通信时 EventBus 不工作