Android Open Source - holoaccent Indetermined Progress Drawable






From Project

Back to project page holoaccent.

License

The source code is released under:

Apache License

If you think the Android project holoaccent 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 2013 NEGU Soft//from w  ww  . j  av a  2 s  .com
 * 
 * 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 com.negusoft.holoaccent.drawable;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.animation.Interpolator;

import com.negusoft.holoaccent.R;

/**
 * Drawable implementation to replace the bitmaps that compose 
 * the "progress_indeterminate_horizontal" animation.
 * <br/><br/>
 * The gaps position is calculated when loading
 */
public class IndeterminedProgressDrawable extends Drawable {
  
  private static final int DEFAULT_SECTION_NUMBER = 5;

  private static final float MIN_WIDTH_DP = 256.0f;
  private static final float MIN_HEIGHT_DP = 16.0f;
  private static final float LINE_WIDTH_DP = 4.0f;
  private static final float GAP_WIDTH_DP = 4.0f;
  
  private final DisplayMetrics mDisplayMetrics;
    private final Paint mPaint;
    private final float[] mGapPercentages;

    /** Used to calculate the gap positions in a accelarate shape. */
    private Interpolator mInterpolator = new Interpolator() {
        @Override public float getInterpolation(float value) {
            return value * value;
        }
    };
    /** Used to calculate the gap positions in a accelarate/decelarate shape. */
//    private Interpolator mInterpolator = new Interpolator() {
//        @Override public float getInterpolation(float value) {
//            if (value <= 0.5f)
//                return 2 * value * value;
//            float offset = value - 0.5f;
//            return 0.5f + (2*offset) - (2*offset*offset);
//        }
//    }
  
  public IndeterminedProgressDrawable(Context c, int frameIndex, int frameCount) {
    this(c, frameIndex, frameCount, DEFAULT_SECTION_NUMBER);
  }
  
  public IndeterminedProgressDrawable(Context c, int frameIndex, int frameCount, int sectionCount) {
    Resources res = c.getResources();
    mDisplayMetrics = res.getDisplayMetrics();

    TypedArray attrs = c.getTheme().obtainStyledAttributes(R.styleable.HoloAccent);
    int color = attrs.getColor(R.styleable.HoloAccent_accentColor, res.getColor(R.color.ha__accent_default));
    attrs.recycle();

        mPaint = getPaint(mDisplayMetrics, color);
        
        mGapPercentages = getGapPercentages(frameIndex, frameCount, sectionCount);
  }
  
  public IndeterminedProgressDrawable(Resources res, int color, int frameIndex, int frameCount) {
    this(res, color, frameIndex, frameCount, DEFAULT_SECTION_NUMBER);
  }
  
  public IndeterminedProgressDrawable(Resources res, int color, int frameIndex, int frameCount, int sectionCount) {
    mDisplayMetrics = res.getDisplayMetrics();
        mPaint = getPaint(mDisplayMetrics, color);
        mGapPercentages = getGapPercentages(frameIndex, frameCount, sectionCount);
  }
  
  private Paint getPaint(DisplayMetrics displayMetrics, int color) {
        float lineWidth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, LINE_WIDTH_DP, displayMetrics);
    Paint result = new Paint();
    result.setColor(color);
    result.setStyle(Paint.Style.STROKE);
    result.setStrokeWidth(lineWidth);
    return result;
  }
  
  private float[] getGapPercentages(int frameIndex, int frameCount, int sectionCount) {
    float sectionWidth = 1f / sectionCount;
    float offset = sectionWidth / frameCount * frameIndex;
    float[] result = new float[sectionCount];
    for (int i=0; i< result.length; i++) {
      float phase = offset + (i * sectionWidth);
      result[i] = mInterpolator.getInterpolation(phase);
    }
    return result;
  }
  
  @Override
  public int getMinimumWidth() {
    float width = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, MIN_WIDTH_DP, mDisplayMetrics);
    return width < 1.0f ? 1 : (int)width;
  }
  
  @Override
  public int getMinimumHeight() {
    float height = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, MIN_HEIGHT_DP, mDisplayMetrics);
    return height < 1.0f ? 1 : (int)height;
  }

  @Override
  public void draw(Canvas canvas) {
    
    Rect bounds = getBounds();
    
    float totalWidth = bounds.width();
    float minWidth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, MIN_WIDTH_DP, mDisplayMetrics);
    if (totalWidth < minWidth)
      totalWidth = minWidth;

        float gapWidth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, GAP_WIDTH_DP, mDisplayMetrics);
        float centerY = bounds.exactCenterY();
        float startX = bounds.left;
        float stopX;
        
        totalWidth += gapWidth;
        
    for (float startPercentage : mGapPercentages) {
      stopX = bounds.left + (totalWidth * startPercentage) - gapWidth;
      if (stopX < bounds.left) {
        startX = bounds.left + (totalWidth * startPercentage);
        continue;
      }
      canvas.drawLine(startX, centerY, stopX, centerY, mPaint);
      startX = stopX + gapWidth;
    }
    canvas.drawLine(startX, centerY, totalWidth, centerY, mPaint);
  }

  @Override
  public int getOpacity() {
    return PixelFormat.TRANSLUCENT;
  }

  @Override
  public void setAlpha(int alpha) {
    // empty
  }

  @Override
  public void setColorFilter(ColorFilter cf) {
    // empty
  }

}




Java Source Code List

com.negusoft.holoaccent.AccentHelper.java
com.negusoft.holoaccent.AccentPalette.java
com.negusoft.holoaccent.AccentResources.java
com.negusoft.holoaccent.activity.AccentActivity.java
com.negusoft.holoaccent.dialog.AccentAlertDialog.java
com.negusoft.holoaccent.dialog.AccentDatePickerDialog.java
com.negusoft.holoaccent.dialog.AccentDialogFragment.java
com.negusoft.holoaccent.dialog.AccentTimePickerDialog.java
com.negusoft.holoaccent.dialog.DividerPainter.java
com.negusoft.holoaccent.dialog.NumberPickerPainter.java
com.negusoft.holoaccent.drawable.ActionBarBackgroundDrawable.java
com.negusoft.holoaccent.drawable.CircleDrawable.java
com.negusoft.holoaccent.drawable.ContactBadgeDrawable.java
com.negusoft.holoaccent.drawable.FastScrollDrawable.java
com.negusoft.holoaccent.drawable.IndeterminedProgressDrawable.java
com.negusoft.holoaccent.drawable.IndeterminedProgressLegacyDrawable.java
com.negusoft.holoaccent.drawable.RectDrawable.java
com.negusoft.holoaccent.drawable.RoundRectDrawable.java
com.negusoft.holoaccent.drawable.ScrubberControlSelectorDrawable.java
com.negusoft.holoaccent.drawable.ScrubberProgressDrawable.java
com.negusoft.holoaccent.drawable.SearchViewDrawable.java
com.negusoft.holoaccent.drawable.SpinnerDrawable.java
com.negusoft.holoaccent.drawable.ToggleForegroundDrawable.java
com.negusoft.holoaccent.drawable.UnderlineDrawable.java
com.negusoft.holoaccent.example.activity.AccentFragmentActivity.java
com.negusoft.holoaccent.example.activity.DialogActivity.java
com.negusoft.holoaccent.example.activity.MainActivity.java
com.negusoft.holoaccent.example.activity.PreferencesActivity.java
com.negusoft.holoaccent.example.activity.SpinnerActivity.java
com.negusoft.holoaccent.example.activity.TabbedActivity.java
com.negusoft.holoaccent.example.activity.TabbedStripActivity.java
com.negusoft.holoaccent.example.activity.themed.DialogActivityLight.java
com.negusoft.holoaccent.example.activity.themed.PreferencesActivityColoredABInverse.java
com.negusoft.holoaccent.example.activity.themed.PreferencesActivityColoredAB.java
com.negusoft.holoaccent.example.activity.themed.PreferencesActivityLightColoredABInverse.java
com.negusoft.holoaccent.example.activity.themed.PreferencesActivityLightColoredAB.java
com.negusoft.holoaccent.example.activity.themed.PreferencesActivityLightDarkAB.java
com.negusoft.holoaccent.example.activity.themed.PreferencesActivityLight.java
com.negusoft.holoaccent.example.activity.themed.SpinnerActivityColoredABInverse.java
com.negusoft.holoaccent.example.activity.themed.SpinnerActivityColoredAB.java
com.negusoft.holoaccent.example.activity.themed.SpinnerActivityLightColoredABInverse.java
com.negusoft.holoaccent.example.activity.themed.SpinnerActivityLightColoredAB.java
com.negusoft.holoaccent.example.activity.themed.SpinnerActivityLightDarkAB.java
com.negusoft.holoaccent.example.activity.themed.SpinnerActivityLight.java
com.negusoft.holoaccent.example.activity.themed.TabbedActivityColoredABInverse.java
com.negusoft.holoaccent.example.activity.themed.TabbedActivityColoredAB.java
com.negusoft.holoaccent.example.activity.themed.TabbedActivityLightColoredABInverse.java
com.negusoft.holoaccent.example.activity.themed.TabbedActivityLightColoredAB.java
com.negusoft.holoaccent.example.activity.themed.TabbedActivityLightDarkAB.java
com.negusoft.holoaccent.example.activity.themed.TabbedActivityLight.java
com.negusoft.holoaccent.example.activity.themed.TabbedStripActivityColoredABInverse.java
com.negusoft.holoaccent.example.activity.themed.TabbedStripActivityColoredAB.java
com.negusoft.holoaccent.example.activity.themed.TabbedStripActivityLightColoredABInverse.java
com.negusoft.holoaccent.example.activity.themed.TabbedStripActivityLightColoredAB.java
com.negusoft.holoaccent.example.activity.themed.TabbedStripActivityLightDarkAB.java
com.negusoft.holoaccent.example.activity.themed.TabbedStripActivityLight.java
com.negusoft.holoaccent.example.fragment.AccentSupportDialogFragment.java
com.negusoft.holoaccent.example.fragment.ButtonFragment.java
com.negusoft.holoaccent.example.fragment.ChoicesFragment.java
com.negusoft.holoaccent.example.fragment.ListFragment.java
com.negusoft.holoaccent.example.fragment.PickersFragment.java
com.negusoft.holoaccent.example.fragment.ProgressFragment.java
com.negusoft.holoaccent.example.fragment.SimpleDialogFragment.java
com.negusoft.holoaccent.example.fragment.TextviewFragment.java
com.negusoft.holoaccent.example.model.ColorOverrideConfig.java
com.negusoft.holoaccent.example.preference.CustomDialogPreference.java
com.negusoft.holoaccent.example.view.CheckableTextView.java
com.negusoft.holoaccent.interceptor.AccentColorInterceptor.java
com.negusoft.holoaccent.interceptor.ActionBarBackgroundInterceptor.java
com.negusoft.holoaccent.interceptor.CircleInterceptor.java
com.negusoft.holoaccent.interceptor.ContactBadgeInterceptor.java
com.negusoft.holoaccent.interceptor.FastScrollInterceptor.java
com.negusoft.holoaccent.interceptor.IndeterminateInterceptor.java
com.negusoft.holoaccent.interceptor.NativeInterceptor.java
com.negusoft.holoaccent.interceptor.PagerTabStripInterceptor.java
com.negusoft.holoaccent.interceptor.RectInterceptor.java
com.negusoft.holoaccent.interceptor.RoundRectInterceptor.java
com.negusoft.holoaccent.interceptor.ScrubberInterceptor.java
com.negusoft.holoaccent.interceptor.SearchViewTextFieldInterceptor.java
com.negusoft.holoaccent.interceptor.SolidColorInterceptor.java
com.negusoft.holoaccent.interceptor.SpinnerInterceptor.java
com.negusoft.holoaccent.interceptor.ToggleInterceptor.java
com.negusoft.holoaccent.interceptor.UnderlineInterceptor.java
com.negusoft.holoaccent.preference.DialogPreference.java
com.negusoft.holoaccent.preference.EditTextPreference.java
com.negusoft.holoaccent.preference.ListPreference.java
com.negusoft.holoaccent.preference.MultiSelectListPreference.java
com.negusoft.holoaccent.preference.SwitchPreference.java
com.negusoft.holoaccent.util.BitmapUtils.java
com.negusoft.holoaccent.util.NativeResources.java
com.negusoft.holoaccent.widget.AccentDatePicker.java
com.negusoft.holoaccent.widget.AccentNumberPicker.java
com.negusoft.holoaccent.widget.AccentQuickContactBadge.java
com.negusoft.holoaccent.widget.AccentRatingBar.java
com.negusoft.holoaccent.widget.AccentSearchView.java
com.negusoft.holoaccent.widget.AccentSwitch.java
com.negusoft.holoaccent.widget.AccentTimePicker.java