PullToRrefresh自定义下拉刷新动画

原文地址:PullToRrefresh自定义下拉刷新动画


参考自 http://blog.csdn.net/wwj_748/article/details/42523611

首先,下载著名的刷新框架https://github.com/chrisbanes/Android-PullToRefresh,其中simple为demo,library和extras作为项目包导入到simple中

一,定义刷新动画的layout

在library下的com.handmark.pulltorefresh.library.internal包中的FlipLoadingLayout和RotateLoadingLayout

FlipLoadingLayout为ios风格的箭头颠倒的刷新动画

RotateLoadingLayout为android风格的图片旋转动画

共同的设置方法是

1,getDefaultDrawableResId()

动画默认图片,可以替换为自己的图片

2,refreshingImpl()

正在刷新时的回调方法,可以设置开始动画

3,resetImpl()

重置

二,正在刷新时为图片居中旋转的效果

1,首先修改library中的pull_to_refresh_header_vertical.xml,去除文字的layout,图片layout水平居中

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <merge xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.   
  4.     <FrameLayout  
  5.         android:id="@+id/fl_inner"  
  6.         android:layout_width="fill_parent"  
  7.         android:layout_height="wrap_content"  
  8.         android:paddingBottom="@dimen/header_footer_top_bottom_padding"  
  9.         android:paddingLeft="@dimen/header_footer_left_right_padding"  
  10.         android:paddingRight="@dimen/header_footer_left_right_padding"  
  11.         android:paddingTop="@dimen/header_footer_top_bottom_padding" >  
  12.   
  13.         <FrameLayout  
  14.             android:layout_width="wrap_content"  
  15.             android:layout_height="wrap_content"  
  16.             android:layout_gravity="center_horizontal" >  
  17.   
  18.             <ImageView  
  19.                 android:id="@+id/pull_to_refresh_image"  
  20.                 android:layout_width="wrap_content"  
  21.                 android:layout_height="wrap_content"  
  22.                 android:layout_gravity="center" />  
  23.   
  24.             <ProgressBar  
  25.                 android:id="@+id/pull_to_refresh_progress"  
  26.                 style="?android:attr/progressBarStyleSmall"  
  27.                 android:layout_width="wrap_content"  
  28.                 android:layout_height="wrap_content"  
  29.                 android:layout_gravity="center"  
  30.                 android:indeterminate="true"  
  31.                 android:visibility="gone" />  
  32.         </FrameLayout>  
  33.   
  34.      <!--    <LinearLayout  
  35.             android:layout_width="wrap_content"  
  36.             android:layout_height="wrap_content"  
  37.             android:layout_gravity="center"  
  38.             android:gravity="center_horizontal"  
  39.             android:orientation="vertical" >  
  40.   
  41.             <TextView  
  42.                 android:id="@+id/pull_to_refresh_text"  
  43.                 android:layout_width="wrap_content"  
  44.                 android:layout_height="wrap_content"  
  45.                 android:singleLine="true"  
  46.                 android:textAppearance="?android:attr/textAppearance"  
  47.                 android:textStyle="bold" />  
  48.   
  49.             <TextView  
  50.                 android:id="@+id/pull_to_refresh_sub_text"  
  51.                 android:layout_width="wrap_content"  
  52.                 android:layout_height="wrap_content"  
  53.                 android:singleLine="true"  
  54.                 android:textAppearance="?android:attr/textAppearanceSmall"  
  55.                 android:visibility="gone" />  
  56.         </LinearLayout> -->  
  57.     </FrameLayout>  
  58.   
  59. </merge>  


2,去除LoadingLayout中的关于textview的代码

3,可以在RotateLoadingLayout中的getDefaultDrawableResId()方法替换成自己的图片

三,设置自定义动画效果

1,首先设置一个简单的小人走的动画效果,在anim文件夹下新建一个xml,需要加载两张图片,控制图片的停留时间

  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <animation-list xmlns:android="http://schemas.android.com/apk/res/android"    
  3.     android:oneshot="false" >    
  4.     
  5.     <item    
  6.         android:drawable="@drawable/app_loading0"    
  7.         android:duration="150"/>    
  8.     <item    
  9.         android:drawable="@drawable/app_loading1"    
  10.         android:duration="150"/>    
  11.     
  12. </animation-list>    

2,新建刷新动画的layout,TweenAnimLoadingLayout,类似于之前的源码中的FlipLoadingLayout和RotateLoadingLayout

主要设置初始化,和那几个关键方法就行

  1. package com.handmark.pulltorefresh.library.internal;  
  2.   
  3.   
  4. import com.handmark.pulltorefresh.library.R;  
  5. import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;  
  6. import com.handmark.pulltorefresh.library.PullToRefreshBase.Orientation;  
  7.   
  8. import android.content.Context;  
  9. import android.content.res.TypedArray;  
  10. import android.graphics.drawable.AnimationDrawable;  
  11. import android.graphics.drawable.Drawable;  
  12. import android.view.View;  
  13.   
  14. /** 
  15.  * @date 2015/1/8 
  16.  * @author wuwenjie 
  17.  * @desc 帧动画加载布局 
  18.  */  
  19. public class TweenAnimLoadingLayout extends LoadingLayout {  
  20.   
  21.     private AnimationDrawable animationDrawable;  
  22.   
  23.     public TweenAnimLoadingLayout(Context context, Mode mode,  
  24.             Orientation scrollDirection, TypedArray attrs) {  
  25.         super(context, mode, scrollDirection, attrs);  
  26.         // 初始化  
  27.         mHeaderImage.setImageResource(R.anim.loading);  
  28.         animationDrawable = (AnimationDrawable) mHeaderImage.getDrawable();  
  29.     }  
  30.     // 默认图片  
  31.     @Override  
  32.     protected int getDefaultDrawableResId() {  
  33.         return R.drawable.app_loading0;  
  34.     }  
  35.       
  36.     @Override  
  37.     protected void onLoadingDrawableSet(Drawable imageDrawable) {  
  38.         // NO-OP  
  39.     }  
  40.       
  41.     @Override  
  42.     protected void onPullImpl(float scaleOfLayout) {  
  43.         // NO-OP  
  44.     }  
  45.     // 下拉以刷新  
  46.     @Override  
  47.     protected void pullToRefreshImpl() {  
  48.         // NO-OP  
  49.     }  
  50.     // 正在刷新时回调  
  51.     @Override  
  52.     protected void refreshingImpl() {  
  53.         // 播放帧动画  
  54.         animationDrawable.start();  
  55.     }  
  56.     // 释放以刷新  
  57.     @Override  
  58.     protected void releaseToRefreshImpl() {  
  59.         // NO-OP  
  60.     }  
  61.     // 重新设置  
  62.     @Override  
  63.     protected void resetImpl() {  
  64.         mHeaderImage.setVisibility(View.VISIBLE);  
  65.         mHeaderImage.clearAnimation();  
  66.     }  
  67.   
  68. }  

3,替换之前的刷新layout为TweenAnimLoadingLayout

找到library项目com.handmark.pulltorefresh.library包下的PullToRefreshListView,发现头脚的layout用的都是LoadingLayout,找到头脚layout的创建方法createLoadingLayout进入,在createLoadingLayout方法中再进入createLoadingLayout,找到最原始的新建动画layout的地方,把默认的RotateLoadingLayout改成TweenAnimLoadingLayout就行了

在PullToRefreshBase类下,变为

  1. //在最原始的地方把新建动画layout换成TweenAnimLoadingLayout  
  2.         LoadingLayout createLoadingLayout(Context context, Mode mode, Orientation scrollDirection, TypedArray attrs) {  
  3.             switch (this) {  
  4.                 case ROTATE:  
  5.                 default:  
  6. //                  return new RotateLoadingLayout(context, mode, scrollDirection, attrs);  
  7.                     return new TweenAnimLoadingLayout(context, mode, scrollDirection, attrs);  
  8.                 case FLIP:  
  9.                     return new FlipLoadingLayout(context, mode, scrollDirection, attrs);  
  10.             }  
  11.         }  

4,去除LoadingLayout中的关于textview的代码



代码下载 http://download.csdn.net/detail/superjunjin/8589827



以上内容为作者原有内容,我在实际应用中对TweenAnimLoadingLayout的写法有些不同。因为根据打印Log可以发现,松手刷新的时候,refreshingImpl和resetImpl会被反复调用多次,(显示一次refreshingImpl,然后紧跟着一次resetImpl,随后是正式的调用refreshingImpl方法刷新,随后又连续调用两次restImpl。推测原因是下拉时,拉的太多了,所以松手的时候,先回弹到争取的下拉位置,然后再开始正式刷新。所以个人感觉将控制动画的代码写到restImpl中不太合适,因为随即在refreshImpl中又要开始调用),以下是我在自己的项目中的改写:

public class MyLoadingLayout extends LoadingLayout{

	private AnimationDrawable animationDrawable;  
	private int[] resId = new int[]{R.drawable.dropdown_anim_00,
			R.drawable.dropdown_anim_01,
			R.drawable.dropdown_anim_02,
			R.drawable.dropdown_anim_03,
			R.drawable.dropdown_anim_04,
			R.drawable.dropdown_anim_05,
			R.drawable.dropdown_anim_06,
			R.drawable.dropdown_anim_07,
			R.drawable.dropdown_anim_08,
			R.drawable.dropdown_anim_09,
			R.drawable.dropdown_anim_10,};
	public MyLoadingLayout(Context context, Mode mode, Orientation scrollDirection, TypedArray attrs) {
		super(context, mode, scrollDirection, attrs);
		mHeaderImage.setImageDrawable(getResources().getDrawable(R.drawable.loading_anim));
		animationDrawable = (AnimationDrawable) mHeaderImage.getDrawable();
	}

	@Override
	protected int getDefaultDrawableResId() {
		return R.drawable.dropdown_anim_00;
	}

	@Override
	protected void onLoadingDrawableSet(Drawable imageDrawable) {
		Log.d("TAG", "onLoadingDrawableSet");
	}

	@Override
	protected void onPullImpl(float scaleOfLayout) {
		if(scaleOfLayout<=1){
			int idx = (int) Math.ceil(scaleOfLayout*10);
			Drawable d = getResources().getDrawable(resId[idx]);
			d.setLevel(100);
			ScaleDrawable sd = new ScaleDrawable(d, Gravity.CENTER, (10-idx)/10.0f,(10-idx)/10.0f);
			mHeaderImage.setImageDrawable(sd);
		}else{
			mHeaderImage.setImageDrawable(getResources().getDrawable(resId[10]));
		}
	}

	@Override
	protected void pullToRefreshImpl() {
		Log.d("TAG", "pullToRefreshImpl");
	}

	@Override
	protected void refreshingImpl() {
		Log.d("TAG", "refreshingImpl");
		if(animationDrawable.isRunning()){return;}
		animationDrawable.start();  
	}

	@Override
	protected void releaseToRefreshImpl() {
		Log.d("TAG", "releaseToRefreshImpl");
	}

	@Override
	protected void resetImpl() {
		Log.d("TAG", "resetImpl");
	}

}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
首先吐槽一下现在流行的刷新库,一个字大,包涵个人很多集成到项目中不需要的类,也很难找到很满意的效果,所以自己自己动手丰衣足食,撸一个。1.概述对所有基础控件(包括,嵌套滑动例如RecyclerView、NestedScrollView,普通的TextView、ListView、ScrollerView、LinearLayout等)提供下拉刷新、上拉加载的支持,处理了横向滑动冲突(例如:顶部banner的情况) ,且实现无痕过度。gradle (改用bintray-release,2017-7-8 16:00上传,以下暂时不会生效)compile 'com.yan:pullrefreshlayout:1.1.2'2.说明支持所有基础控件 loading 出现效果默认(STATE_FOLLOW、STATE_PLACEHOLDER_FOLLOW、STATE_CENTER、STATE_PLACEHOLDER_CENTER、STATE_FOLLOW_CENTER、STATE_CENTER_FOLLOW)  //-控件设置-     refreshLayout.autoRefresh();// 自动刷新     refreshLayout.setOverScrollDampingRatio(0.2f);//  值越大overscroll越短 default 0.2     refreshLayout.setAdjustTwinkDuring(3);// 值越大overscroll越慢 default 3     refreshLayout.setScrollInterpolator(interpolator);// 设置scroller的插值器     refreshLayout.setLoadMoreEnable(true);// 上拉加载是否可用 default false     refreshLayout.setDuringAdjustValue(10f);// 动画执行时间调节,越大动画执行越慢 default 10f     // 刷新或加载完成后回复动画执行时间,为-1时,根据setDuringAdjustValue()方法实现 default 300     refreshLayout.setRefreshBackTime(300);     refreshLayout.setDragDampingRatio(0.6f);// 阻尼系数 default 0.6     refreshLayout.setPullFlowHeight(400);// 拖拽最大范围,为-1时拖拽范围不受限制 default -1     refreshLayout.setRefreshEnable(false);// 下拉刷新是否可用 default false     refreshLayout.setPullTwinkEnable(true);// 回弹是否可用 default true      refreshLayout.setAutoLoadingEnable(true);// 自动加载是否可用 default false          // headerView和footerView需实现PullRefreshLayout.OnPullListener接口调整状态     refreshLayout.setHeaderView(headerView);// 设置headerView     refreshLayout.setFooterView(footerView);// 设置footerView          /**     * 设置header或者footer的的出现方式,默认7种方式     * STATE_FOLLOW, STATE_PLACEHOLDER_FOLLOW, STATE_PLACEHOLDER_CENTER     * , STATE_CENTER, STATE_CENTER_FOLLOW, STATE_FOLLOW_CENTER     * ,STATE_PLACEHOLDER     */     refreshLayout.setRefreshShowGravity(RefreshShowHelper.STATE_CENTER,RefreshShowHelper.STATE_CENTER);     refreshLayout.setHeaderShowGravity(RefreshShowHelper.STATE_CENTER)// header出现动画     refreshLayout.setFooterShowGravity(RefreshShowHelper.STATE_CENTER)// footer出现动画     // PullRefreshLayout.OnPullListener         public interface OnPullListener {             // 刷新或加载过程中位置相刷新或加载触发位置的百分比,时刻调用             void onPullChange(float percent);             void onPullReset();// 数据重置调用             void onPullHoldTrigger();// 拖拽超过触发位置调用             void onPullHoldUnTrigger();// 拖拽回到触发位置之前调用             void onPullHolding(); // 正在刷新             void onPullFinish();// 刷新完成         }3.demo用到的库loading 动画 AVLoadingIndicatorView(https://github.com/81813780/AVLoadingIndicatorView)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值