Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added fading out when limit hit

  • Loading branch information...
commit 1e6127917792affa8e0161322e057b317d2ec146 1 parent ff2f634
@matto1990 matto1990 authored
Showing with 66 additions and 15 deletions.
  1. +66 −15 viewflow/src/org/taptwo/android/widget/CircleFlowIndicator.java
View
81 viewflow/src/org/taptwo/android/widget/CircleFlowIndicator.java
@@ -22,9 +22,12 @@
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
+import android.os.AsyncTask;
import android.util.AttributeSet;
-import android.util.Log;
import android.view.View;
+import android.view.animation.Animation;
+import android.view.animation.AnimationUtils;
+import android.view.animation.Animation.AnimationListener;
/**
* A FlowIndicator which draws circles (one for each view). The current view
@@ -41,10 +44,12 @@
* radius: Define the circle radius (default to 4.0)
* </ul>
*/
-public class CircleFlowIndicator extends View implements FlowIndicator {
+public class CircleFlowIndicator extends View implements FlowIndicator, AnimationListener {
private static final int STYLE_STROKE = 0;
private static final int STYLE_FILL = 1;
+ private static final int FADE_OUT = 2;
+
private float radius = 4;
private final Paint mPaintStroke = new Paint(Paint.ANTI_ALIAS_FLAG);
private final Paint mPaintActive = new Paint(Paint.ANTI_ALIAS_FLAG);
@@ -52,6 +57,8 @@
private int currentScroll = 0;
private int flowWidth = 0;
private FadeTimer timer;
+ public AnimationListener animationListener = this;
+ private Animation animation;
/**
* Default constructor
@@ -112,8 +119,6 @@ public CircleFlowIndicator(Context context, AttributeSet attrs) {
// Retrieve the radius
radius = a.getDimension(R.styleable.CircleFlowIndicator_radius, 4.0f);
initColors(activeColor, inactiveColor, activeType, inactiveType);
-
-
}
private void initColors(int activeColor, int inactiveColor, int activeType,
@@ -187,9 +192,7 @@ public void onSwitched(View view, int position) {
*/
@Override
public void setViewFlow(ViewFlow view) {
- timer = new FadeTimer();
- timer.start();
-
+ resetTimer();
viewFlow = view;
flowWidth = viewFlow.getWidth();
invalidate();
@@ -203,7 +206,8 @@ public void setViewFlow(ViewFlow view) {
*/
@Override
public void onScrolled(int h, int v, int oldh, int oldv) {
- timer.resetTimer();
+ setVisibility(View.VISIBLE);
+ resetTimer();
currentScroll = h;
flowWidth = viewFlow.getWidth();
invalidate();
@@ -303,30 +307,77 @@ public void setStrokeColor(int color) {
invalidate();
}
- // TODO: Stop this counting when view not in view
- // TODO: Stop counting when limit hit
- // TODO: Start animation when limit hit
- private class FadeTimer extends Thread {
+ /**
+ * Resets the fade out timer to 0. Creating a new one if needed
+ */
+ private void resetTimer() {
+ // Check if we need to create a new timer
+ if (timer == null || timer._run == false) {
+ // Create and start a new timer
+ timer = new FadeTimer();
+ timer.execute();
+ }
+ else {
+ // Reset the current tiemr to 0
+ timer.resetTimer();
+ }
+ }
+
+ /**
+ * Counts from 0 to the fade out time and animates the view away when reached
+ */
+ private class FadeTimer extends AsyncTask<Void, Void, Void> {
+ // The current count
private int timer = 0;
+ // If we are inside the timing loop
private boolean _run = true;
public void resetTimer() {
- Log.d("Circle", "Timer reset");
timer = 0;
}
@Override
- public void run() {
+ protected Void doInBackground(Void... arg0) {
while (_run) {
try {
+ // Wait for a second
Thread.sleep(1000);
+ // Increment the timer
timer++;
- Log.d("Circle", "Timer\t" + timer);
+
+ // Check if we've reached the fade out time
+ if (timer == FADE_OUT) {
+ // Stop running
+ _run = false;
+ }
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
+ return null;
}
+
+ @Override
+ protected void onPostExecute(Void result) {
+ animation = AnimationUtils.loadAnimation(getContext(), android.R.anim.fade_out);
+ animation.setAnimationListener(animationListener);
+ startAnimation(animation);
+ }
+ }
+
+ @Override
+ public void onAnimationEnd(Animation animation) {
+ setVisibility(View.GONE);
+ }
+
+ @Override
+ public void onAnimationRepeat(Animation animation) {
+ }
+
+ @Override
+ public void onAnimationStart(Animation animation) {
+ // TODO Auto-generated method stub
+
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.