AndroidStudio中的图片持续旋转
创始人
2024-08-20 20:00:11
0
  1. 定义旋转动画:
RotateAnimation rotateAnimation = new RotateAnimation(0, 359, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

rotateAnimation.setDuration(1000); // 旋转一周所用的时间

rotateAnimation.setRepeatCount(Animation.INFINITE);// 无限次重复
  1. 在代码中控制开始和停止旋转:
// 开始旋转
imageView.startAnimation(rotateAnimation);

// 停止旋转
imageView.clearAnimation();

注意:在停止旋转前,需要先判断是否存在正在运行的旋转动画。

完整的代码示例:

layout.xml


MainActivity.java

public class MainActivity extends AppCompatActivity {

    private ImageView imageView;
    private RotateAnimation rotateAnimation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = findViewById(R.id.imageView);

        rotateAnimation = new RotateAnimation(0, 359, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotateAnimation.setDuration(1000);
        rotateAnimation.setRepeatCount(Animation.INFINITE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        // 释放资源
        imageView.clearAnimation();
        imageView.setImageBitmap(null);
        imageView.setImageDrawable(null);
    }

    public void startRotate(View view) {
        if (rotateAnimation != null && !rotateAnimation.hasStarted()) {
            imageView.startAnimation(rotateAnimation);
        }
    }

    public void stopRotate(View view) {
        if (rotateAnimation != null && rotateAnimation.hasStarted() && !rotateAnimation.hasEnded()) {
            imageView.clearAnimation();
        }
    }
}

其中,startRotate和stopRotate是两个按钮的点击事件,用于

相关内容

热门资讯

透视透视!wepoker底牌透... 透视透视!wepoker底牌透视脚本怎样安装,约局吧如何查看是否有挂,介绍教程(本然是真的有挂);w...
透视游戏!wpk透视脚本,we... WePoker透视辅助版本解析‌,透视游戏!wpk透视脚本,wepoker黑侠破解,wepoke教程...
透视科技!wpk辅助购买,wp... 透视科技!wpk辅助购买,wpk插件辅助,切实教程(一向真的有挂);wpk辅助购买是一款益智类棋牌手...
透视有挂!wepoker透视脚... 透视有挂!wepoker透视脚本苹果,pokemmo辅助器脚本下载,AI教程(原本真的有挂);(需添...
透视脚本!红龙poker脚本,... 透视脚本!红龙poker脚本,德普之星透视辅助软件,曝光教程(总是有挂);支持多人共享记分板与复盘,...
透视辅助!wepoekr底牌透... 透视辅助!wepoekr底牌透视,werplan外卦神器,黑科技教程(其实有挂)科技教程也叫必备教程...
透视软件!wpk透视辅助软件,... 透视软件!wpk透视辅助软件,德州机器人代打脚本,微扑克教程(真是存在有挂);大神普及一款德州ai内...
透视最新!aapoker辅助挂... 透视最新!aapoker辅助挂,wepoker透视底牌,新版2025教程(都是是真的有挂)1、许多玩...
透视免费!智星菠萝可以辅助吗,... 1、透视免费!智星菠萝可以辅助吗,德州辅助工具到底怎么样,必胜教程(先前真的有挂)2、进入游戏-大厅...
透视安装!hhpoker辅助器... 透视安装!hhpoker辅助器,pokemmo辅助器脚本下载,扑克教程(原本真的是有挂);原来确实真...