package com.handmark.pulltorefresh.library.internal;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.R;
import com.nineoldandroids.animation.FloatEvaluator;
import com.nineoldandroids.animation.ObjectAnimator;
import com.nineoldandroids.animation.TypeEvaluator;
import com.nineoldandroids.animation.ValueAnimator;
import com.nineoldandroids.view.ViewHelper;

/**
 * Created by dangjie1 on 15/2/2.
 */
public class NewYearLoadingLayout extends LoadingLayout {
    private int mHeaderImageHeight = 0;
    private ValueAnimator valueAnimator;

    public NewYearLoadingLayout(Context context, PullToRefreshBase.Mode mode, PullToRefreshBase.Orientation scrollDirection, TypedArray attrs) {
        super(context, mode, scrollDirection, attrs);
        mHeaderImageHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 75, context.getResources().getDisplayMetrics());
        ViewHelper.setTranslationY(mHeaderImage, mHeaderImageHeight / 3 * 2);

    }

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

    @Override
    protected void onLoadingDrawableSet(Drawable imageDrawable) {

    }


    @Override
    protected void onPullImpl(float scaleOfLayout) {
        if (scaleOfLayout > 0.9f) {
            if (valueAnimator == null) {
                valueAnimator = ValueAnimator.ofFloat(mHeaderImageHeight / 4 * 3, mHeaderImageHeight / 5).setDuration(250);
                valueAnimator.start();
            } else {
                float currentValue = (Float) valueAnimator.getAnimatedValue();
                if (ViewHelper.getTranslationY(mHeaderImage) != currentValue) {
                    ViewHelper.setTranslationY(mHeaderImage, currentValue);
                }
            }
        }

    }

    @Override
    protected void pullToRefreshImpl() {
        setVisibility(View.VISIBLE);
    }

    @Override
    protected void refreshingImpl() {
        showNewYearAnimator();
    }

    @Override
    protected void releaseToRefreshImpl() {
        showNewYearAnimator();
    }

    private void showNewYearAnimator() {
        ViewHelper.setTranslationY(mHeaderImage, mHeaderImageHeight / 5);
        ((AnimationDrawable) mHeaderImage.getDrawable()).start();
    }

    @Override
    protected void resetImpl() {
        setVisibility(View.INVISIBLE);
        ((AnimationDrawable) mHeaderImage.getDrawable()).stop();
        if (mHeaderImageHeight > 0) {
            ViewHelper.setTranslationY(mHeaderImage, mHeaderImageHeight / 3 * 2);
        }
        valueAnimator = null;
    }
}

@@ -122,39 +122,8 @@
 ay;%0A
-import android.graphics.Color;%0A
 impo
@@ -274,81 +274,8 @@
 iew;
-%0Aimport android.view.ViewTreeObserver;%0Aimport android.widget.FrameLayout;
 %0A%0Aim
@@ -382,166 +382,8 @@
 .R;%0A
-import com.nineoldandroids.animation.FloatEvaluator;%0Aimport com.nineoldandroids.animation.ObjectAnimator;%0Aimport com.nineoldandroids.animation.TypeEvaluator;%0A
 impo
@@ -656,16 +656,91 @@
 imator;%0A
+    private boolean hasShowAnimator = false;%0A    private float startY,endY;
 %0A    pub
@@ -1070,16 +1070,101 @@
 ics());%0A
+        startY =  mHeaderImageHeight / 3 * 2;%0A        endY =  mHeaderImageHeight /6;%0A
         
@@ -1208,38 +1208,17 @@
 ge, 
-mHeaderImageHeight / 3 * 2
+startY
 );%0A
-%0A
     
@@ -1624,58 +1624,20 @@
 oat(
-mHeaderImageHeight / 4 * 3, mHeaderImageHeight / 5
+startY, endY
 ).se
@@ -1687,24 +1687,63 @@
 or.start();%0A
+                showNewYearAnimator();%0A
             
@@ -2095,44 +2095,8 @@
 ) %7B%0A
-        setVisibility(View.VISIBLE);
 %0A   
@@ -2143,32 +2143,88 @@
 reshingImpl() %7B%0A
+        ViewHelper.setTranslationY(mHeaderImage, endY);%0A
         showNewY
@@ -2307,38 +2307,8 @@
 ) %7B%0A
-        showNewYearAnimator();
 %0A   
@@ -2356,82 +2356,8 @@
 ) %7B%0A
-        ViewHelper.setTranslationY(mHeaderImage, mHeaderImageHeight / 5);%0A
     
@@ -2414,24 +2414,56 @@
 )).start();%0A
+        hasShowAnimator = true;%0A
     %7D%0A%0A    @
@@ -2496,32 +2496,142 @@
 d resetImpl() %7B%0A
+        //%E5%B7%B2%E7%BB%8F%E6%98%BE%E7%A4%BA%E8%BF%87%E5%8A%A8%E7%94%BB%E7%9A%84 %E5%9C%A8%E7%A7%BB%E9%99%A4%E5%89%8D %E8%AE%BE%E7%BD%AE%E4%B8%BA%E4%B8%8D%E6%98%BE%E7%A4%BA %E9%98%B2%E6%AD%A2%E7%95%8C%E9%9D%A2%E5%87%BA%E7%8E%B0%E9%97%AA%E7%83%81%0A        if(hasShowAnimator)%7B%0A            hasShowAnimator = false;%0A    
         setVisib
@@ -2653,16 +2653,27 @@
 SIBLE);%0A
+        %7D%0A%0A
         
@@ -2733,50 +2733,8 @@
 ();%0A
-        if (mHeaderImageHeight %3E 0) %7B%0A    
     
@@ -2782,46 +2782,16 @@
 ge, 
-mHeaderImageHeight / 3 * 2);%0A        %7D
+startY);
 %0A   

修正 修改显示 移动位置 而导致的 界面闪烁问题