Android Open Source - Material Progress View






From Project

Back to project page Material.

License

The source code is released under:

Apache License

If you think the Android project Material 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 com.rey.material.view;
/*w w  w.ja  va 2s . com*/
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.view.View;

import com.rey.material.R;
import com.rey.material.drawable.CircularProgressDrawable;
import com.rey.material.drawable.LinearProgressDrawable;

public class ProgressView extends View {

  private boolean mAutostart;
  private boolean mCircular;
  private int mProgressId;
  
  public static final int MODE_DETERMINATE = 0;
  public static final int MODE_INDETERMINATE = 1;
  public static final int MODE_BUFFER = 2;
  public static final int MODE_QUERY = 3;
  
  
  private Drawable mProgressDrawable;
  
  public ProgressView(Context context) {
    this(context, null, 0);
  }

  public ProgressView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  @SuppressWarnings("deprecation")
  @SuppressLint("NewApi")
  public ProgressView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ProgressView, 0, defStyle);
    mAutostart = a.getBoolean(R.styleable.ProgressView_pv_autostart, true);
    mCircular = a.getBoolean(R.styleable.ProgressView_pv_circular, true);
    mProgressId = a.getResourceId(R.styleable.ProgressView_pv_progressStyle, 0);  
    
    a.recycle();
    
    if(mProgressId > 0){
      if(mCircular)
        mProgressDrawable = new CircularProgressDrawable.Builder(context, attrs, mProgressId).build();
      else
        mProgressDrawable = new LinearProgressDrawable.Builder(context, attrs, mProgressId).build();
            
      if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN)
        setBackground(mProgressDrawable);
      else
        setBackgroundDrawable(mProgressDrawable);
    }
  }
  
  @Override
    public void setVisibility(int v) {
        if(getVisibility() != v) {
            super.setVisibility(v);

            if (getProgressMode() == MODE_INDETERMINATE && mAutostart) {
                if (v == GONE || v == INVISIBLE)
                  stop();
                else
                  start();
            }
        }
    }
  
  @Override
  protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
    super.onVisibilityChanged(changedView, visibility);

      if (getProgressMode() == MODE_INDETERMINATE && mAutostart) {
        if (visibility == GONE || visibility == INVISIBLE)
          stop();
            else
               start();
        }
  }
  
  @Override
  protected void onAttachedToWindow() {
    super.onAttachedToWindow();
      if (getProgressMode() == MODE_INDETERMINATE && mAutostart) 
        start();
  }

  @Override
  protected void onDetachedFromWindow() {
    if (getProgressMode() == MODE_INDETERMINATE && mAutostart) 
        stop();
    
      super.onDetachedFromWindow();
  }
   
  public int getProgressMode(){
    if(mCircular)
      return ((CircularProgressDrawable)mProgressDrawable).getProgressMode();
    else
      return ((LinearProgressDrawable)mProgressDrawable).getProgressMode();
  }
  
  public float getProgress(){
    if(mCircular)
      return ((CircularProgressDrawable)mProgressDrawable).getProgress();
    else
      return ((LinearProgressDrawable)mProgressDrawable).getProgress();
  }
  
  public float getSecondaryProgress(){
    if(mCircular)
      return ((CircularProgressDrawable)mProgressDrawable).getSecondaryProgress();
    else
      return ((LinearProgressDrawable)mProgressDrawable).getSecondaryProgress();
  }
  
  public void setProgress(float percent){
    if(mCircular)
      ((CircularProgressDrawable)mProgressDrawable).setProgress(percent);
    else
      ((LinearProgressDrawable)mProgressDrawable).setProgress(percent);
  }
  
  public void setSecondaryProgress(float percent){
    if(mCircular)
      ((CircularProgressDrawable)mProgressDrawable).setSecondaryProgress(percent);
    else
      ((LinearProgressDrawable)mProgressDrawable).setSecondaryProgress(percent);
  }
  
  public void start(){
    if(mProgressDrawable != null)
      ((Animatable)mProgressDrawable).start();
  }
  
  public void stop(){
    if(mProgressDrawable != null)
      ((Animatable)mProgressDrawable).stop();
  }

}




Java Source Code List

com.rey.material.ApplicationTest.java
com.rey.material.demo.ButtonFragment.java
com.rey.material.demo.MainActivity.java
com.rey.material.demo.ProgressFragment.java
com.rey.material.demo.SnackbarFragment.java
com.rey.material.demo.SwitchesFragment.java
com.rey.material.demo.TextfieldFragment.java
com.rey.material.drawable.ArrowDrawable.java
com.rey.material.drawable.BlankDrawable.java
com.rey.material.drawable.CheckBoxDrawable.java
com.rey.material.drawable.CircularProgressDrawable.java
com.rey.material.drawable.DividerDrawable.java
com.rey.material.drawable.LineMorphingDrawable.java
com.rey.material.drawable.LinearProgressDrawable.java
com.rey.material.drawable.NavigationDrawerDrawable.java
com.rey.material.drawable.RadioButtonDrawable.java
com.rey.material.drawable.RevealDrawable.java
com.rey.material.drawable.RippleDrawable.java
com.rey.material.drawable.ToolbarRippleDrawable.java
com.rey.material.util.ColorUtil.java
com.rey.material.util.ThemeUtil.java
com.rey.material.util.ViewUtil.java
com.rey.material.view.Button.java
com.rey.material.view.CheckBox.java
com.rey.material.view.CheckedTextView.java
com.rey.material.view.CompoundButton.java
com.rey.material.view.EditText.java
com.rey.material.view.FloatingActionButton.java
com.rey.material.view.ListPopupWindow.java
com.rey.material.view.ListView.java
com.rey.material.view.PopupWindow.java
com.rey.material.view.ProgressView.java
com.rey.material.view.RadioButton.java
com.rey.material.view.RippleManager.java
com.rey.material.view.SnackBar.java
com.rey.material.view.Spinner.java
com.rey.material.view.Switch.java
com.rey.material.view.TabPageIndicator.java
com.rey.material.view.TextView.java