ui给了一个进度条小圆球,在任务没执行完毕的时候,需要这个图片旋转起来,这个时候就用到了Animation
使用步骤:
1、首先在res文件夹下创建一个名字为anim的文件夹,名字不要写错
2、在anim里面创建一个xlm文件:img_animation.xml,这个名字随便写都可以,注意不要大写,里面的代码如下:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<rotate
android:duration="5000"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="-1"
android:repeatMode="restart"
android:toDegrees="1110" />
</set>
上面参数具体含义是:
duration:时间
fromDegrees=“0”: 从几度开始转
toDegrees=“1110” : 旋转多少度
pivotX="50%:旋转中心距离view的左顶点为50%距离, 注意一点:pivotX=50才会原地转上下左右摆
pivotY="50%: 距离view的上边缘为50%距离,注意一点:pivotX=50才会原地转上下左右摆
repeatCount=“-1”:重复次数,-1为一直重复
repeatMode=“restart”:重复模式,restart从头开始重复
布局文件代码没变,依旧是:放一个控件就行了
使用:
在页面布局的xml里引入上面这个样式文件就可以了
</
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff00ff"
>
<ImageView
android:id="@+id/iv_checkCharge"
android:layout_width="@dimen/gxd_dimen_40"
android:layout_height="@dimen/gxd_dimen_32"
android:layout_marginRight="15dp"
android:layout_gravity="right"
android:src="@drawable/ic_loading01">
</ImageView>
</RelativeLayout>
任意一个普通的控件都可以旋转
如果要求旋转的速度,修改toDegrees=""这个参数,越高越快,越低越慢
Java代码:
public class BatteryCalibrationFragment extends Fragment {
private static String TAG = "BatteryCalibrationFragment";
ImageView iv_checkCharge;
ImageView iv_checkLengthen;
ImageView imgLoadOk;
ImageView ivBack;
@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_battery_calibration, container, false);
iv_checkCharge = rootView.findViewById(R.id.iv_checkCharge);
iv_checkLengthen = rootView.findViewById(R.id.iv_checkLengthen);
imgLoadOk = rootView.findViewById(R.id.iv_checkSuccess);
//animation
Animation animation = AnimationUtils.loadAnimation(getActivity(), R.anim.img_animation);
// set animation
LinearInterpolator lin = new LinearInterpolator();
animation.setInterpolator(lin);
iv_checkCharge.setAnimation(animation);
iv_checkLengthen.setAnimation(animation);
}
}
需要动画停止的场景来关闭动画
mImageView.clearAnimation();
参考文章:https://www.xp.cn/b.php/96118.html