Skip to content

Commit

Permalink
Added fading out when limit hit
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Oakes committed Aug 21, 2011
1 parent ff2f634 commit 1e61279
Showing 1 changed file with 66 additions and 15 deletions.
81 changes: 66 additions & 15 deletions viewflow/src/org/taptwo/android/widget/CircleFlowIndicator.java
Expand Up @@ -22,9 +22,12 @@
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.Paint; import android.graphics.Paint;
import android.graphics.Paint.Style; import android.graphics.Paint.Style;
import android.os.AsyncTask;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.Log;
import android.view.View; 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 * A FlowIndicator which draws circles (one for each view). The current view
Expand All @@ -41,17 +44,21 @@
* radius: Define the circle radius (default to 4.0) * radius: Define the circle radius (default to 4.0)
* </ul> * </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_STROKE = 0;
private static final int STYLE_FILL = 1; private static final int STYLE_FILL = 1;


private static final int FADE_OUT = 2;

private float radius = 4; private float radius = 4;
private final Paint mPaintStroke = new Paint(Paint.ANTI_ALIAS_FLAG); private final Paint mPaintStroke = new Paint(Paint.ANTI_ALIAS_FLAG);
private final Paint mPaintActive = new Paint(Paint.ANTI_ALIAS_FLAG); private final Paint mPaintActive = new Paint(Paint.ANTI_ALIAS_FLAG);
private ViewFlow viewFlow; private ViewFlow viewFlow;
private int currentScroll = 0; private int currentScroll = 0;
private int flowWidth = 0; private int flowWidth = 0;
private FadeTimer timer; private FadeTimer timer;
public AnimationListener animationListener = this;
private Animation animation;


/** /**
* Default constructor * Default constructor
Expand Down Expand Up @@ -112,8 +119,6 @@ public CircleFlowIndicator(Context context, AttributeSet attrs) {
// Retrieve the radius // Retrieve the radius
radius = a.getDimension(R.styleable.CircleFlowIndicator_radius, 4.0f); radius = a.getDimension(R.styleable.CircleFlowIndicator_radius, 4.0f);
initColors(activeColor, inactiveColor, activeType, inactiveType); initColors(activeColor, inactiveColor, activeType, inactiveType);


} }


private void initColors(int activeColor, int inactiveColor, int activeType, private void initColors(int activeColor, int inactiveColor, int activeType,
Expand Down Expand Up @@ -187,9 +192,7 @@ public void onSwitched(View view, int position) {
*/ */
@Override @Override
public void setViewFlow(ViewFlow view) { public void setViewFlow(ViewFlow view) {
timer = new FadeTimer(); resetTimer();
timer.start();

viewFlow = view; viewFlow = view;
flowWidth = viewFlow.getWidth(); flowWidth = viewFlow.getWidth();
invalidate(); invalidate();
Expand All @@ -203,7 +206,8 @@ public void setViewFlow(ViewFlow view) {
*/ */
@Override @Override
public void onScrolled(int h, int v, int oldh, int oldv) { public void onScrolled(int h, int v, int oldh, int oldv) {
timer.resetTimer(); setVisibility(View.VISIBLE);
resetTimer();
currentScroll = h; currentScroll = h;
flowWidth = viewFlow.getWidth(); flowWidth = viewFlow.getWidth();
invalidate(); invalidate();
Expand Down Expand Up @@ -303,30 +307,77 @@ public void setStrokeColor(int color) {
invalidate(); invalidate();
} }


// TODO: Stop this counting when view not in view /**
// TODO: Stop counting when limit hit * Resets the fade out timer to 0. Creating a new one if needed
// TODO: Start animation when limit hit */
private class FadeTimer extends Thread { 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; private int timer = 0;
// If we are inside the timing loop
private boolean _run = true; private boolean _run = true;


public void resetTimer() { public void resetTimer() {
Log.d("Circle", "Timer reset");
timer = 0; timer = 0;
} }


@Override @Override
public void run() { protected Void doInBackground(Void... arg0) {
while (_run) { while (_run) {
try { try {
// Wait for a second
Thread.sleep(1000); Thread.sleep(1000);
// Increment the timer
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) { } catch (InterruptedException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); 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

} }
} }

0 comments on commit 1e61279

Please sign in to comment.