動畫代碼,在res下創建anim文件夾,新建名爲rotate的文件,添加代碼如下
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <rotate android:duration="200"//動畫的持續時間 android:fromDegrees="0"//起始角度,0代表當前位置 android:pivotX="50%"//旋轉中心的x座標,50%代表當前控件的水平中心 android:pivotY="50%"//旋轉中心的y座標,50%代表當前控件的垂直中心 android:repeatMode="reverse"//重複模式,reverse代表與原動畫反向 android:toDegrees="180"//結束角度 /> </set> 生成動畫
Animation rotate = AnimationUtils.loadAnimation(getActivity(), R.anim.rotate);//創建動畫
rotate.setInterpolator(new LinearInterpolator());//設置爲線性旋轉
ImageView imageView = (ImageView) view.findViewById(R.id.iv_secondpage_parent_arrow);
爲imageView設置點擊事件,並添加如下代碼rotate.setFillAfter(!rotate.getFillAfter());//每次都取相反值,使得可以不恢復原位的旋轉imageView.startAnimation(rotate);
rotate.setFillAfter(boolean)此代碼設置爲true則控件會停留在旋轉後的位置,false則旋轉後會恢復原位,顯然
需求是點一次停留在旋轉後的位置,再點一次恢復原位,所以true或者false都不合適,所以需要動態的設置,
即:
rotate.setFillAfter(!rotate.getFillAfter());//每次都取相反值,使得可以不恢復原位的旋轉