Commits

sbgapps committed 8c5164f Merge
  • Participants
  • Parent commits 217ff2f, e0cc62e

Comments (0)

Files changed (6)

HoloGraphLibrary/src/com/echo/holographlibrary/HoloGraphAnimate.java

+package com.echo.holographlibrary;
+
+import android.animation.Animator;
+import android.view.animation.Interpolator;
+
+/**
+ * Created by DouglasW on 6/8/2014.
+ */
+public interface HoloGraphAnimate {
+
+    int getDuration();
+    void setDuration(int duration);
+
+    Interpolator getInterpolator();
+    void setInterpolator(Interpolator interpolator);
+
+    void animateToGoalValues();
+    void setAnimationListener(Animator.AnimatorListener animationListener);
+}

HoloGraphLibrary/src/com/echo/holographlibrary/PieGraph.java

 
 package com.echo.holographlibrary;
 
+import android.animation.Animator;
+import android.animation.ValueAnimator;
+import android.annotation.TargetApi;
 import android.content.Context;
 import android.content.res.TypedArray;
 import android.graphics.Canvas;
 import android.graphics.Point;
 import android.graphics.RectF;
 import android.graphics.Region;
+import android.os.Build;
 import android.util.AttributeSet;
+import android.util.Log;
 import android.view.MotionEvent;
 import android.view.View;
+import android.view.animation.Animation;
+import android.view.animation.Interpolator;
+import android.view.animation.LinearInterpolator;
 
 import java.util.ArrayList;
 
-public class PieGraph extends View {
+public class PieGraph extends View implements  HoloGraphAnimate {
 
     private int mPadding;
     private int mInnerCircleRatio;
 
         float currentAngle = 270;
         float currentSweep = 0;
-        int totalValue = 0;
+        float totalValue = 0;
 
         midX = getWidth() / 2;
         midY = getHeight() / 2;
         return true;
     }
 
+    /**
+     * sets padding
+     * @param padding
+     */
     public void setPadding(int padding) {
         mPadding = padding;
         postInvalidate();
         postInvalidate();
     }
 
+    int mDuration = 300;
+    Interpolator mInterpolator;
+    Animator.AnimatorListener mAnimationListener;
+    @Override
+    public int getDuration() {
+        return mDuration;
+    }
+
+    @Override
+    public void setDuration(int duration) {mDuration = duration;}
+
+    @Override
+    public Interpolator getInterpolator() {
+        return mInterpolator;
+    }
+
+    @Override
+    public void setInterpolator(Interpolator interpolator) {mInterpolator = interpolator;}
+
+    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
+    @Override
+    public void animateToGoalValues() {
+        if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR1){
+            Log.e("HoloGraphLibrary compatibility error", "Animation not supported on api level 12 and below. Returning without animating.");
+            return;
+        }
+        for (PieSlice s : mSlices)
+            s.setOldValue(s.getValue());
+        ValueAnimator va = ValueAnimator.ofFloat(0,1);
+        va.setDuration(getDuration());
+        if (mInterpolator == null) mInterpolator = new LinearInterpolator();
+        va.setInterpolator(mInterpolator);
+        if (mAnimationListener != null) va.addListener(mAnimationListener);
+        va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
+            @Override
+            public void onAnimationUpdate(ValueAnimator animation) {
+                float f = Math.max(animation.getAnimatedFraction(), 0.01f);//avoid blank frames; never multiply values by 0
+               // Log.d("f", String.valueOf(f));
+                for (PieSlice s : mSlices) {
+                    float x = s.getGoalValue() - s.getOldValue();
+                    s.setValue(s.getOldValue() + (x * f));
+                }
+                postInvalidate();
+            }});
+            va.start();
+
+        }
+
+
+
+    @Override
+    public void setAnimationListener(Animator.AnimatorListener animationListener) { mAnimationListener = animationListener;}
+
     public interface OnSliceClickedListener {
         public abstract void onClick(int index);
     }

HoloGraphLibrary/src/com/echo/holographlibrary/PieSlice.java

     private int mColor = 0xFF33B5E5;
     private int mSelectedColor = -1;
     private float mValue;
+    private float mOldValue;
+    private float mGoalValue;
     private String mTitle;
 
     public String getTitle() {
         mValue = value;
     }
 
+    public float getOldValue() {
+        return mOldValue;
+    }
+
+    public void setOldValue(float oldValue) { mOldValue = oldValue; }
+
+    public float getGoalValue() {
+        return mGoalValue;
+    }
+
+    public void setGoalValue(float goalValue) { mGoalValue = goalValue; }
+
     public Path getPath() {
         return mPath;
     }

HoloGraphLibrarySample/res/layout/fragment_piegraph.xml

             android:layout_weight="1"/>
 
         <SeekBar
-            android:layout_width="0dip"
-            android:layout_height="wrap_content"
-            android:progress="0"
-            android:max="10"
-            android:id="@+id/seekBarPadding"
-            android:layout_weight="1"/>
+        android:layout_width="0dip"
+        android:layout_height="wrap_content"
+        android:progress="0"
+        android:max="10"
+        android:id="@+id/seekBarPadding"
+        android:layout_weight="1"/>
+
+
     </LinearLayout>
+    <Button
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:text="Animate to random values"
+        android:id="@+id/animatePieButton"
+        android:layout_weight="0"/>
 
 </LinearLayout>

HoloGraphLibrarySample/src/com/echo/holographlibrarysample/PieFragment.java

 
 package com.echo.holographlibrarysample;
 
+import android.animation.Animator;
+import android.animation.ValueAnimator;
+import android.annotation.TargetApi;
 import android.content.res.Resources;
+import android.os.Build;
+import android.view.animation.*;
 import android.os.Bundle;
 import android.support.v4.app.Fragment;
+import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
+import android.widget.Button;
 import android.widget.SeekBar;
 import android.widget.Toast;
 
         final View v = inflater.inflate(R.layout.fragment_piegraph, container, false);
         final Resources resources = getResources();
         final PieGraph pg = (PieGraph) v.findViewById(R.id.piegraph);
+        final Button animateButton = (Button) v.findViewById(R.id.animatePieButton);
         PieSlice slice = new PieSlice();
         slice.setColor(resources.getColor(R.color.green_light));
         slice.setSelectedColor(resources.getColor(R.color.transparent_orange));
         slice.setValue(2);
+        slice.setTitle("first");
         pg.addSlice(slice);
         slice = new PieSlice();
         slice.setColor(resources.getColor(R.color.orange));
             }
         });
 
+        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1)
+        animateButton.setOnClickListener(new View.OnClickListener() {
+            @Override
+            public void onClick(View v) {
+                for (PieSlice s : pg.getSlices())
+                    s.setGoalValue((float)Math.random() * 10);
+                pg.setDuration(1000);//default if unspecified is 300 ms
+                pg.setInterpolator(new AccelerateDecelerateInterpolator());//default if unspecified is linear
+                //pg.setAnimationListener(getAnimationListener());
+                pg.animateToGoalValues();
+
+            }
+        });
         return v;
     }
+
+    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
+    public Animator.AnimatorListener getAnimationListener(){
+        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1)
+        return new Animator.AnimatorListener() {
+            @Override
+            public void onAnimationStart(Animator animation) {
+
+            }
+
+            @Override
+            public void onAnimationEnd(Animator animation) {//you might want to call slice.setvalue(slice.getGoalValue)
+            }
+
+            @Override
+            public void onAnimationCancel(Animator animation) {
+
+            }
+
+            @Override
+            public void onAnimationRepeat(Animator animation) {
+
+            }
+        };
+        else return null;
+
+    }
+
 }
   }
 
   dependencies {
-    classpath 'com.android.tools.build:gradle:0.9.+'
+    classpath 'com.android.tools.build:gradle:0.11.+'
   }
 }
 
 
 ext {
     compileSdkVersion = 19
-    buildToolsVersion = "19.0.3"
+    buildToolsVersion = "19.1.0"
 }
 
 apply plugin: 'android-reporting'