在进行网络数据请求的时候,在请求数据返回之前,为了减少用户等待的焦虑感,经常需要有转圈加载等待的过渡效果。具体的方法如下:(点击重新定位,左边的圈圈会转动)
1、在 res/anim/ 文件夹下新建anim_circle_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="359" <!--0~359防止卡顿-->
android:pivotX="50%" <!--设置旋转中心点为控件中心-->
android:pivotY="50%"
android:duration="1000" <!--控制每一圈持续的时间-->
android:repeatCount="-1" /> <!--设置不断旋转-->
</set>
2、给目标 ImageView(mIvRotate)加上动画效果
ImageView iv_refresh_location = (ImageView) findViewById(R.id.iv_refresh_location);
LinearLayout ll_refresh_location = (LinearLayout) findViewById(R.id.ll_refresh_location);
Animation rotateAnimation = AnimationUtils.loadAnimation(this, R.anim.anim_circle_rotate);
LinearInterpolator interpolator = new LinearInterpolator();
rotateAnimation.setInterpolator(interpolator);
3、开始动画与结束动画
对于Animation /RotationAnimation,调用startAnimation()开始动画,调用 clearAnimation()
清除动画;
ll_refresh_location.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
iv_refresh_location.startAnimation(refreshAnimation);
tv_refresh_location.setText("正在定位");
//延时1000ms后自动停止
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
iv_refresh_location.clearAnimation();
tv_refresh_location.setText("重新定位");
tv_location.setText(LocationUtil.getAddress());
}
}, 1000);
}
});