Android Open Source - Munin-for-Android Circle Flow Indicator






From Project

Back to project page Munin-for-Android.

License

The source code is released under:

GNU General Public License

If you think the Android project Munin-for-Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2011 Patrik ?kerfeldt//  w ww . jav  a 2 s. c  o m
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.taptwo.android.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.os.AsyncTask;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;

import com.chteuchteu.munin.R;

/**
 * A FlowIndicator which draws circles (one for each view). 
 * <br/>
 * Availables attributes are:<br/>
 * <ul>
 * activeColor: Define the color used to draw the active circle (default to white)
 * </ul>
 * <ul>
 * inactiveColor: Define the color used to draw the inactive circles (default to 0x44FFFFFF)
 * </ul>
 * <ul>
 * inactiveType: Define how to draw the inactive circles, either stroke or fill (default to stroke)
 * </ul>
 * <ul>
 * activeType: Define how to draw the active circle, either stroke or fill (default to fill)
 * </ul>
 * <ul>
 * fadeOut: Define the time (in ms) until the indicator will fade out (default to 0 = never fade out)
 * </ul>
 * <ul>
 * radius: Define the circle radius (default to 4.0)
 * </ul>
 *  * <ul>
 * spacing: Define the circle spacing (default to 4.0)
 * </ul>
 */
public class CircleFlowIndicator extends View implements FlowIndicator,
    AnimationListener {
  private static final int STYLE_STROKE = 0;
  private static final int STYLE_FILL = 1;

  private float radius = 4;
  private float spacing = 4;
  private int fadeOutTime = 0;
  private final Paint mPaintInactive = new Paint(Paint.ANTI_ALIAS_FLAG);
  private final Paint mPaintActive = new Paint(Paint.ANTI_ALIAS_FLAG);
  private ViewFlow viewFlow;
  private int currentScroll = 0;
  private int flowWidth = 0;
  private FadeTimer timer;
  public AnimationListener animationListener = this;
  private Animation animation;
  @SuppressWarnings("unused")
  private boolean mCentered = false;

  /**
   * Default constructor
   * 
   * @param context
   */
  public CircleFlowIndicator(Context context) {
    super(context);
    initColors(0xFFFFFFFF, 0xFFFFFFFF, STYLE_FILL, STYLE_STROKE);
  }

  /**
   * The contructor used with an inflater
   * 
   * @param context
   * @param attrs
   */
  @SuppressLint("Recycle")
  public CircleFlowIndicator(Context context, AttributeSet attrs) {
    super(context, attrs);
    // Retrieve styles attributs
    TypedArray a = context.obtainStyledAttributes(attrs,
        R.styleable.CircleFlowIndicator);

    // Gets the active circle type, defaulting to "fill"
    int activeType = a.getInt(R.styleable.CircleFlowIndicator_activeType,
        STYLE_FILL);
    
    int activeDefaultColor = 0xFFFFFFFF;
    
    // Get a custom active color if there is one
    int activeColor = a
        .getColor(R.styleable.CircleFlowIndicator_activeColor,
            activeDefaultColor);

    // Gets the inactive circle type, defaulting to "stroke"
    int inactiveType = a.getInt(
        R.styleable.CircleFlowIndicator_inactiveType, STYLE_STROKE);

    int inactiveDefaultColor = 0x44FFFFFF;
    // Get a custom inactive color if there is one
    int inactiveColor = a.getColor(
        R.styleable.CircleFlowIndicator_inactiveColor,
        inactiveDefaultColor);

    // Retrieve the radius
    radius = a.getDimension(R.styleable.CircleFlowIndicator_radius, 4.0f);
    
    // Retrieve the spacing
    spacing = a.getDimension(R.styleable.CircleFlowIndicator_spacing, 4.0f);
    
    // Retrieve the fade out time
    fadeOutTime = a.getInt(R.styleable.CircleFlowIndicator_fadeOut, 0);
    
    mCentered = a.getBoolean(R.styleable.CircleFlowIndicator_centered, false);
    
    initColors(activeColor, inactiveColor, activeType, inactiveType);
  }

  private void initColors(int activeColor, int inactiveColor, int activeType,
      int inactiveType) {
    // Select the paint type given the type attr
    switch (inactiveType) {
    case STYLE_FILL:
      mPaintInactive.setStyle(Style.FILL);
      break;
    default:
      mPaintInactive.setStyle(Style.STROKE);
    }
    mPaintInactive.setColor(inactiveColor);

    // Select the paint type given the type attr
    switch (activeType) {
    case STYLE_STROKE:
      mPaintActive.setStyle(Style.STROKE);
      break;
    default:
      mPaintActive.setStyle(Style.FILL);
    }
    mPaintActive.setColor(activeColor);
  }

  /*
   * (non-Javadoc)
   * 
   * @see android.view.View#onDraw(android.graphics.Canvas)
   */
  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    int count = 3;
    if (viewFlow != null) {
      count = viewFlow.getViewsCount();
    }
    
    float circleSeparation = 2*radius+spacing;
    //this is the amount the first circle should be offset to make the entire thing centered
    float centeringOffset = 0;
    
    int leftPadding = getPaddingLeft();
    
    // Draw stroked circles
    for (int iLoop = 0; iLoop < count; iLoop++) {
      canvas.drawCircle(leftPadding + radius
          + (iLoop * circleSeparation) + centeringOffset,
          getPaddingTop() + radius, radius, mPaintInactive);
    }
    float cx = 0;
    if (flowWidth != 0) {
      // Draw the filled circle according to the current scroll
      cx = (currentScroll * (2 * radius + spacing)) / flowWidth;
    }
    // The flow width has been upadated yet. Draw the default position
    canvas.drawCircle(leftPadding + radius + cx+centeringOffset, getPaddingTop()
        + radius, radius, mPaintActive);
  }

  /*
   * (non-Javadoc)
   * 
   * @see
   * org.taptwo.android.widget.ViewFlow.ViewSwitchListener#onSwitched(android
   * .view.View, int)
   */
  @Override
  public void onSwitched(View view, int position) {
  }

  /*
   * (non-Javadoc)
   * 
   * @see
   * org.taptwo.android.widget.FlowIndicator#setViewFlow(org.taptwo.android
   * .widget.ViewFlow)
   */
  @Override
  public void setViewFlow(ViewFlow view) {
    resetTimer();
    viewFlow = view;
    flowWidth = viewFlow.getWidth();
    invalidate();
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.taptwo.android.widget.FlowIndicator#onScrolled(int, int, int,
   * int)
   */
  @Override
  public void onScrolled(int h, int v, int oldh, int oldv) {
    setVisibility(View.VISIBLE);
    resetTimer();
    currentScroll = h;
    flowWidth = viewFlow.getWidth();
    invalidate();
  }

  /*
   * (non-Javadoc)
   * 
   * @see android.view.View#onMeasure(int, int)
   */
  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    setMeasuredDimension(measureWidth(widthMeasureSpec),
        measureHeight(heightMeasureSpec));
  }

  /**
   * Determines the width of this view
   * 
   * @param measureSpec
   *            A measureSpec packed into an int
   * @return The width of the view, honoring constraints from measureSpec
   */
  private int measureWidth(int measureSpec) {
    int result = 0;
    int specMode = MeasureSpec.getMode(measureSpec);
    int specSize = MeasureSpec.getSize(measureSpec);

    // We were told how big to be
    if (specMode == MeasureSpec.EXACTLY) {
      result = specSize;
    }
    // Calculate the width according the views count
    else {
      int count = 3;
      if (viewFlow != null) {
        count = viewFlow.getViewsCount();
      }
      result = (int) (getPaddingLeft() + getPaddingRight()
          + (count * 2 * radius) + (count - 1) * spacing + 1);
      // Respect AT_MOST value if that was what is called for by
      // measureSpec
      if (specMode == MeasureSpec.AT_MOST) {
        result = Math.min(result, specSize);
      }
    }
    return result;
  }

  /**
   * Determines the height of this view
   * 
   * @param measureSpec
   *            A measureSpec packed into an int
   * @return The height of the view, honoring constraints from measureSpec
   */
  private int measureHeight(int measureSpec) {
    int result = 0;
    int specMode = MeasureSpec.getMode(measureSpec);
    int specSize = MeasureSpec.getSize(measureSpec);

    // We were told how big to be
    if (specMode == MeasureSpec.EXACTLY) {
      result = specSize;
    }
    // Measure the height
    else {
      result = (int) (2 * radius + getPaddingTop() + getPaddingBottom() + 1);
      // Respect AT_MOST value if that was what is called for by
      // measureSpec
      if (specMode == MeasureSpec.AT_MOST) {
        result = Math.min(result, specSize);
      }
    }
    return result;
  }

  /**
   * Sets the fill color
   * 
   * @param color
   *            ARGB value for the text
   */
  public void setFillColor(int color) {
    mPaintActive.setColor(color);
    invalidate();
  }

  /**
   * Sets the stroke color
   * 
   * @param color
   *            ARGB value for the text
   */
  public void setStrokeColor(int color) {
    mPaintInactive.setColor(color);
    invalidate();
  }

  /**
   * Resets the fade out timer to 0. Creating a new one if needed
   */
  private void resetTimer() {
    // Only set the timer if we have a timeout of at least 1 millisecond
    if (fadeOutTime > 0) {
      // 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() {
      timer = 0;
    }

    @Override
    protected Void doInBackground(Void... arg0) {
      while (_run) {
        try {
          // Wait for a millisecond
          Thread.sleep(1);
          // Increment the timer
          timer++;

          // Check if we've reached the fade out time
          if (timer == fadeOutTime) {
            // 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) {
  }
}




Java Source Code List

com.chteuchteu.munin.BootReceiver.java
com.chteuchteu.munin.CustomSSLFactory.java
com.chteuchteu.munin.MuninFoo.java
com.chteuchteu.munin.Service_Notifications.java
com.chteuchteu.munin.adptr.Adapter_ExpandableListView.java
com.chteuchteu.munin.adptr.Adapter_GraphView.java
com.chteuchteu.munin.adptr.Adapter_IconList.java
com.chteuchteu.munin.adptr.Adapter_SeparatedList.java
com.chteuchteu.munin.exc.ImportExportWebserviceException.java
com.chteuchteu.munin.exc.NullMuninFooException.java
com.chteuchteu.munin.exc.TrialExpirationDateReached.java
com.chteuchteu.munin.hlpr.BillingService.java
com.chteuchteu.munin.hlpr.DatabaseHelper.java
com.chteuchteu.munin.hlpr.DigestUtils.java
com.chteuchteu.munin.hlpr.DocumentationHelper.java
com.chteuchteu.munin.hlpr.DrawerHelper.java
com.chteuchteu.munin.hlpr.DynazoomHelper.java
com.chteuchteu.munin.hlpr.EncryptionHelper.java
com.chteuchteu.munin.hlpr.GridDownloadHelper.java
com.chteuchteu.munin.hlpr.I18nHelper.java
com.chteuchteu.munin.hlpr.ImportExportHelper.java
com.chteuchteu.munin.hlpr.JSONHelper.java
com.chteuchteu.munin.hlpr.MediaScannerUtil.java
com.chteuchteu.munin.hlpr.NetHelper.java
com.chteuchteu.munin.hlpr.SQLite.java
com.chteuchteu.munin.hlpr.Util.java
com.chteuchteu.munin.obj.AlertsWidget.java
com.chteuchteu.munin.obj.GraphWidget.java
com.chteuchteu.munin.obj.GridItem.java
com.chteuchteu.munin.obj.Grid.java
com.chteuchteu.munin.obj.HTTPResponse_Bitmap.java
com.chteuchteu.munin.obj.HTTPResponse.java
com.chteuchteu.munin.obj.Label.java
com.chteuchteu.munin.obj.MuninMaster.java
com.chteuchteu.munin.obj.MuninPlugin.java
com.chteuchteu.munin.obj.MuninServer.java
com.chteuchteu.munin.obj.SearchResult.java
com.chteuchteu.munin.ui.Activity_About.java
com.chteuchteu.munin.ui.Activity_AlertsPluginSelection.java
com.chteuchteu.munin.ui.Activity_Alerts.java
com.chteuchteu.munin.ui.Activity_GoPremium.java
com.chteuchteu.munin.ui.Activity_GraphView.java
com.chteuchteu.munin.ui.Activity_Grid.java
com.chteuchteu.munin.ui.Activity_Grids.java
com.chteuchteu.munin.ui.Activity_Label.java
com.chteuchteu.munin.ui.Activity_Labels.java
com.chteuchteu.munin.ui.Activity_Main.java
com.chteuchteu.munin.ui.Activity_Notifications.java
com.chteuchteu.munin.ui.Activity_Plugins.java
com.chteuchteu.munin.ui.Activity_Server.java
com.chteuchteu.munin.ui.Activity_ServersEdit.java
com.chteuchteu.munin.ui.Activity_Servers.java
com.chteuchteu.munin.ui.Activity_Settings.java
com.chteuchteu.munin.ui.HackyDrawerLayout.java
com.chteuchteu.munin.ui.MuninActivity.java
com.chteuchteu.munin.wdget.Widget_AlertsWidget_Configure.java
com.chteuchteu.munin.wdget.Widget_AlertsWidget_ViewsFactory.java
com.chteuchteu.munin.wdget.Widget_AlertsWidget_WidgetProvider.java
com.chteuchteu.munin.wdget.Widget_AlertsWidget_WidgetService.java
com.chteuchteu.munin.wdget.Widget_GraphWidget_Configure.java
com.chteuchteu.munin.wdget.Widget_GraphWidget_WidgetProvider.java
com.mobeta.android.dslv.DragSortController.java
com.mobeta.android.dslv.DragSortCursorAdapter.java
com.mobeta.android.dslv.DragSortItemViewCheckable.java
com.mobeta.android.dslv.DragSortItemView.java
com.mobeta.android.dslv.DragSortListView.java
com.mobeta.android.dslv.ResourceDragSortCursorAdapter.java
com.mobeta.android.dslv.SimpleDragSortCursorAdapter.java
com.mobeta.android.dslv.SimpleFloatViewManager.java
org.taptwo.android.widget.CircleFlowIndicator.java
org.taptwo.android.widget.FlowIndicator.java
org.taptwo.android.widget.TitleFlowIndicator.java
org.taptwo.android.widget.TitleProvider.java
org.taptwo.android.widget.ViewFlow.java
uk.co.senab.photoview.Compat.java
uk.co.senab.photoview.IPhotoView.java
uk.co.senab.photoview.PhotoViewAttacher.java
uk.co.senab.photoview.PhotoView.java
uk.co.senab.photoview.SDK16.java
uk.co.senab.photoview.ScrollerProxy.java
uk.co.senab.photoview.VersionedGestureDetector.java