FileDocCategorySizeDatePackage
KeyButtonRipple.javaAPI DocAndroid 5.1 API11757Thu Mar 12 22:22:42 GMT 2015com.android.systemui.statusbar.policy

KeyButtonRipple

public class KeyButtonRipple extends android.graphics.drawable.Drawable

Fields Summary
private static final float
GLOW_MAX_SCALE_FACTOR
private static final float
GLOW_MAX_ALPHA
private static final int
ANIMATION_DURATION_SCALE
private static final int
ANIMATION_DURATION_FADE
private android.graphics.Paint
mRipplePaint
private android.graphics.CanvasProperty
mLeftProp
private android.graphics.CanvasProperty
mTopProp
private android.graphics.CanvasProperty
mRightProp
private android.graphics.CanvasProperty
mBottomProp
private android.graphics.CanvasProperty
mRxProp
private android.graphics.CanvasProperty
mRyProp
private android.graphics.CanvasProperty
mPaintProp
private float
mGlowAlpha
private float
mGlowScale
private boolean
mPressed
private boolean
mDrawingHardwareGlow
private int
mMaxWidth
private final android.view.animation.Interpolator
mInterpolator
private final android.view.animation.Interpolator
mAlphaExitInterpolator
private boolean
mSupportHardware
private final android.view.View
mTargetView
private final HashSet
mRunningAnimations
private final ArrayList
mTmpArray
private final android.animation.AnimatorListenerAdapter
mAnimatorListener
Constructors Summary
public KeyButtonRipple(android.content.Context ctx, android.view.View targetView)


         
        mMaxWidth =  ctx.getResources().getDimensionPixelSize(R.dimen.key_button_ripple_max_width);
        mTargetView = targetView;
    
Methods Summary
private voidcancelAnimations()

        mTmpArray.addAll(mRunningAnimations);
        int size = mTmpArray.size();
        for (int i = 0; i < size; i++) {
            Animator a = mTmpArray.get(i);
            a.cancel();
        }
        mTmpArray.clear();
        mRunningAnimations.clear();
    
public voiddraw(android.graphics.Canvas canvas)

        mSupportHardware = canvas.isHardwareAccelerated();
        if (mSupportHardware) {
            drawHardware((HardwareCanvas) canvas);
        } else {
            drawSoftware(canvas);
        }
    
private voiddrawHardware(android.view.HardwareCanvas c)

        if (mDrawingHardwareGlow) {
            c.drawRoundRect(mLeftProp, mTopProp, mRightProp, mBottomProp, mRxProp, mRyProp,
                    mPaintProp);
        }
    
private voiddrawSoftware(android.graphics.Canvas canvas)

        if (mGlowAlpha > 0f) {
            final Paint p = getRipplePaint();
            p.setAlpha((int)(mGlowAlpha * 255f));

            final float w = getBounds().width();
            final float h = getBounds().height();
            final boolean horizontal = w > h;
            final float diameter = getRippleSize() * mGlowScale;
            final float radius = diameter * .5f;
            final float cx = w * .5f;
            final float cy = h * .5f;
            final float rx = horizontal ? radius : cx;
            final float ry = horizontal ? cy : radius;
            final float corner = horizontal ? cy : cx;

            canvas.drawRoundRect(cx - rx, cy - ry,
                    cx + rx, cy + ry,
                    corner, corner, p);
        }
    
private voidenterHardware()

        cancelAnimations();
        mDrawingHardwareGlow = true;
        setExtendStart(CanvasProperty.createFloat(getExtendSize() / 2));
        final RenderNodeAnimator startAnim = new RenderNodeAnimator(getExtendStart(),
                getExtendSize()/2 - GLOW_MAX_SCALE_FACTOR * getRippleSize()/2);
        startAnim.setDuration(ANIMATION_DURATION_SCALE);
        startAnim.setInterpolator(mInterpolator);
        startAnim.addListener(mAnimatorListener);
        startAnim.setTarget(mTargetView);

        setExtendEnd(CanvasProperty.createFloat(getExtendSize() / 2));
        final RenderNodeAnimator endAnim = new RenderNodeAnimator(getExtendEnd(),
                getExtendSize()/2 + GLOW_MAX_SCALE_FACTOR * getRippleSize()/2);
        endAnim.setDuration(ANIMATION_DURATION_SCALE);
        endAnim.setInterpolator(mInterpolator);
        endAnim.addListener(mAnimatorListener);
        endAnim.setTarget(mTargetView);

        if (isHorizontal()) {
            mTopProp = CanvasProperty.createFloat(0f);
            mBottomProp = CanvasProperty.createFloat(getBounds().height());
            mRxProp = CanvasProperty.createFloat(getBounds().height()/2);
            mRyProp = CanvasProperty.createFloat(getBounds().height()/2);
        } else {
            mLeftProp = CanvasProperty.createFloat(0f);
            mRightProp = CanvasProperty.createFloat(getBounds().width());
            mRxProp = CanvasProperty.createFloat(getBounds().width()/2);
            mRyProp = CanvasProperty.createFloat(getBounds().width()/2);
        }

        mGlowScale = GLOW_MAX_SCALE_FACTOR;
        mGlowAlpha = GLOW_MAX_ALPHA;
        mRipplePaint = getRipplePaint();
        mRipplePaint.setAlpha((int) (mGlowAlpha * 255));
        mPaintProp = CanvasProperty.createPaint(mRipplePaint);

        startAnim.start();
        endAnim.start();
        mRunningAnimations.add(startAnim);
        mRunningAnimations.add(endAnim);

        invalidateSelf();
    
private voidenterSoftware()

        cancelAnimations();
        mGlowAlpha = GLOW_MAX_ALPHA;
        ObjectAnimator scaleAnimator = ObjectAnimator.ofFloat(this, "glowScale",
                0f, GLOW_MAX_SCALE_FACTOR);
        scaleAnimator.setInterpolator(mInterpolator);
        scaleAnimator.setDuration(ANIMATION_DURATION_SCALE);
        scaleAnimator.addListener(mAnimatorListener);
        scaleAnimator.start();
        mRunningAnimations.add(scaleAnimator);
    
private voidexitHardware()

        mPaintProp = CanvasProperty.createPaint(getRipplePaint());
        final RenderNodeAnimator opacityAnim = new RenderNodeAnimator(mPaintProp,
                RenderNodeAnimator.PAINT_ALPHA, 0);
        opacityAnim.setDuration(ANIMATION_DURATION_FADE);
        opacityAnim.setInterpolator(mAlphaExitInterpolator);
        opacityAnim.addListener(mAnimatorListener);
        opacityAnim.setTarget(mTargetView);

        opacityAnim.start();
        mRunningAnimations.add(opacityAnim);

        invalidateSelf();
    
private voidexitSoftware()

        ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(this, "glowAlpha", mGlowAlpha, 0f);
        alphaAnimator.setInterpolator(mAlphaExitInterpolator);
        alphaAnimator.setDuration(ANIMATION_DURATION_FADE);
        alphaAnimator.addListener(mAnimatorListener);
        alphaAnimator.start();
        mRunningAnimations.add(alphaAnimator);
    
private android.graphics.CanvasPropertygetExtendEnd()

        return isHorizontal() ? mRightProp : mBottomProp;
    
private intgetExtendSize()

        return isHorizontal() ? getBounds().width() : getBounds().height();
    
private android.graphics.CanvasPropertygetExtendStart()

        return isHorizontal() ? mLeftProp : mTopProp;
    
public floatgetGlowAlpha()

        return mGlowAlpha;
    
public floatgetGlowScale()

        return mGlowScale;
    
public intgetOpacity()

        return PixelFormat.TRANSLUCENT;
    
private android.graphics.PaintgetRipplePaint()

        if (mRipplePaint == null) {
            mRipplePaint = new Paint();
            mRipplePaint.setAntiAlias(true);
            mRipplePaint.setColor(0xffffffff);
        }
        return mRipplePaint;
    
private intgetRippleSize()

        int size = isHorizontal() ? getBounds().width() : getBounds().height();
        return Math.min(size, mMaxWidth);
    
private booleanisHorizontal()

        return getBounds().width() > getBounds().height();
    
public booleanisStateful()

        return true;
    
public voidjumpToCurrentState()

        cancelAnimations();
    
protected booleanonStateChange(int[] state)

        boolean pressed = false;
        for (int i = 0; i < state.length; i++) {
            if (state[i] == android.R.attr.state_pressed) {
                pressed = true;
                break;
            }
        }
        if (pressed != mPressed) {
            setPressed(pressed);
            mPressed = pressed;
            return true;
        } else {
            return false;
        }
    
public voidsetAlpha(int alpha)

        // Not supported.
    
public voidsetColorFilter(android.graphics.ColorFilter cf)

        // Not supported.
    
private voidsetExtendEnd(android.graphics.CanvasProperty prop)
Sets the right/bottom property for the round rect to {@code prop} depending on whether we are horizontal or vertical mode.

        if (isHorizontal()) {
            mRightProp = prop;
        } else {
            mBottomProp = prop;
        }
    
private voidsetExtendStart(android.graphics.CanvasProperty prop)
Sets the left/top property for the round rect to {@code prop} depending on whether we are horizontal or vertical mode.

        if (isHorizontal()) {
            mLeftProp = prop;
        } else {
            mTopProp = prop;
        }
    
public voidsetGlowAlpha(float x)

        mGlowAlpha = x;
        invalidateSelf();
    
public voidsetGlowScale(float x)

        mGlowScale = x;
        invalidateSelf();
    
public voidsetPressed(boolean pressed)

        if (mSupportHardware) {
            setPressedHardware(pressed);
        } else {
            setPressedSoftware(pressed);
        }
    
private voidsetPressedHardware(boolean pressed)

        if (pressed) {
            enterHardware();
        } else {
            exitHardware();
        }
    
private voidsetPressedSoftware(boolean pressed)

        if (pressed) {
            enterSoftware();
        } else {
            exitSoftware();
        }