Android 利用 xml 文件实现 ImageView 的加载转圈动画

    在进行网络数据请求的时候,在请求数据返回之前,为了减少用户等待的焦虑感,经常需要有转圈加载等待的过渡效果。具体的方法如下:(点击重新定位,左边的圈圈会转动)

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);
            }
        });

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值