前半部分参考自http://blog.csdn.net/superjunjin/article/details/45022595,转载过来记录学习情况,后半部分记录下
PullToRefreshScrollView 自定义下拉刷新动画,只需改一处。
以下部分转载自http://blog.csdn.net/superjunjin/article/details/45022595
一,定义刷新动画的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水平居中
- <?xml version="1.0" encoding="utf-8"?>
- <merge xmlns:android="http://schemas.android.com/apk/res/android" >
-
- <FrameLayout
- android:id="@+id/fl_inner"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:paddingBottom="@dimen/header_footer_top_bottom_padding"
- android:paddingLeft="@dimen/header_footer_left_right_padding"
- android:paddingRight="@dimen/header_footer_left_right_padding"
- android:paddingTop="@dimen/header_footer_top_bottom_padding" >
-
- <FrameLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal" >
-
- <ImageView
- android:id="@+id/pull_to_refresh_image"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center" />
-
- <ProgressBar
- android:id="@+id/pull_to_refresh_progress"
- style="?android:attr/progressBarStyleSmall"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:indeterminate="true"
- android:visibility="gone" />
- </FrameLayout>
-
- <!-- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:gravity="center_horizontal"
- android:orientation="vertical" >
-
- <TextView
- android:id="@+id/pull_to_refresh_text"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:singleLine="true"
- android:textAppearance="?android:attr/textAppearance"
- android:textStyle="bold" />
-
- <TextView
- android:id="@+id/pull_to_refresh_sub_text"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:singleLine="true"
- android:textAppearance="?android:attr/textAppearanceSmall"
- android:visibility="gone" />
- </LinearLayout> -->
- </FrameLayout>
-
- </merge>
2,去除LoadingLayout中的关于textview的代码
3,可以在RotateLoadingLayout中的getDefaultDrawableResId()方法替换成自己的图片
三,设置自定义动画效果
1,首先设置一个简单的小人走的动画效果,在anim文件夹下新建一个xml,需要加载两张图片,控制图片的停留时间
- <?xml version="1.0" encoding="utf-8"?>
- <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
- android:oneshot="false" >
-
- <item
- android:drawable="@drawable/app_loading0"
- android:duration="150"/>
- <item
- android:drawable="@drawable/app_loading1"
- android:duration="150"/>
-
- </animation-list>
2,新建刷新动画的layout,TweenAnimLoadingLayout,类似于之前的源码中的FlipLoadingLayout和RotateLoadingLayout
主要设置初始化,和那几个关键方法就行
- package com.handmark.pulltorefresh.library.internal;
-
-
- import com.handmark.pulltorefresh.library.R;
- import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
- import com.handmark.pulltorefresh.library.PullToRefreshBase.Orientation;
-
- import android.content.Context;
- import android.content.res.TypedArray;
- import android.graphics.drawable.AnimationDrawable;
- import android.graphics.drawable.Drawable;
- import android.view.View;
-
-
-
-
-
-
- public class TweenAnimLoadingLayout extends LoadingLayout {
-
- private AnimationDrawable animationDrawable;
-
- public TweenAnimLoadingLayout(Context context, Mode mode,
- Orientation scrollDirection, TypedArray attrs) {
- super(context, mode, scrollDirection, attrs);
-
- mHeaderImage.setImageResource(R.anim.loading);
- animationDrawable = (AnimationDrawable) mHeaderImage.getDrawable();
- }
-
- @Override
- protected int getDefaultDrawableResId() {
- return R.drawable.app_loading0;
- }
-
- @Override
- protected void onLoadingDrawableSet(Drawable imageDrawable) {
-
- }
-
- @Override
- protected void onPullImpl(float scaleOfLayout) {
-
- }
-
- @Override
- protected void pullToRefreshImpl() {
-
- }
-
- @Override
- protected void refreshingImpl() {
-
- animationDrawable.start();
- }
-
- @Override
- protected void releaseToRefreshImpl() {
-
- }
-
- @Override
- protected void resetImpl() {
- mHeaderImage.setVisibility(View.VISIBLE);
- mHeaderImage.clearAnimation();
- }
-
- }
3,替换之前的刷新layout为TweenAnimLoadingLayout
找到library项目com.handmark.pulltorefresh.library包下的PullToRefreshListView,发现头脚的layout用的都是LoadingLayout,找到头脚layout的创建方法createLoadingLayout进入,在createLoadingLayout方法中再进入createLoadingLayout,找到最原始的新建动画layout的地方,把默认的RotateLoadingLayout改成TweenAnimLoadingLayout就行了
在PullToRefreshBase类下,变为
-
- LoadingLayout createLoadingLayout(Context context, Mode mode, Orientation scrollDirection, TypedArray attrs) {
- switch (this) {
- case ROTATE:
- default:
-
- return new TweenAnimLoadingLayout(context, mode, scrollDirection, attrs);
- case FLIP:
- return new FlipLoadingLayout(context, mode, scrollDirection, attrs);
- }
- }
4,去除LoadingLayout中的关于textview的代码
下面记录关于scrollview的自定义动画,其实之前的部分已经为我们铺垫好了,我们只需要将
ptr:ptrAnimationStyle="flip"
改为
ptr:ptrAnimationStyle="<span style="color:#ff0000;"><strong>rotate</strong></span>"
即可
此时再运行程序,下拉刷新时会发现,下拉刷新由箭头变成了我们之前自定义的动画,为什么会这样?为什么不是旋转图标呢???
具体原因我们可以查看源码:
打开com.handmark.pulltorefresh.library里面的PullToRefreshScrollView.java
我们可以看到PullToRefreshScrollView是继承自PullToRefreshBase<ScrollView>
public class PullToRefreshScrollView extends PullToRefreshBase<ScrollView> {
而我们刚刚自定义PullToRefreshListView 的下拉刷新动画时,就是在PullToRefreshBase<T> 中进行修改的,而且我们进入
PullToRefreshBase<T>类中,找到
public abstract class PullToRefreshBase<T extends View> extends LinearLayout implements IPullToRefresh<T> {
...
<span style="white-space:pre"> </span>private LoadingLayout <span style="color:#ff0000;">mHeaderLayout</span>;
private LoadingLayout <span style="color:#ff0000;">mFooterLayout</span>;
...
<span style="font-family: Arial;">}</span>
然后我们去看它被引用的位置
<span style="font-family: Arial;">public abstract class PullToRefreshBase<T extends View> extends LinearLayout implements IPullToRefresh<T> {</span><span style="white-space:pre"></span><pre name="code" class="java" style="line-height: 26px;">
...
<span style="white-space:pre"> </span>// We need to create now layouts now
mHeaderLayout = <span style="color:#ff0000;">createLoadingLayout</span>(context, Mode.PULL_FROM_START, a);
mFooterLayout = <span style="color:#ff0000;">createLoadingLayout</span>(context, Mode.PULL_FROM_END, a);
...
}
我们会发现,这就是我们之前自定义下拉刷新的时候看到过的,我们一直进入
createLoadingLayout方法,找到最原始的新建动画layout的地方,会发现,其实就是之前改过的地方!!!
-
- LoadingLayout createLoadingLayout(Context context, Mode mode, Orientation scrollDirection, TypedArray attrs) {
- switch (this) {
- case ROTATE:
- default:
-
- return new TweenAnimLoadingLayout(context, mode, scrollDirection, attrs);
- case FLIP:
- return new FlipLoadingLayout(context, mode, scrollDirection, attrs);
- }
- }
看到了吧,这就是为什么我们设置PullToRefreshScrollView的ptr:ptrAnimationStyle属性为rotate,下拉刷新动画会变成我们之前定义的动画的原因
然后PullToRrefesh这个开源项目中的其他控件的下拉刷新动画也是这样的原理啦。