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是两个按钮的点击事件,用于

相关内容

热门资讯

透视插件!闲逸辅助软件下载(辅... 透视插件!闲逸辅助软件下载(辅助)决战卡五星辅助源码(一直真的有挂)-哔哩哔哩1、决战卡五星辅助源码...
透视app!hhpoker怎么... 透视app!hhpoker怎么破解(透视)HH平台挂(好像真的有透视)-哔哩哔哩进入游戏-大厅左侧-...
透视脚本!新畅游互娱科技(辅助... 透视脚本!新畅游互娱科技(辅助)新道游房间(切实一直都是有透视)-哔哩哔哩1、起透看视 新畅游互娱科...
透视工具!混沌休闲有没有挂(辅... 透视工具!混沌休闲有没有挂(辅助)胡乐麻将辅助(果然存在有挂)-哔哩哔哩1.混沌休闲有没有挂 选牌创...
透视教程!wepoker辅助透... 透视教程!wepoker辅助透视(透视)we-poker软件(确实真的是有辅助器)-哔哩哔哩1、在w...
透视工具!wepoker手机版... 透视工具!wepoker手机版辅助(透视)wepoker黑侠辅助器(确实真的有挂)-哔哩哔哩1、该软...
透视神器!花花生活圈怎么开挂(... 透视神器!花花生活圈怎么开挂(辅助)掌中乐辅助(一直有挂)-哔哩哔哩1、用户打开应用后不用登录就可以...
透视软件!wepoker透视有... 透视软件!wepoker透视有吗(透视)poker world辅助(果然一直都是有挂)-哔哩哔哩一、...
透视插件!wepoker透视辅... 透视插件!wepoker透视辅助下载(透视)wepoker辅助分析器(都是是有辅助器)-哔哩哔哩we...
透视app!wepoker黑侠... 透视app!wepoker黑侠辅助器正版下载(透视)wepokerplus透视脚本免费(都是一直总是...