Android Open Source - Material Navigation Drawer 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;
//w w  w  . j  a v  a 2 s.  com
import com.rey.material.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;

public class NavigationDrawerDrawable extends Drawable implements Drawable.Callback{

  private ToolbarRippleDrawable mRippleDrawable;
  private LineMorphingDrawable mLineDrawable;
    
  public static final int STATE_DRAWER = 0;
  public static final int STATE_ARROW = 1;
  
  public NavigationDrawerDrawable(ToolbarRippleDrawable rippleDrawable, LineMorphingDrawable lineDrawable){
    mRippleDrawable = rippleDrawable;
    mLineDrawable = lineDrawable;
    
    mRippleDrawable.setCallback(this);
    mLineDrawable.setCallback(this);
  }
  
  public void switchIconState(boolean animation){
    mLineDrawable.switchLineState((getIconState() + 1) % 2, animation);
  }
  
  public int getIconState(){
    return mLineDrawable.getLineState();
  }
  
  public boolean setIconState(int state, float progress){
    return mLineDrawable.setLineState(state, progress);
  }
  
  @Override
  public void draw(Canvas canvas) {
    mRippleDrawable.draw(canvas);
    mLineDrawable.draw(canvas);
  }

  @Override
  public void setAlpha(int alpha) {
    mRippleDrawable.setAlpha(alpha);
    mLineDrawable.setAlpha(alpha);
  }

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

  @Override
  public int getOpacity() {
    return PixelFormat.TRANSLUCENT;
  }
  
  @Override
  public void setBounds(int left, int top, int right, int bottom) {
    mRippleDrawable.setBounds(left, top, right, bottom);
    mLineDrawable.setBounds(left, top, right, bottom);
  }
  
  @Override
  public void setDither(boolean dither) {
    mRippleDrawable.setDither(dither);
    mLineDrawable.setDither(dither);
  }
  
  @Override
  public void invalidateDrawable(Drawable who) {
    invalidateSelf();
  }

  @Override
  public void scheduleDrawable(Drawable who, Runnable what, long when) {
    scheduleSelf(what, when);
  }

  @Override
  public void unscheduleDrawable(Drawable who, Runnable what) {
    unscheduleSelf(what);
  }
  
  @Override
  public boolean isStateful() {
    return true;    
  }
  
  @Override
  protected boolean onStateChange(int[] state) {    
    return mRippleDrawable.onStateChange(state);
  }
  
  public static class Builder{
    private ToolbarRippleDrawable mRippleDrawable;
    private LineMorphingDrawable mLineDrawable;
    
    public Builder(){}
    
    public Builder(Context context, AttributeSet attrs, int defStyle){
      TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.NavigationDrawerDrawable, 0, defStyle);
            
      if(a != null){
        int rippleId = a.getResourceId(R.styleable.NavigationDrawerDrawable_nd_ripple, 0);
        int lineId = a.getResourceId(R.styleable.NavigationDrawerDrawable_nd_icon, 0);
          
        if(rippleId > 0)
          ripple(new ToolbarRippleDrawable.Builder(context, null, rippleId).build());
        
        if(lineId > 0){
          LineMorphingDrawable.Builder builder = new LineMorphingDrawable.Builder(context, null, lineId);
          
          builder.states(new LineMorphingDrawable.State(new float[]{0f, 0.1f, 1f, 0.1f, 0f, 0.5f, 1f, 0.5f, 0f, 0.9f, 1f, 0.9f}, null), new LineMorphingDrawable.State(new float[]{0.5f, 0f, 1f, 0.5f, 0f, 0.5f, 1f, 0.5f, 0.5f, 1f, 1f, 0.5f}, new int[]{0, 2}));
          
          line(builder.build());
        }
            
        a.recycle();
      }    
    }
    
    public NavigationDrawerDrawable build(){
      return new NavigationDrawerDrawable(mRippleDrawable, mLineDrawable);
    }
    
    public Builder ripple(ToolbarRippleDrawable drawable){
      mRippleDrawable = drawable;
      
      return this;
    }
    
    public Builder line(LineMorphingDrawable drawable){
      mLineDrawable = drawable;
      
      return this;
    }
    
  }
}




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