1. 程式人生 > >Android PullToRrefresh 自定義下拉刷新動畫 (listview、scrollview等)

Android PullToRrefresh 自定義下拉刷新動畫 (listview、scrollview等)

appear ram nal ima cas 創建 protect tom inter

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;  
  
/** 
 * @date 2015/1/8 
 * @author wuwenjie 
 * @desc 幀動畫加載布局 
 */  
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) {  
        // NO-OP  
    }  
      
    @Override  
    protected void onPullImpl(float scaleOfLayout) {  
        // NO-OP  
    }  
    // 下拉以刷新  
    @Override  
    protected void pullToRefreshImpl() {  
        // NO-OP  
    }  
    // 正在刷新時回調  
    @Override  
    protected void refreshingImpl() {  
        // 播放幀動畫  
        animationDrawable.start();  
    }  
    // 釋放以刷新  
    @Override  
    protected void releaseToRefreshImpl() {  
        // NO-OP  
    }  
    // 重新設置  
    @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類下,變為

//在最原始的地方把新建動畫layout換成TweenAnimLoadingLayout  
        LoadingLayout createLoadingLayout(Context context, Mode mode, Orientation scrollDirection, TypedArray attrs) {  
            switch (this) {  
                case ROTATE:  
                default:  
//                  return new RotateLoadingLayout(context, mode, scrollDirection, attrs);  
                    return new TweenAnimLoadingLayout(context, mode, scrollDirection, attrs);  
                case FLIP:  
                    return new FlipLoadingLayout(context, mode, scrollDirection, attrs);  
            }  
        }  

4,去除LoadingLayout中的關於textview的代碼

代碼下載 http://download.csdn.net/detail/superjunjin/8589827

Android PullToRrefresh 自定義下拉刷新動畫 (listview、scrollview等)