├── app ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── values │ │ │ │ ├── strings.xml │ │ │ │ ├── dimens.xml │ │ │ │ ├── colors.xml │ │ │ │ └── styles.xml │ │ │ ├── mipmap-hdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-mdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xxhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xxxhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── drawable │ │ │ │ ├── icon_pull_2_refresh_ball.png │ │ │ │ ├── icon_pull_2_refresh_light.png │ │ │ │ ├── icon_pull_2_refresh_star.png │ │ │ │ ├── icon_pull_2_refresh_flying.png │ │ │ │ ├── icon_pull_2_refresh_cloud_big.png │ │ │ │ ├── icon_pull_2_refresh_img_planet.png │ │ │ │ ├── icon_pull_2_refresh_cloud_small.png │ │ │ │ └── icon_pull_2_refresh_flying_away.png │ │ │ ├── values-w820dp │ │ │ │ └── dimens.xml │ │ │ └── layout │ │ │ │ ├── activity_main.xml │ │ │ │ └── donkey_content.xml │ │ ├── java │ │ │ └── com │ │ │ │ └── example │ │ │ │ └── sangenan │ │ │ │ └── myapplication │ │ │ │ ├── App.java │ │ │ │ ├── PointDonkey.java │ │ │ │ ├── BaseActivity.java │ │ │ │ ├── PointEvaluator.java │ │ │ │ ├── MainActivity.java │ │ │ │ ├── drawPlanet.java │ │ │ │ ├── drawBall.java │ │ │ │ ├── drawDog.java │ │ │ │ └── DonkeyRefreshListView.java │ │ └── AndroidManifest.xml │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── example │ │ │ └── sangenan │ │ │ └── myapplication │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── com │ │ └── example │ │ └── sangenan │ │ └── myapplication │ │ └── ExampleInstrumentedTest.java ├── proguard-rules.pro └── build.gradle ├── settings.gradle ├── README.md ├── .idea ├── copyright │ └── profiles_settings.xml ├── modules.xml ├── runConfigurations.xml ├── compiler.xml ├── gradle.xml └── misc.xml ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── gradle.properties ├── gradlew.bat └── gradlew /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #Android下拉刷新--仿赶集网app下拉刷新 2 | 效果: 3 | 4 | ![markdown](http://ojlv24d9n.bkt.clouddn.com/Kapture.gif) 5 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | My Application 3 | 4 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sangenan/DonkeyRefresh/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sangenan/DonkeyRefresh/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sangenan/DonkeyRefresh/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sangenan/DonkeyRefresh/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sangenan/DonkeyRefresh/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sangenan/DonkeyRefresh/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/icon_pull_2_refresh_ball.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sangenan/DonkeyRefresh/HEAD/app/src/main/res/drawable/icon_pull_2_refresh_ball.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/icon_pull_2_refresh_light.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sangenan/DonkeyRefresh/HEAD/app/src/main/res/drawable/icon_pull_2_refresh_light.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/icon_pull_2_refresh_star.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sangenan/DonkeyRefresh/HEAD/app/src/main/res/drawable/icon_pull_2_refresh_star.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/icon_pull_2_refresh_flying.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sangenan/DonkeyRefresh/HEAD/app/src/main/res/drawable/icon_pull_2_refresh_flying.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/icon_pull_2_refresh_cloud_big.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sangenan/DonkeyRefresh/HEAD/app/src/main/res/drawable/icon_pull_2_refresh_cloud_big.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/icon_pull_2_refresh_img_planet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sangenan/DonkeyRefresh/HEAD/app/src/main/res/drawable/icon_pull_2_refresh_img_planet.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/icon_pull_2_refresh_cloud_small.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sangenan/DonkeyRefresh/HEAD/app/src/main/res/drawable/icon_pull_2_refresh_cloud_small.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/icon_pull_2_refresh_flying_away.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sangenan/DonkeyRefresh/HEAD/app/src/main/res/drawable/icon_pull_2_refresh_flying_away.png -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Dec 28 10:00:20 PST 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | #1f1b35 7 | #ffffff 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/sangenan/myapplication/App.java: -------------------------------------------------------------------------------- 1 | package com.example.sangenan.myapplication; 2 | 3 | import android.app.Application; 4 | import android.support.v7.app.AppCompatDelegate; 5 | 6 | /** 7 | * Created by kuwakuzukusunoki on 2017/1/1. 8 | */ 9 | 10 | public class App extends Application { 11 | @Override 12 | public void onCreate() { 13 | super.onCreate(); 14 | AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); 15 | 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /app/src/test/java/com/example/sangenan/myapplication/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.example.sangenan.myapplication; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() throws Exception { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/sangenan/myapplication/PointDonkey.java: -------------------------------------------------------------------------------- 1 | package com.example.sangenan.myapplication; 2 | 3 | /** 4 | * Created by kuwakuzukusunoki on 2017/1/6. 5 | */ 6 | 7 | public class PointDonkey { 8 | private float dx; 9 | 10 | private float dy; 11 | 12 | public PointDonkey(float dx, float dy) { 13 | this.dx = dx; 14 | this.dy = dy; 15 | } 16 | 17 | public float getDx() { 18 | return dx; 19 | } 20 | 21 | public void setDx(float dx) { 22 | this.dx = dx; 23 | } 24 | 25 | public float getDy() { 26 | return dy; 27 | } 28 | 29 | public void setDy(float dy) { 30 | this.dy = dy; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/sangenan/myapplication/BaseActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.sangenan.myapplication; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.util.Log; 7 | 8 | /** 9 | * Created by kuwakuzukusunoki on 2016/12/29. 10 | */ 11 | 12 | public abstract class BaseActivity extends AppCompatActivity { 13 | public abstract int getID(); 14 | 15 | @Override 16 | protected void onCreate(@Nullable Bundle savedInstanceState) { 17 | super.onCreate(savedInstanceState); 18 | setContentView(getID()); 19 | Log.d("tag","Oncreate_after_Base"); 20 | 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/kuwakuzukusunoki/Library/Android/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 18 | 19 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 18 | 19 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | org.gradle.jvmargs=-Xmx1536m 13 | 14 | # When configured, Gradle will run in incubating parallel mode. 15 | # This option should only be used with decoupled projects. More details, visit 16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 17 | # org.gradle.parallel=true 18 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/sangenan/myapplication/PointEvaluator.java: -------------------------------------------------------------------------------- 1 | package com.example.sangenan.myapplication; 2 | 3 | import android.animation.TypeEvaluator; 4 | 5 | /** 6 | * Created by kuwakuzukusunoki on 2017/1/6. 7 | */ 8 | 9 | public class PointEvaluator implements TypeEvaluator { 10 | 11 | @Override 12 | public Object evaluate(float fraction, Object startValue, Object endValue) { 13 | PointDonkey startPoint = (PointDonkey) startValue; 14 | PointDonkey endPoint = (PointDonkey) endValue; 15 | float x = startPoint.getDx() + fraction * (endPoint.getDx() - startPoint.getDx()); 16 | float y = startPoint.getDy() + fraction * (endPoint.getDy() - startPoint.getDy()); 17 | PointDonkey point = new PointDonkey(x , y); 18 | return point; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/example/sangenan/myapplication/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.example.sangenan.myapplication; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.example.sangenan.myapplication", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/res/layout/donkey_content.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 10 | 14 | 18 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 24 5 | buildToolsVersion "25.0.1" 6 | defaultConfig { 7 | applicationId "com.example.sangenan.myapplication" 8 | minSdkVersion 19 9 | targetSdkVersion 24 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(include: ['*.jar'], dir: 'libs') 24 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 25 | exclude group: 'com.android.support', module: 'support-annotations' 26 | }) 27 | compile 'com.android.support:appcompat-v7:24.2.1' 28 | testCompile 'junit:junit:4.12' 29 | compile 'com.jakewharton:butterknife:7.0.1' 30 | compile 'in.srain.cube:ultra-ptr:1.0.11' 31 | 32 | } 33 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/sangenan/myapplication/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.sangenan.myapplication; 2 | 3 | import android.os.Bundle; 4 | import android.os.Handler; 5 | import android.util.Log; 6 | import android.widget.ArrayAdapter; 7 | 8 | import java.util.ArrayList; 9 | import java.util.Arrays; 10 | import java.util.List; 11 | 12 | public class MainActivity extends BaseActivity implements DonkeyRefreshListView.OnDonkeyRefreshListener { 13 | private DonkeyRefreshListView mListView; 14 | private List mDatas; 15 | private ArrayAdapter mAdapter; 16 | 17 | private final static int REFRESH_COMPLETE = 0; 18 | 19 | private Handler mHandler = new Handler(){ 20 | public void handleMessage(android.os.Message msg) { 21 | switch (msg.what) { 22 | case REFRESH_COMPLETE: 23 | mListView.setOnRefreshComplete(); 24 | mAdapter.notifyDataSetChanged(); 25 | mListView.setSelection(0); 26 | break; 27 | 28 | default: 29 | break; 30 | } 31 | }; 32 | }; @Override 33 | public int getID() { 34 | return R.layout.activity_main; 35 | } 36 | 37 | @Override 38 | protected void onCreate(Bundle savedInstanceState) { 39 | super.onCreate(savedInstanceState); 40 | Log.d("tag", "Oncreate_after_Main"); 41 | 42 | mListView = (DonkeyRefreshListView) findViewById(R.id.lv); 43 | String[] data = new String[]{"a","b","c","d", 44 | "e","f","g","h","i", 45 | "j","k","l","m","n","o","p","q","r","s"}; 46 | mDatas = new ArrayList(Arrays.asList(data)); 47 | mAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,mDatas); 48 | mListView.setAdapter(mAdapter); 49 | mListView.setOnDonkeyRefreshListener(this); 50 | } 51 | @Override 52 | public void onRefresh() { 53 | new Thread(new Runnable() { 54 | 55 | @Override 56 | public void run() { 57 | try { 58 | Thread.sleep(1000); 59 | mDatas.add(0, "new data"); 60 | mHandler.sendEmptyMessage(REFRESH_COMPLETE); 61 | } catch (InterruptedException e) { 62 | // TODO Auto-generated catch block 63 | e.printStackTrace(); 64 | } 65 | } 66 | }).start(); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/sangenan/myapplication/drawPlanet.java: -------------------------------------------------------------------------------- 1 | package com.example.sangenan.myapplication; 2 | 3 | import android.animation.ValueAnimator; 4 | import android.content.Context; 5 | import android.graphics.Bitmap; 6 | import android.graphics.BitmapFactory; 7 | import android.graphics.Canvas; 8 | import android.graphics.Matrix; 9 | import android.util.AttributeSet; 10 | import android.view.View; 11 | import android.view.animation.LinearInterpolator; 12 | 13 | /** 14 | * Created by kuwakuzukusunoki on 2017/1/11. 15 | */ 16 | 17 | public class drawPlanet extends View { 18 | 19 | private int locationX; 20 | private int locationY; 21 | private Bitmap flyingPlanet; 22 | private float upDateY; 23 | 24 | public drawPlanet(Context context) { 25 | super(context); 26 | initView(); 27 | } 28 | 29 | public drawPlanet(Context context, AttributeSet attrs) { 30 | super(context, attrs); 31 | initView(); 32 | 33 | } 34 | 35 | private void initView() { 36 | flyingPlanet = BitmapFactory.decodeResource(getResources(), R.drawable.icon_pull_2_refresh_img_planet); 37 | upDateY = 0; 38 | 39 | } 40 | 41 | @Override 42 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { 43 | super.onSizeChanged(w, h, oldw, oldh); 44 | 45 | locationX = w / 2; 46 | locationY = h; 47 | } 48 | 49 | @Override 50 | protected void onDraw(Canvas canvas) { 51 | super.onDraw(canvas); 52 | Matrix matrixPlanet = new Matrix(); 53 | matrixPlanet.setScale(0.4f, 0.4f); 54 | matrixPlanet.postTranslate(locationX / 2 * 3, locationY /4); 55 | matrixPlanet.postTranslate(0, upDateY); 56 | canvas.drawBitmap(flyingPlanet,matrixPlanet,null); 57 | 58 | } 59 | public void startTranslatePlanet(int duration){ 60 | ValueAnimator valueAnimator = new ValueAnimator(); 61 | valueAnimator.setFloatValues(-50.0f, 50.0f); 62 | valueAnimator.setDuration(duration); 63 | valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 64 | @Override 65 | public void onAnimationUpdate(ValueAnimator animation) { 66 | upDateY = (float) animation.getAnimatedValue(); 67 | invalidate(); 68 | } 69 | }); 70 | valueAnimator.setRepeatCount(ValueAnimator.INFINITE); 71 | valueAnimator.setRepeatMode(ValueAnimator.REVERSE); 72 | valueAnimator.setInterpolator(new LinearInterpolator()); 73 | valueAnimator.start(); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/sangenan/myapplication/drawBall.java: -------------------------------------------------------------------------------- 1 | package com.example.sangenan.myapplication; 2 | 3 | import android.animation.ValueAnimator; 4 | import android.content.Context; 5 | import android.graphics.Bitmap; 6 | import android.graphics.BitmapFactory; 7 | import android.graphics.Canvas; 8 | import android.graphics.Matrix; 9 | import android.graphics.RectF; 10 | import android.util.AttributeSet; 11 | import android.util.Log; 12 | import android.view.View; 13 | import android.view.animation.LinearInterpolator; 14 | 15 | /** 16 | * Created by kuwakuzukusunoki on 2017/1/8. 17 | */ 18 | 19 | public class drawBall extends View { 20 | 21 | private Bitmap flyingBall; 22 | private float flyingBall_Height; 23 | private float flyingBall_Width; 24 | private int locationX; 25 | private int locationY; 26 | private Bitmap cloudBig; 27 | private Bitmap cloudSmall; 28 | private float degreeBall; 29 | private float upDateY; 30 | private RectF rectfCloudBig; 31 | private RectF rectfCloudSmall; 32 | private float sizeCloudBig; 33 | private float sizeCloudSmall; 34 | 35 | public drawBall(Context context) { 36 | super(context); 37 | initView(); 38 | } 39 | 40 | public drawBall(Context context, AttributeSet attrs) { 41 | super(context, attrs); 42 | initView(); 43 | 44 | } 45 | 46 | private void initView() { 47 | flyingBall = BitmapFactory.decodeResource(getResources(), R.drawable.icon_pull_2_refresh_ball); 48 | cloudBig = BitmapFactory.decodeResource(getResources(), R.drawable.icon_pull_2_refresh_cloud_big); 49 | cloudSmall = BitmapFactory.decodeResource(getResources(), R.drawable.icon_pull_2_refresh_cloud_small); 50 | 51 | flyingBall_Height = flyingBall.getHeight() * 0.2f; 52 | flyingBall_Width = flyingBall.getWidth() * 0.2f; 53 | sizeCloudBig = (float) cloudBig.getWidth() / cloudBig.getHeight(); 54 | sizeCloudSmall = (float) cloudSmall.getWidth() / cloudSmall.getHeight(); 55 | 56 | } 57 | @Override 58 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { 59 | super.onSizeChanged(w, h, oldw, oldh); 60 | locationX = w / 2; 61 | locationY = h; 62 | 63 | rectfCloudBig = new RectF(locationX +flyingBall_Width /4,locationY -flyingBall_Width /4 - 60 64 | ,locationX + flyingBall_Width /4 + 60 * sizeCloudBig , locationY -flyingBall_Width / 4); 65 | 66 | rectfCloudSmall = new RectF(locationX - flyingBall_Width / 4 - 50 * sizeCloudSmall , locationY -flyingBall_Width / 4 - 50 , 67 | locationX - flyingBall_Width / 4,locationY - flyingBall_Width / 4); 68 | } 69 | @Override 70 | protected void onDraw(Canvas canvas) { 71 | super.onDraw(canvas); 72 | Matrix matrixBall = new Matrix(); 73 | matrixBall.setScale(0.2f, 0.2f); 74 | if ((locationY + upDateY) > (locationY - flyingBall_Height / 2)) { 75 | matrixBall.postTranslate(locationX - flyingBall_Width / 2, locationY + upDateY); 76 | matrixBall.postRotate(degreeBall, locationX, (locationY +upDateY + flyingBall_Height /2) ); 77 | Log.d("tag","upDateY"+upDateY); 78 | } 79 | else { 80 | matrixBall.postTranslate(locationX - flyingBall_Width / 2, locationY - flyingBall_Height / 2); 81 | matrixBall.postRotate(degreeBall, locationX, locationY); 82 | 83 | } 84 | 85 | canvas.drawBitmap(flyingBall, matrixBall, null); 86 | canvas.drawBitmap(cloudBig , null , rectfCloudBig , null); 87 | canvas.drawBitmap(cloudSmall , null , rectfCloudSmall ,null); 88 | 89 | } 90 | 91 | public void startBallAnim(long duration) { 92 | ValueAnimator valueAnimator = new ValueAnimator(); 93 | valueAnimator.setFloatValues(0.0f, 360.0f); 94 | valueAnimator.setDuration(duration); 95 | valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 96 | @Override 97 | public void onAnimationUpdate(ValueAnimator animation) { 98 | degreeBall = (float) animation.getAnimatedValue(); 99 | invalidate(); 100 | } 101 | }); 102 | valueAnimator.setRepeatCount(ValueAnimator.INFINITE); 103 | valueAnimator.setInterpolator(new LinearInterpolator()); 104 | valueAnimator.start(); 105 | } 106 | public void UpBall(float offsetY){ 107 | if (upDateY!=offsetY) { 108 | upDateY = offsetY; 109 | invalidate(); 110 | } 111 | } 112 | 113 | public void accelerateBall(long duration) { 114 | clearAnimation(); 115 | startBallAnim(duration); 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/sangenan/myapplication/drawDog.java: -------------------------------------------------------------------------------- 1 | package com.example.sangenan.myapplication; 2 | 3 | import android.animation.Animator; 4 | import android.animation.AnimatorListenerAdapter; 5 | import android.animation.ValueAnimator; 6 | import android.content.Context; 7 | import android.graphics.Bitmap; 8 | import android.graphics.BitmapFactory; 9 | import android.graphics.Canvas; 10 | import android.graphics.Matrix; 11 | import android.graphics.Paint; 12 | import android.graphics.RectF; 13 | import android.util.AttributeSet; 14 | import android.util.Log; 15 | import android.view.View; 16 | import android.view.animation.AccelerateInterpolator; 17 | 18 | /** 19 | * Created by kuwakuzukusunoki on 2017/1/3. 20 | */ 21 | 22 | public class drawDog extends View{ 23 | private int locationX; 24 | private int locationY; 25 | private float degree; 26 | private float flyingDonkey_Height; 27 | private float flyingDonkey_Width; 28 | private float flyingBall_Width; 29 | private float refreshLight_Width; 30 | private float refreshLight_Height; 31 | private Bitmap flyingDonkey; 32 | private Bitmap refreshLight; 33 | private float upDateY; 34 | 35 | private PointDonkey pointDonkey; 36 | private Paint paintBackgroud; 37 | 38 | int density; 39 | private Bitmap flyingBall; 40 | 41 | 42 | public drawDog(Context context) { 43 | super(context); 44 | init(); 45 | } 46 | 47 | public drawDog(Context context, AttributeSet attrs) { 48 | super(context, attrs); 49 | init(); 50 | } 51 | 52 | private void init() { 53 | density = (int) getResources().getDisplayMetrics().density; 54 | Log.d("tag","density"+density); 55 | 56 | 57 | flyingDonkey = BitmapFactory.decodeResource(getResources(), R.drawable.icon_pull_2_refresh_flying); 58 | refreshLight = BitmapFactory.decodeResource(getResources(), R.drawable.icon_pull_2_refresh_light); 59 | flyingBall = BitmapFactory.decodeResource(getResources(), R.drawable.icon_pull_2_refresh_ball); 60 | 61 | flyingDonkey_Height = flyingDonkey.getHeight() * 0.3f; 62 | flyingDonkey_Width = flyingDonkey.getWidth() * 0.3f; 63 | refreshLight_Width = refreshLight.getWidth() * 0.15f; 64 | refreshLight_Height = refreshLight.getHeight() * 0.15f; 65 | 66 | flyingBall_Width = flyingBall.getWidth() * 0.2f; 67 | 68 | paintBackgroud = new Paint(); 69 | paintBackgroud.setColor(getResources().getColor(R.color.color_Refresh_Backgroud)); 70 | 71 | degree = 0; 72 | } 73 | 74 | 75 | @Override 76 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { 77 | super.onSizeChanged(w, h, oldw, oldh); 78 | locationX = w / 2; 79 | locationY = h; 80 | pointDonkey = new PointDonkey(locationX - flyingDonkey_Width / 2, locationY - flyingDonkey_Height); 81 | 82 | refreshLight_Height = (w / refreshLight_Width) * refreshLight_Height; 83 | 84 | } 85 | 86 | @Override 87 | protected void onDraw(Canvas canvas) { 88 | super.onDraw(canvas); 89 | 90 | RectF rectfLight = new RectF( 0 , locationY - refreshLight_Height / 2 , 2 * locationX , locationY + refreshLight_Height /2 ); 91 | canvas.drawBitmap(refreshLight , null , rectfLight , null); 92 | 93 | Matrix matrix = new Matrix(); 94 | matrix.setScale(0.3f, 0.3f); 95 | matrix.postTranslate(pointDonkey.getDx(), pointDonkey.getDy()); 96 | matrix.postRotate(degree, locationX, locationY + flyingBall_Width / 2); 97 | matrix.postTranslate(0 , upDateY); 98 | canvas.drawBitmap(flyingDonkey, matrix, null); 99 | } 100 | 101 | public void setDonkeyRotate(int rotate , float offsetY) { 102 | if (Math.abs(rotate - degree) >= 0.5) { 103 | degree = rotate; 104 | upDateY = offsetY; 105 | invalidate(); 106 | } 107 | } 108 | public void changeDonkey(){ 109 | flyingDonkey = BitmapFactory.decodeResource(getResources(), R.drawable.icon_pull_2_refresh_flying_away); 110 | invalidate(); 111 | } 112 | 113 | 114 | public void launchDonkey() { 115 | flyingDonkey = BitmapFactory.decodeResource(getResources(), R.drawable.icon_pull_2_refresh_flying); 116 | PointDonkey startPoint = new PointDonkey(locationX - flyingDonkey_Width / 2, locationY - flyingDonkey_Height); 117 | PointDonkey endPoint = new PointDonkey(2 * locationX, 0); 118 | ValueAnimator valueAnimator = ValueAnimator.ofObject(new PointEvaluator(), startPoint, endPoint); 119 | valueAnimator.setDuration(1000); 120 | valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 121 | @Override 122 | public void onAnimationUpdate(ValueAnimator animation) { 123 | pointDonkey = (PointDonkey) animation.getAnimatedValue(); 124 | invalidate(); 125 | } 126 | }); 127 | valueAnimator.addListener(new AnimatorListenerAdapter() { 128 | @Override 129 | public void onAnimationEnd(Animator animation) { 130 | reStart(); 131 | } 132 | }); 133 | valueAnimator.setInterpolator(new AccelerateInterpolator()); 134 | valueAnimator.start(); 135 | } 136 | 137 | public void reStart() { 138 | clearAnimation(); 139 | pointDonkey.setDx(locationX - flyingDonkey_Width / 2); 140 | pointDonkey.setDy(locationY - flyingDonkey_Height); 141 | degree = 180; 142 | } 143 | 144 | } 145 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/sangenan/myapplication/DonkeyRefreshListView.java: -------------------------------------------------------------------------------- 1 | package com.example.sangenan.myapplication; 2 | 3 | import android.content.Context; 4 | import android.util.AttributeSet; 5 | import android.view.LayoutInflater; 6 | import android.view.MotionEvent; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | import android.widget.AbsListView; 10 | import android.widget.ListView; 11 | import android.widget.RelativeLayout; 12 | 13 | /** 14 | * Created by Hankkin on 16/4/10. 15 | */ 16 | public class DonkeyRefreshListView extends ListView implements AbsListView.OnScrollListener{ 17 | private static final int DONE = 0; //刷新完毕状态 18 | private static final int PULL_TO_REFRESH = 1; //下拉刷新状态 19 | private static final int RELEASE_TO_REFRESH = 2; //释放状态 20 | private static final int REFRESHING = 3; //正在刷新状态 21 | private static final int RATIO = 2; 22 | private RelativeLayout headView; //下拉刷新头 23 | private int headViewHeight; //头高度 24 | private float startY; //开始Y坐标 25 | private float offsetY; //Y轴偏移量 26 | private OnDonkeyRefreshListener mOnRefreshListener; //刷新接口 27 | private int state; //状态值 28 | private int mFirstVisibleItem; //第一项可见item索引 29 | private boolean isRecord; //是否记录 30 | private boolean isEnd; //是否结束 31 | private boolean isRefreable; //是否刷新 32 | 33 | private drawDog donkey; 34 | private drawBall ball; 35 | private drawPlanet planet; 36 | private static final int startRotate =300; 37 | private static final int endRotate = 360; 38 | public DonkeyRefreshListView(Context context) { 39 | super(context); 40 | init(context); 41 | } 42 | 43 | public DonkeyRefreshListView(Context context, AttributeSet attrs) { 44 | super(context, attrs); 45 | init(context); 46 | } 47 | 48 | public DonkeyRefreshListView(Context context, AttributeSet attrs, int defStyleAttr) { 49 | super(context, attrs, defStyleAttr); 50 | init(context); 51 | } 52 | 53 | public interface OnDonkeyRefreshListener{ 54 | void onRefresh(); 55 | } 56 | 57 | /** 58 | * 回调接口,想实现下拉刷新的listview实现此接口 59 | * @param onRefreshListener 60 | */ 61 | public void setOnDonkeyRefreshListener(OnDonkeyRefreshListener onRefreshListener){ 62 | mOnRefreshListener = onRefreshListener; 63 | isRefreable = true; 64 | } 65 | 66 | /** 67 | * 刷新完毕,从主线程发送过来,并且改变headerView的状态和文字动画信息 68 | */ 69 | public void setOnRefreshComplete(){ 70 | //一定要将isEnd设置为true,以便于下次的下拉刷新 71 | isEnd = true; 72 | state = DONE; 73 | 74 | changeHeaderByState(state); 75 | } 76 | 77 | private void init(Context context) { 78 | //关闭view的OverScroll 79 | setOverScrollMode(OVER_SCROLL_NEVER); 80 | setOnScrollListener(this); 81 | //加载头布局 82 | headView = (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.donkey_content,this,false); 83 | //测量头布局 84 | measureView(headView); 85 | //给ListView添加头布局 86 | addHeaderView(headView); 87 | //设置头文件隐藏在ListView的第一项 88 | headViewHeight = headView.getMeasuredHeight(); 89 | headView.setPadding(0, -headViewHeight, 0, 0); 90 | 91 | donkey = (drawDog) headView.findViewById(R.id.donkey); 92 | ball = (drawBall) headView.findViewById(R.id.ball); 93 | planet = (drawPlanet) headView.findViewById(R.id.planet); 94 | state = DONE; 95 | isEnd = true; 96 | isRefreable = false; 97 | 98 | 99 | } 100 | 101 | @Override 102 | public void onScrollStateChanged(AbsListView absListView, int i) { 103 | } 104 | @Override 105 | public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) { 106 | mFirstVisibleItem = firstVisibleItem; 107 | } 108 | 109 | @Override 110 | public boolean onTouchEvent(MotionEvent ev) { 111 | if (isEnd) {//如果现在时结束的状态,即刷新完毕了,可以再次刷新了,在onRefreshComplete中设置 112 | if (isRefreable) {//如果现在是可刷新状态 在setOnMeiTuanListener中设置为true 113 | switch (ev.getAction()){ 114 | //用户按下 115 | case MotionEvent.ACTION_DOWN: 116 | //如果当前是在listview顶部并且没有记录y坐标 117 | if (mFirstVisibleItem == 0 && !isRecord) { 118 | //将isRecord置为true,说明现在已记录y坐标 119 | isRecord = true; 120 | //将当前y坐标赋值给startY起始y坐标 121 | startY = ev.getY(); 122 | 123 | ball.startBallAnim(1000); 124 | planet.startTranslatePlanet(1000); 125 | } 126 | break; 127 | //用户滑动 128 | case MotionEvent.ACTION_MOVE: 129 | //再次得到y坐标,用来和startY相减来计算offsetY位移值 130 | float tempY = ev.getY(); 131 | //再起判断一下是否为listview顶部并且没有记录y坐标 132 | if (mFirstVisibleItem == 0 && !isRecord) { 133 | isRecord = true; 134 | startY = tempY; 135 | } 136 | //如果当前状态不是正在刷新的状态,并且已经记录了y坐标 137 | if (state!=REFRESHING && isRecord ) { 138 | //计算y的偏移量 139 | offsetY = tempY - startY; 140 | //计算当前滑动的高度 141 | float currentHeight = (-headViewHeight+offsetY/RATIO); 142 | //用当前滑动的高度和头部headerView的总高度进行比 计算出当前滑动的百分比 0到1 143 | float currentProgress = 1+currentHeight/headViewHeight; 144 | //如果当前百分比大于1了,将其设置为1,目的是让第一个状态的椭圆不再继续变大 145 | if (currentProgress>=1) { 146 | currentProgress = 1; 147 | donkey.changeDonkey(); 148 | } 149 | 150 | int margeRotate = (int) ((endRotate -startRotate) * currentProgress +startRotate); 151 | donkey.setDonkeyRotate(margeRotate , -offsetY /5 ); 152 | ball.UpBall(-offsetY / 5); 153 | 154 | //如果当前的状态是放开刷新,并且已经记录y坐标 155 | if (state == RELEASE_TO_REFRESH && isRecord) { 156 | 157 | setSelection(0); 158 | //如果当前滑动的距离小于headerView的总高度 159 | if (-headViewHeight+offsetY/RATIO<0) { 160 | //将状态置为下拉刷新状态 161 | state = PULL_TO_REFRESH; 162 | //根据状态改变headerView,主要是更新动画和文字等信息 163 | changeHeaderByState(state); 164 | //如果当前y的位移值小于0,即为headerView隐藏了 165 | }else if (offsetY<=0) { 166 | //将状态变为done 167 | state = DONE; 168 | //根据状态改变headerView,主要是更新动画和文字等信息 169 | changeHeaderByState(state); 170 | } 171 | } 172 | //如果当前状态为下拉刷新并且已经记录y坐标 173 | if (state == PULL_TO_REFRESH && isRecord) { 174 | setSelection(0); 175 | //如果下拉距离大于等于headerView的总高度 176 | if (-headViewHeight+offsetY/RATIO>=0) { 177 | //将状态变为放开刷新 178 | state = RELEASE_TO_REFRESH; 179 | //根据状态改变headerView,主要是更新动画和文字等信息 180 | changeHeaderByState(state); 181 | //如果当前y的位移值小于0,即为headerView隐藏了 182 | }else if (offsetY<=0) { 183 | //将状态变为done 184 | state = DONE; 185 | //根据状态改变headerView,主要是更新动画和文字等信息 186 | changeHeaderByState(state); 187 | } 188 | } 189 | //如果当前状态为done并且已经记录y坐标 190 | if (state == DONE && isRecord) { 191 | //如果位移值大于0 192 | if (offsetY>=0) { 193 | //将状态改为下拉刷新状态 194 | state = PULL_TO_REFRESH; 195 | changeHeaderByState(state); 196 | } 197 | } 198 | //如果为下拉刷新状态 199 | if (state == PULL_TO_REFRESH) { 200 | //则改变headerView的padding来实现下拉的效果 201 | headView.setPadding(0,(int)(-headViewHeight+offsetY/RATIO) ,0,0); 202 | } 203 | //如果为放开刷新状态 204 | if (state == RELEASE_TO_REFRESH) { 205 | //改变headerView的padding值 206 | headView.setPadding(0, 0, 0, 0); 207 | } 208 | } 209 | break; 210 | //当用户手指抬起时 211 | case MotionEvent.ACTION_UP: 212 | //如果当前状态为下拉刷新状态 213 | if (state == PULL_TO_REFRESH) { 214 | //平滑的隐藏headerView 215 | this.smoothScrollBy((int)(-headViewHeight+offsetY/RATIO)+headViewHeight, 500); 216 | //根据状态改变headerView 217 | changeHeaderByState(state); 218 | } 219 | //如果当前状态为放开刷新 220 | if (state == RELEASE_TO_REFRESH) { 221 | //平滑的滑到正好显示headerView 222 | // this.smoothScrollBy((int)(-headViewHeight+offsetY/RATIO), 500); 223 | //将当前状态设置为正在刷新 224 | state = REFRESHING; 225 | //回调接口的onRefresh方法 226 | mOnRefreshListener.onRefresh(); 227 | //根据状态改变headerView 228 | changeHeaderByState(state); 229 | } 230 | //这一套手势执行完,一定别忘了将记录y坐标的isRecord改为false,以便于下一次手势的执行 231 | isRecord = false; 232 | break; 233 | } 234 | 235 | } 236 | } 237 | return super.onTouchEvent(ev); 238 | } 239 | 240 | /** 241 | * 根据状态改变headerView的动画和文字显示 242 | * @param state 243 | */ 244 | private void changeHeaderByState(int state){ 245 | switch (state) { 246 | case DONE://如果的隐藏的状态 247 | //设置headerView的padding为隐藏 248 | headView.setPadding(0, -headViewHeight, 0, 0); 249 | break; 250 | case RELEASE_TO_REFRESH://当前状态为放开刷新 251 | break; 252 | case PULL_TO_REFRESH://当前状态为下拉刷新 253 | break; 254 | case REFRESHING://当前状态为正在刷新 255 | donkey.launchDonkey(); 256 | ball.accelerateBall(500); 257 | break; 258 | default: 259 | break; 260 | } 261 | } 262 | 263 | /** 264 | * 测量View 265 | * @param child 266 | */ 267 | private void measureView(View child) { 268 | ViewGroup.LayoutParams p = child.getLayoutParams(); 269 | if (p == null) { 270 | p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 271 | ViewGroup.LayoutParams.WRAP_CONTENT); 272 | } 273 | int childWidthSpec = ViewGroup.getChildMeasureSpec(0, 0 + 0, p.width); 274 | int lpHeight = p.height; 275 | int childHeightSpec; 276 | if (lpHeight > 0) { 277 | childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, 278 | MeasureSpec.EXACTLY); 279 | } else { 280 | childHeightSpec = MeasureSpec.makeMeasureSpec(0, 281 | MeasureSpec.UNSPECIFIED); 282 | } 283 | child.measure(childWidthSpec, childHeightSpec); 284 | } 285 | 286 | } 287 | --------------------------------------------------------------------------------