FileDocCategorySizeDatePackage
DrawerArrowDrawable.javaAPI DocAndroid 5.1 API7738Thu Mar 12 22:22:56 GMT 2015android.support.v7.app

DrawerArrowDrawable

public abstract class DrawerArrowDrawable extends android.graphics.drawable.Drawable
A drawable that can draw a "Drawer hamburger" menu or an Arrow and animate between them.

Fields Summary
private final android.graphics.Paint
mPaint
private static final float
ARROW_HEAD_ANGLE
private final float
mBarThickness
private final float
mTopBottomArrowSize
private final float
mBarSize
private final float
mMiddleArrowSize
private final float
mBarGap
private final boolean
mSpin
private final android.graphics.Path
mPath
private final int
mSize
private boolean
mVerticalMirror
private float
mProgress
private float
mMaxCutForBarSize
private float
mCenterOffset
Constructors Summary
DrawerArrowDrawable(android.content.Context context)

param
context used to get the configuration for the drawable from


                    
      
        final TypedArray typedArray = context.getTheme()
                .obtainStyledAttributes(null, R.styleable.DrawerArrowToggle,
                        R.attr.drawerArrowStyle,
                        R.style.Base_Widget_AppCompat_DrawerArrowToggle);
        mPaint.setAntiAlias(true);
        mPaint.setColor(typedArray.getColor(R.styleable.DrawerArrowToggle_color, 0));
        mSize = typedArray.getDimensionPixelSize(R.styleable.DrawerArrowToggle_drawableSize, 0);
        // round this because having this floating may cause bad measurements
        mBarSize = Math.round(typedArray.getDimension(R.styleable.DrawerArrowToggle_barSize, 0));
        // round this because having this floating may cause bad measurements
        mTopBottomArrowSize = Math.round(typedArray.getDimension(
                R.styleable.DrawerArrowToggle_topBottomBarArrowSize, 0));
        mBarThickness = typedArray.getDimension(R.styleable.DrawerArrowToggle_thickness, 0);
        // round this because having this floating may cause bad measurements
        mBarGap = Math.round(typedArray.getDimension(
                R.styleable.DrawerArrowToggle_gapBetweenBars, 0));
        mSpin = typedArray.getBoolean(R.styleable.DrawerArrowToggle_spinBars, true);
        mMiddleArrowSize = typedArray
                .getDimension(R.styleable.DrawerArrowToggle_middleBarArrowSize, 0);
        final int remainingSpace = (int) (mSize - mBarThickness * 3 - mBarGap * 2);
        mCenterOffset = (remainingSpace / 4) * 2; //making sure it is a multiple of 2.
        mCenterOffset += mBarThickness * 1.5 + mBarGap;
        typedArray.recycle();

        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.MITER);
        mPaint.setStrokeCap(Paint.Cap.BUTT);
        mPaint.setStrokeWidth(mBarThickness);

        mMaxCutForBarSize = (float) (mBarThickness / 2 * Math.cos(ARROW_HEAD_ANGLE));
    
Methods Summary
public voiddraw(android.graphics.Canvas canvas)

        Rect bounds = getBounds();
        final boolean isRtl = isLayoutRtl();
        // Interpolated widths of arrow bars
        final float arrowSize = lerp(mBarSize, mTopBottomArrowSize, mProgress);
        final float middleBarSize = lerp(mBarSize, mMiddleArrowSize, mProgress);
        // Interpolated size of middle bar
        final float middleBarCut = Math.round(lerp(0, mMaxCutForBarSize, mProgress));
        // The rotation of the top and bottom bars (that make the arrow head)
        final float rotation = lerp(0, ARROW_HEAD_ANGLE, mProgress);

        // The whole canvas rotates as the transition happens
        final float canvasRotate = lerp(isRtl ? 0 : -180, isRtl ? 180 : 0, mProgress);
        final float arrowWidth = Math.round(arrowSize * Math.cos(rotation));
        final float arrowHeight = Math.round(arrowSize * Math.sin(rotation));


        mPath.rewind();
        final float topBottomBarOffset = lerp(mBarGap + mBarThickness, -mMaxCutForBarSize,
                mProgress);

        final float arrowEdge = -middleBarSize / 2;
        // draw middle bar
        mPath.moveTo(arrowEdge + middleBarCut, 0);
        mPath.rLineTo(middleBarSize - middleBarCut * 2, 0);

        // bottom bar
        mPath.moveTo(arrowEdge, topBottomBarOffset);
        mPath.rLineTo(arrowWidth, arrowHeight);

        // top bar
        mPath.moveTo(arrowEdge, -topBottomBarOffset);
        mPath.rLineTo(arrowWidth, -arrowHeight);

        mPath.close();

        canvas.save();
        // Rotate the whole canvas if spinning, if not, rotate it 180 to get
        // the arrow pointing the other way for RTL.
        canvas.translate(bounds.centerX(), mCenterOffset);
        if (mSpin) {
            canvas.rotate(canvasRotate * ((mVerticalMirror ^ isRtl) ? -1 : 1));
        } else if (isRtl) {
            canvas.rotate(180);
        }
        canvas.drawPath(mPath, mPaint);

        canvas.restore();
    
public intgetIntrinsicHeight()

        return mSize;
    
public intgetIntrinsicWidth()

        return mSize;
    
public intgetOpacity()

        return PixelFormat.TRANSLUCENT;
    
public floatgetProgress()

        return mProgress;
    
public booleanisAutoMirrored()

        // Draws rotated 180 degrees in RTL mode.
        return true;
    
abstract booleanisLayoutRtl()

private static floatlerp(float a, float b, float t)
Linear interpolate between a and b with parameter t.

        return a + (b - a) * t;
    
public voidsetAlpha(int i)

        mPaint.setAlpha(i);
    
public voidsetColorFilter(android.graphics.ColorFilter colorFilter)

        mPaint.setColorFilter(colorFilter);
    
public voidsetProgress(float progress)

        mProgress = progress;
        invalidateSelf();
    
protected voidsetVerticalMirror(boolean verticalMirror)
If set, canvas is flipped when progress reached to end and going back to start.

        mVerticalMirror = verticalMirror;