当前位置: 移动技术网 > 移动技术>移动开发>Android > Android实现下拉刷新的视图和图标的旋转

Android实现下拉刷新的视图和图标的旋转

2019年07月24日  | 移动技术网移动技术  | 我要评论

一、下拉才出现的视图

pull_to_refresh_header.xml

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pull_to_refresh_header"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#f3f3f3">
 <relativelayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:paddingtop="23dp">
  <linearlayout 
       android:id="@+id/pull_to_refresh_view"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:orientation="vertical"
   android:layout_centerhorizontal="true">
   <textview
android:id="@+id/pull_to_refresh_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
android:text="@string/pull_to_refresh_text"
    android:textcolor="#777777"
    android:textsize="16sp"/>
   <textview
android:id="@+id/pull_to_refresh_update_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
android:text="@string/pull_to_refresh_update_text"
    android:textcolor="#999999"
    android:textsize="14sp"/>
  </linearlayout>
  <progressbar
android:id="@+id/pull_to_refresh_progress"
   android:layout_width="30dp"
   android:layout_height="30dp"
android:layout_toleftof="@id/pull_to_refresh_view"
   android:layout_marginright="22dp"
   android:layout_margintop="5dp"
   android:indeterminate="true"
android:indeterminatedrawable="@drawable/ic_loading_refresh"
   android:visibility="gone"/>
  <imageview
   android:id="@+id/pull_to_refresh_image"
   android:layout_width="32dp"
   android:layout_height="32dp"
android:layout_toleftof="@id/pull_to_refresh_view"
   android:layout_marginright="20dp"
   android:layout_margintop="5dp"
   android:src="@drawable/ic_refresh_down"
   android:scaletype="centerinside"
android:contentdescription="@string/app_name"/>
 </relativelayout>
 <view
  android:layout_width="match_parent"
  android:layout_height="15dp"/>
</linearlayout>

ic_loading_refresh.xml

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
 android:drawable="@drawable/ic_loading"
 android:fromdegrees="0"
 android:todegrees="360"
 android:pivotx="50%"
 android:pivoty="50%" />

1、progressbar的indeterminate属性,代表进程的时间是否不确定。

2、黄色底的是android studio的提示。第一个提示的是,当linearlayout中的text拓展得足够长时,会与imageview重叠,实际效果是把imageview给覆盖了。第二个是,建议用tostartof代替toleftof,用marginend代替marginright等。原因和影响还没完全搞懂。

二、图标旋转的动画

private imageview mpull_to_refresh_image;
private rotateanimation mflipanimation;

...

  mflipanimation = new rotateanimation(0, -180, rotateanimation.relative_to_self, 0.5f,
    rotateanimation.relative_to_self, 0.5f);
  mflipanimation.setinterpolator(new linearinterpolator());
  mflipanimation.setduration(250);
  mflipanimation.setfillafter(true);

...
mpull_to_refresh_image.startanimation(mflipanimation);

1、构造方法:

public rotateanimation(float fromdegrees, float todegrees, int pivotxtype, float pivotxvalue,
   int pivotytype, float pivotyvalue)

(1)todegrees - fromdegrees < 0 就会是逆时针旋转,反之是顺时针。这是我取不同值测试出来的。水平线右边是0°,或者360°,左边是180°,或者是-180°。

(2)pivottype是枢轴类型,也就是旋转中心。relative_to_self代表相对这个view本身。0.5f代表这个view一半大小的位置。

2、setinterpolator作用是设置速度器。linearinterpolator是匀速加速器,也就是匀速播放动画。

3、setduration作用是设置动画时长,以毫秒为单位。

4、setfillafter作用是,当参数为true时,动画播放完后,view会维持在最终的状态。而默认值是false,也就是动画播放完后,view会恢复原来的状态。

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持移动技术网!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网