Android Open Source - Material Divider Drawable






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.drawable;
//ww w. ja  va 2  s . com
import android.content.res.ColorStateList;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.Drawable;
import android.os.SystemClock;

import com.rey.material.util.ViewUtil;

public class DividerDrawable extends Drawable implements Animatable{
  
  private boolean mRunning = false;
  private long mStartTime;
  private float mAnimProgress;
  private int mAnimDuration;
  
  private Paint mPaint;
  private ColorStateList mColorStateList;
  private int mHeight;
  private int mPrevColor;
  private int mCurColor;
  
  private boolean mEnable = true;
  private PathEffect mPathEffect;    
  private Path mPath;
  
  private boolean mInEditMode = false;
  private boolean mAnimEnable = true;
  
  public DividerDrawable(int height, ColorStateList colorStateList, int animDuration){
    mHeight = height;
    mAnimDuration = animDuration;
    
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeWidth(mHeight);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    
    mPath = new Path();
    
    mAnimEnable = false;
    setColor(colorStateList);
    mAnimEnable = true;
  }
  
  public void setInEditMode(boolean b){
    mInEditMode = b;
  }
  
  public void setAnimEnable(boolean b){
    mAnimEnable = b;
  }
  
  public void setColor(ColorStateList colorStateList){
    mColorStateList = colorStateList;
    onStateChange(getState());
  }
  
  private PathEffect getPathEffect(){
    if(mPathEffect == null)
      mPathEffect = new DashPathEffect(new float[]{0.2f, mHeight * 2}, 0f);
    
    return mPathEffect;
  }
  
  @Override
  public void draw(Canvas canvas) {
    Rect bounds = getBounds();
    float y = bounds.bottom - mHeight / 2;
    
    if(!isRunning()){
      mPath.reset();
      mPath.moveTo(bounds.left, y);
      mPath.lineTo(bounds.right, y);
      mPaint.setPathEffect(mEnable ? null : getPathEffect());
      mPaint.setColor(mCurColor);
      canvas.drawPath(mPath, mPaint);
    }
    else{        
      float start = bounds.exactCenterX() * (1f - mAnimProgress) + bounds.left * mAnimProgress;
      float end = bounds.exactCenterX() * (1f - mAnimProgress) + bounds.right * mAnimProgress;
      
      mPaint.setPathEffect(null);
      
      if(mAnimProgress < 1f){
        mPaint.setColor(mPrevColor);        
        mPath.reset();
        mPath.moveTo(bounds.left, y);
        mPath.lineTo(start, y);
        mPath.moveTo(bounds.right, y);
        mPath.lineTo(end, y);        
        canvas.drawPath(mPath, mPaint);
      }
      
      mPaint.setColor(mCurColor);
      mPath.reset();
      mPath.moveTo(start, y);
      mPath.lineTo(end, y);
      canvas.drawPath(mPath, mPaint);
    }
  }
      
  @Override
  public void setAlpha(int alpha) {
    mPaint.setAlpha(alpha);      
  }

  @Override
  public void setColorFilter(ColorFilter cf) {
    mPaint.setColorFilter(cf);
  }

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

  @Override
  public boolean isStateful() {
    return true;
  }

  @Override
  protected boolean onStateChange(int[] state) {
    mEnable = ViewUtil.hasState(state, android.R.attr.state_enabled);    
    int color = mColorStateList.getColorForState(state, mCurColor);    
        
    if(mCurColor != color){
      if(!mInEditMode && mAnimEnable){
        mPrevColor = isRunning() ? mPrevColor : mCurColor;
        mCurColor = color;  
        start();        
      }
      else{
        mPrevColor = color;
        mCurColor = color;
      }    
      return true;
    }
    else if(!isRunning())
      mPrevColor = color;
      
    return false;
  }

  private void resetAnimation(){  
    mStartTime = SystemClock.uptimeMillis();
    mAnimProgress = 0f;
  }
  
  @Override
  public void start() {
    resetAnimation();      
    scheduleSelf(mUpdater, SystemClock.uptimeMillis() + ViewUtil.FRAME_DURATION);
      invalidateSelf();  
  }

  @Override
  public void stop() {
    mRunning = false;
    unscheduleSelf(mUpdater);
    invalidateSelf();
  }

  @Override
  public boolean isRunning() {
    return mRunning;
  }
  
  @Override
  public void scheduleSelf(Runnable what, long when) {
    mRunning = true;
      super.scheduleSelf(what, when);
  }
  
  private final Runnable mUpdater = new Runnable() {

      @Override
      public void run() {
        update();
      }
        
  };
    
  private void update(){
    long curTime = SystemClock.uptimeMillis();
    mAnimProgress = Math.min(1f, (float)(curTime - mStartTime) / mAnimDuration);  
    
    if(mAnimProgress == 1f)
      mRunning = false;    
        
      if(isRunning())
        scheduleSelf(mUpdater, SystemClock.uptimeMillis() + ViewUtil.FRAME_DURATION);
      
      invalidateSelf();
  }
  
}




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