【问题标题】:animating ImageButton in android?在android中动画ImageButton?
【发布时间】:2011-09-27 04:55:44
【问题描述】:

我对 android 中的动画(以及几乎其他任何东西)真的很陌生。有没有办法为 ImageButton 设置动画?我只是想有时旋转按钮。就这样。有什么帮助吗?

谢谢。

【问题讨论】:

    标签: android animation imagebutton


    【解决方案1】:

    试试这个代码 sn-p。

    rotate.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate
    
        android:fromDegrees="0"
        android:toDegrees="360"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="0"
        android:duration="1000" />
    
    </set>
    

    在java文件中

    ImageButton imgbt = (ImageButton)findViewById(R.id.your_id);
    Animation ranim = (Animation)AnimationUtils.loadAnimation(context, R.anim.rotate);
    imgbt.setAnimation(ranim);
    

    【讨论】:

    • 在这一行收到java.lang.ClassCastException: android.view.animation.AnimationSet 错误:` RotateAnimation ranim = (RotateAnimation)AnimationUtils.loadAnimation(this, R.anim.rotate);`
    • 对此有什么帮助吗?我是动画的新手.. :(
    • 我认为该行应该使用Animation而不是RotateAnimation。
    • 另外,如果我想让圆形图像按钮围绕自身旋转怎么办?
    • 如果 imagebutton 的填充是统一的,这可以正常工作。谢谢。
    【解决方案2】:

    rotate.xml

    <?xml version="1.0" encoding="utf-8"?>
    <rotate xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="1000"
        android:fromDegrees="0"
        android:interpolator="@android:anim/linear_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="infinite"
        android:startOffset="0"
        android:toDegrees="360" />
    

    Java 代码:

    RotateAnimation rotateAnimation = (RotateAnimation) AnimationUtils.loadAnimation(context,R.anim.rotate);
    view.startAnimation(rotateAnimation);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多