Android Open Source - PullToRefresh Smooth Progress Bar






From Project

Back to project page PullToRefresh.

License

The source code is released under:

Apache License

If you think the Android project PullToRefresh 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

package fr.castorflex.android.smoothprogressbar;
//from w  w  w .j  a v  a  2s.  co m
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;
import android.widget.ProgressBar;

/**
 * Created by castorflex on 11/10/13.
 */
public class SmoothProgressBar extends ProgressBar {

    private static final int INTERPOLATOR_ACCELERATE = 0;
    private static final int INTERPOLATOR_LINEAR = 1;
    private static final int INTERPOLATOR_ACCELERATEDECELERATE = 2;
    private static final int INTERPOLATOR_DECELERATE = 3;

    public SmoothProgressBar(Context context) {
        this(context, null);
    }

    public SmoothProgressBar(Context context, AttributeSet attrs) {
        this(context, attrs, R.attr.spbStyle);
    }

    public SmoothProgressBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        Resources res = context.getResources();
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SmoothProgressBar, defStyle, 0);


        final int color = a.getColor(R.styleable.SmoothProgressBar_spb_color, res.getColor(R.color.spb_default_color));
        final int sectionsCount = a.getInteger(R.styleable.SmoothProgressBar_spb_sections_count, res.getInteger(R.integer.spb_default_sections_count));
        final int separatorLength = a.getDimensionPixelSize(R.styleable.SmoothProgressBar_spb_stroke_separator_length, res.getDimensionPixelSize(R.dimen.spb_default_stroke_separator_length));
        final float strokeWidth = a.getDimension(R.styleable.SmoothProgressBar_spb_stroke_width, res.getDimension(R.dimen.spb_default_stroke_width));
        final float speed = a.getFloat(R.styleable.SmoothProgressBar_spb_speed, Float.parseFloat(res.getString(R.string.spb_default_speed)));
        final int iInterpolator = a.getInteger(R.styleable.SmoothProgressBar_spb_interpolator, res.getInteger(R.integer.spb_default_interpolator));
        final boolean reversed = a.getBoolean(R.styleable.SmoothProgressBar_spb_reversed, res.getBoolean(R.bool.spb_default_reversed));
        final boolean mirrorMode = a.getBoolean(R.styleable.SmoothProgressBar_spb_mirror_mode, res.getBoolean(R.bool.spb_default_mirror_mode));
        final int colorsId = a.getResourceId(R.styleable.SmoothProgressBar_spb_colors, 0);
        a.recycle();

        //interpolator
        Interpolator interpolator;
        switch (iInterpolator) {
            case INTERPOLATOR_ACCELERATEDECELERATE:
                interpolator = new AccelerateDecelerateInterpolator();
                break;
            case INTERPOLATOR_DECELERATE:
                interpolator = new DecelerateInterpolator();
                break;
            case INTERPOLATOR_LINEAR:
                interpolator = new LinearInterpolator();
                break;
            case INTERPOLATOR_ACCELERATE:
            default:
                interpolator = new AccelerateInterpolator();
        }

        int[] colors = null;
        //colors
        if (colorsId != 0) {
            colors = res.getIntArray(colorsId);
        }

        SmoothProgressDrawable.Builder builder = new SmoothProgressDrawable.Builder(context)
                .speed(speed)
                .interpolator(interpolator)
                .sectionsCount(sectionsCount)
                .separatorLength(separatorLength)
                .strokeWidth(strokeWidth)
                .reversed(reversed)
                .mirrorMode(mirrorMode);

        if(colors != null && colors.length > 0)
            builder.colors(colors);
        else
            builder.color(color);

        setIndeterminateDrawable(builder.build());
    }

    public void applyStyle(int styleResId){
        TypedArray a = getContext().obtainStyledAttributes(null, R.styleable.SmoothProgressBar, 0, styleResId);

        if(a.hasValue(R.styleable.SmoothProgressBar_spb_color)){
            setSmoothProgressDrawableColor(a.getColor(R.styleable.SmoothProgressBar_spb_color, 0));
        }
        if(a.hasValue(R.styleable.SmoothProgressBar_spb_colors)){
            int colorsId = a.getResourceId(R.styleable.SmoothProgressBar_spb_colors, 0);
            if(colorsId != 0){
                int[] colors = getResources().getIntArray(colorsId);
                if(colors != null && colors.length > 0)
                    setSmoothProgressDrawableColors(colors);
            }
        }
        if(a.hasValue(R.styleable.SmoothProgressBar_spb_sections_count)){
            setSmoothProgressDrawableSectionsCount(a.getInteger(R.styleable.SmoothProgressBar_spb_sections_count, 0));
        }
        if(a.hasValue(R.styleable.SmoothProgressBar_spb_stroke_separator_length)){
            setSmoothProgressDrawableSeparatorLength(a.getDimensionPixelSize(R.styleable.SmoothProgressBar_spb_stroke_separator_length, 0));
        }
        if(a.hasValue(R.styleable.SmoothProgressBar_spb_stroke_width)){
            setSmoothProgressDrawableStrokeWidth(a.getDimension(R.styleable.SmoothProgressBar_spb_stroke_width, 0));
        }
        if(a.hasValue(R.styleable.SmoothProgressBar_spb_speed)){
            setSmoothProgressDrawableSpeed(a.getFloat(R.styleable.SmoothProgressBar_spb_speed, 0));
        }
        if(a.hasValue(R.styleable.SmoothProgressBar_spb_reversed)){
            setSmoothProgressDrawableReversed(a.getBoolean(R.styleable.SmoothProgressBar_spb_reversed, false));
        }
        if(a.hasValue(R.styleable.SmoothProgressBar_spb_mirror_mode)){
            setSmoothProgressDrawableMirrorMode(a.getBoolean(R.styleable.SmoothProgressBar_spb_mirror_mode, false));
        }
        if(a.hasValue(R.styleable.SmoothProgressBar_spb_interpolator)){
            int iInterpolator = a.getInteger(R.styleable.SmoothProgressBar_spb_interpolator, -1);
            Interpolator interpolator;
            switch (iInterpolator) {
                case INTERPOLATOR_ACCELERATEDECELERATE:
                    interpolator = new AccelerateDecelerateInterpolator();
                    break;
                case INTERPOLATOR_DECELERATE:
                    interpolator = new DecelerateInterpolator();
                    break;
                case INTERPOLATOR_LINEAR:
                    interpolator = new LinearInterpolator();
                    break;
                case INTERPOLATOR_ACCELERATE:
                    interpolator = new AccelerateInterpolator();
                    break;
                default:
                    interpolator = null;
            }
            if(interpolator != null){
                setInterpolator(interpolator);
            }
        }
        a.recycle();
    }

    private SmoothProgressDrawable checkIndeterminateDrawable(){
        Drawable ret = getIndeterminateDrawable();
        if(ret == null || !(ret instanceof SmoothProgressDrawable))
            throw new RuntimeException("The drawable is not a SmoothProgressDrawable");
        return (SmoothProgressDrawable) ret;
    }

    @Override
    public void setInterpolator(Interpolator interpolator) {
        super.setInterpolator(interpolator);
        Drawable ret = getIndeterminateDrawable();
        if(ret != null && (ret instanceof SmoothProgressDrawable))
            ((SmoothProgressDrawable) ret).setInterpolator(interpolator);
    }

    public void setSmoothProgressDrawableInterpolator(Interpolator interpolator) {
        checkIndeterminateDrawable().setInterpolator(interpolator);
    }

    public void setSmoothProgressDrawableColors(int[] colors) {
        checkIndeterminateDrawable().setColors(colors);
    }

    public void setSmoothProgressDrawableColor(int color) {
        checkIndeterminateDrawable().setColor(color);
    }

    public void setSmoothProgressDrawableSpeed(float speed){
        checkIndeterminateDrawable().setSpeed(speed);
    }

    public void setSmoothProgressDrawableSectionsCount(int sectionsCount){
        checkIndeterminateDrawable().setSectionsCount(sectionsCount);
    }

    public void setSmoothProgressDrawableSeparatorLength(int separatorLength){
        checkIndeterminateDrawable().setSeparatorLength(separatorLength);
    }

    public void setSmoothProgressDrawableStrokeWidth(float strokeWidth){
        checkIndeterminateDrawable().setStrokeWidth(strokeWidth);
    }

    public void setSmoothProgressDrawableReversed(boolean reversed){
        checkIndeterminateDrawable().setReversed(reversed);
    }

    public void setSmoothProgressDrawableMirrorMode(boolean mirrorMode){
        checkIndeterminateDrawable().setMirrorMode(mirrorMode);
    }
}




Java Source Code List

android.support.v7.appcompat.BuildConfig.java
android.support.v7.appcompat.BuildConfig.java
android.support.v7.gridlayout.BuildConfig.java
android.support.v7.gridlayout.BuildConfig.java
fr.castorflex.android.smoothprogressbar.BuildConfig.java
fr.castorflex.android.smoothprogressbar.BuildConfig.java
fr.castorflex.android.smoothprogressbar.ContentLoadingSmoothProgressBar.java
fr.castorflex.android.smoothprogressbar.ContentLoadingSmoothProgressBar.java
fr.castorflex.android.smoothprogressbar.ContentLoadingSmoothProgressBar.java
fr.castorflex.android.smoothprogressbar.SmoothProgressBar.java
fr.castorflex.android.smoothprogressbar.SmoothProgressBar.java
fr.castorflex.android.smoothprogressbar.SmoothProgressBar.java
fr.castorflex.android.smoothprogressbar.SmoothProgressDrawable.java
fr.castorflex.android.smoothprogressbar.SmoothProgressDrawable.java
fr.castorflex.android.smoothprogressbar.SmoothProgressDrawable.java
uk.co.senab.actionbarpulltorefresh.extras.actionbarcompat.AbcDefaultHeaderTransformer.java
uk.co.senab.actionbarpulltorefresh.extras.actionbarcompat.AbcPullToRefreshAttacher.java
uk.co.senab.actionbarpulltorefresh.extras.actionbarcompat.BuildConfig.java
uk.co.senab.actionbarpulltorefresh.extras.actionbarcompat.PullToRefreshLayout.java
uk.co.senab.actionbarpulltorefresh.extras.actionbarsherlock.AbsDefaultHeaderTransformer.java
uk.co.senab.actionbarpulltorefresh.extras.actionbarsherlock.AbsPullToRefreshAttacher.java
uk.co.senab.actionbarpulltorefresh.extras.actionbarsherlock.PullToRefreshLayout.java
uk.co.senab.actionbarpulltorefresh.library.ActionBarPullToRefresh.java
uk.co.senab.actionbarpulltorefresh.library.ActionBarPullToRefresh.java
uk.co.senab.actionbarpulltorefresh.library.BuildConfig.java
uk.co.senab.actionbarpulltorefresh.library.BuildConfig.java
uk.co.senab.actionbarpulltorefresh.library.DefaultHeaderTransformer.java
uk.co.senab.actionbarpulltorefresh.library.DefaultHeaderTransformer.java
uk.co.senab.actionbarpulltorefresh.library.EnvironmentDelegate.java
uk.co.senab.actionbarpulltorefresh.library.EnvironmentDelegate.java
uk.co.senab.actionbarpulltorefresh.library.HeaderTransformer.java
uk.co.senab.actionbarpulltorefresh.library.HeaderTransformer.java
uk.co.senab.actionbarpulltorefresh.library.InstanceCreationUtils.java
uk.co.senab.actionbarpulltorefresh.library.InstanceCreationUtils.java
uk.co.senab.actionbarpulltorefresh.library.Options.java
uk.co.senab.actionbarpulltorefresh.library.Options.java
uk.co.senab.actionbarpulltorefresh.library.PullToRefreshAttacher.java
uk.co.senab.actionbarpulltorefresh.library.PullToRefreshAttacher.java
uk.co.senab.actionbarpulltorefresh.library.PullToRefreshLayout.java
uk.co.senab.actionbarpulltorefresh.library.PullToRefreshLayout.java
uk.co.senab.actionbarpulltorefresh.library.listeners.HeaderViewListener.java
uk.co.senab.actionbarpulltorefresh.library.listeners.HeaderViewListener.java
uk.co.senab.actionbarpulltorefresh.library.listeners.OnRefreshListener.java
uk.co.senab.actionbarpulltorefresh.library.listeners.OnRefreshListener.java
uk.co.senab.actionbarpulltorefresh.library.sdk.CompatBase.java
uk.co.senab.actionbarpulltorefresh.library.sdk.CompatBase.java
uk.co.senab.actionbarpulltorefresh.library.sdk.CompatV11.java
uk.co.senab.actionbarpulltorefresh.library.sdk.CompatV11.java
uk.co.senab.actionbarpulltorefresh.library.sdk.CompatV16.java
uk.co.senab.actionbarpulltorefresh.library.sdk.CompatV16.java
uk.co.senab.actionbarpulltorefresh.library.sdk.Compat.java
uk.co.senab.actionbarpulltorefresh.library.sdk.Compat.java
uk.co.senab.actionbarpulltorefresh.library.viewdelegates.AbsListViewDelegate.java
uk.co.senab.actionbarpulltorefresh.library.viewdelegates.AbsListViewDelegate.java
uk.co.senab.actionbarpulltorefresh.library.viewdelegates.ScrollYDelegate.java
uk.co.senab.actionbarpulltorefresh.library.viewdelegates.ScrollYDelegate.java
uk.co.senab.actionbarpulltorefresh.library.viewdelegates.ViewDelegate.java
uk.co.senab.actionbarpulltorefresh.library.viewdelegates.ViewDelegate.java
uk.co.senab.actionbarpulltorefresh.library.viewdelegates.WebViewDelegate.java
uk.co.senab.actionbarpulltorefresh.library.viewdelegates.WebViewDelegate.java
uk.co.senab.actionbarpulltorefresh.samples.actionbarcompat.BaseSampleActivity.java
uk.co.senab.actionbarpulltorefresh.samples.actionbarcompat.Constants.java
uk.co.senab.actionbarpulltorefresh.samples.actionbarcompat.FragmentTabPager.java
uk.co.senab.actionbarpulltorefresh.samples.actionbarcompat.FragmentTabsActivity.java
uk.co.senab.actionbarpulltorefresh.samples.actionbarcompat.FragmentTabsViewPagerActivity.java
uk.co.senab.actionbarpulltorefresh.samples.actionbarcompat.GridViewActivity.java
uk.co.senab.actionbarpulltorefresh.samples.actionbarcompat.ListViewActivity.java
uk.co.senab.actionbarpulltorefresh.samples.actionbarcompat.MainActivity.java
uk.co.senab.actionbarpulltorefresh.samples.actionbarcompat.ScrollViewActivity.java
uk.co.senab.actionbarpulltorefresh.samples.actionbarcompat.WebViewActivity.java
uk.co.senab.actionbarpulltorefresh.samples.actionbarsherlock.BaseSampleActivity.java
uk.co.senab.actionbarpulltorefresh.samples.actionbarsherlock.Constants.java
uk.co.senab.actionbarpulltorefresh.samples.actionbarsherlock.FragmentTabPager.java
uk.co.senab.actionbarpulltorefresh.samples.actionbarsherlock.FragmentTabsActivity.java
uk.co.senab.actionbarpulltorefresh.samples.actionbarsherlock.FragmentTabsViewPagerActivity.java
uk.co.senab.actionbarpulltorefresh.samples.actionbarsherlock.GridViewActivity.java
uk.co.senab.actionbarpulltorefresh.samples.actionbarsherlock.ListViewActivity.java
uk.co.senab.actionbarpulltorefresh.samples.actionbarsherlock.MainActivity.java
uk.co.senab.actionbarpulltorefresh.samples.actionbarsherlock.ScrollViewActivity.java
uk.co.senab.actionbarpulltorefresh.samples.actionbarsherlock.WebViewActivity.java
uk.co.senab.actionbarpulltorefresh.samples.stock.BaseSampleActivity.java
uk.co.senab.actionbarpulltorefresh.samples.stock.Constants.java
uk.co.senab.actionbarpulltorefresh.samples.stock.FragmentTabsActivity.java
uk.co.senab.actionbarpulltorefresh.samples.stock.GridViewActivity.java
uk.co.senab.actionbarpulltorefresh.samples.stock.ListViewActivity.java
uk.co.senab.actionbarpulltorefresh.samples.stock.MainActivity.java
uk.co.senab.actionbarpulltorefresh.samples.stock.ScrollViewActivity.java
uk.co.senab.actionbarpulltorefresh.samples.stock.WebViewActivity.java