FileDocCategorySizeDatePackage
SearchOrbView.javaAPI DocAndroid 5.1 API12425Thu Mar 12 22:22:56 GMT 2015android.support.v17.leanback.widget

SearchOrbView

public class SearchOrbView extends android.widget.FrameLayout implements View.OnClickListener

A widget that draws a search affordance, represented by a round background and an icon.

Background color and icon can be customized

Fields Summary
private OnClickListener
mListener
private android.view.View
mRootView
private android.view.View
mSearchOrbView
private android.widget.ImageView
mIcon
private android.graphics.drawable.Drawable
mIconDrawable
private Colors
mColors
private final float
mFocusedZoom
private final int
mPulseDurationMs
private final int
mScaleDurationMs
private final float
mUnfocusedZ
private final float
mFocusedZ
private android.animation.ValueAnimator
mColorAnimator
private final android.animation.ArgbEvaluator
mColorEvaluator
private final ValueAnimator.AnimatorUpdateListener
mUpdateListener
private android.animation.ValueAnimator
mShadowFocusAnimator
private final ValueAnimator.AnimatorUpdateListener
mFocusUpdateListener
Constructors Summary
public SearchOrbView(android.content.Context context)

        this(context, null);
    
public SearchOrbView(android.content.Context context, android.util.AttributeSet attrs)

        this(context, attrs, R.attr.searchOrbViewStyle);
    
public SearchOrbView(android.content.Context context, android.util.AttributeSet attrs, int defStyleAttr)

        super(context, attrs, defStyleAttr);

        final Resources res = context.getResources();

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mRootView = inflater.inflate(getLayoutResourceId(), this, true);
        mSearchOrbView = mRootView.findViewById(R.id.search_orb);
        mIcon = (ImageView) mRootView.findViewById(R.id.icon);

        mFocusedZoom = context.getResources().getFraction(
                R.fraction.lb_search_orb_focused_zoom, 1, 1);
        mPulseDurationMs = context.getResources().getInteger(
                R.integer.lb_search_orb_pulse_duration_ms);
        mScaleDurationMs = context.getResources().getInteger(
                R.integer.lb_search_orb_scale_duration_ms);
        mFocusedZ = context.getResources().getDimensionPixelSize(
                R.dimen.lb_search_orb_focused_z);
        mUnfocusedZ = context.getResources().getDimensionPixelSize(
                R.dimen.lb_search_orb_unfocused_z);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.lbSearchOrbView,
                defStyleAttr, 0);

        Drawable img = a.getDrawable(R.styleable.lbSearchOrbView_searchOrbIcon);
        if (img == null) {
            img = res.getDrawable(R.drawable.lb_ic_in_app_search);
        }
        setOrbIcon(img);

        int defColor = res.getColor(R.color.lb_default_search_color);
        int color = a.getColor(R.styleable.lbSearchOrbView_searchOrbColor, defColor);
        int brightColor = a.getColor(
                R.styleable.lbSearchOrbView_searchOrbBrightColor, color);
        int iconColor = a.getColor(R.styleable.lbSearchOrbView_searchOrbIconColor, Color.TRANSPARENT);
        setOrbColors(new Colors(color, brightColor, iconColor));
        a.recycle();

        setFocusable(true);
        setClipChildren(false);
        setOnClickListener(this);
        setSoundEffectsEnabled(false);
        setSearchOrbZ(0);

        // Icon has no background, but must be on top of the search orb view
        ShadowHelper.getInstance().setZ(mIcon, mFocusedZ);
    
Methods Summary
voidanimateOnFocus(boolean hasFocus)

        final float zoom = hasFocus ? mFocusedZoom : 1f;
        mRootView.animate().scaleX(zoom).scaleY(zoom).setDuration(mScaleDurationMs).start();
        startShadowFocusAnimation(hasFocus, mScaleDurationMs);
        enableOrbColorAnimation(hasFocus);
    
public voidenableOrbColorAnimation(boolean enable)
Enables or disables the orb color animation.

Orb color animation is handled automatically when the orb is focused/unfocused, however, an app may choose to override the current animation state, for example when an activity is paused.

        if (mColorAnimator != null) {
            mColorAnimator.end();
            mColorAnimator = null;
        }
        if (enable) {
            // TODO: set interpolator (material if available)
            mColorAnimator = ValueAnimator.ofObject(mColorEvaluator,
                    mColors.color, mColors.brightColor, mColors.color);
            mColorAnimator.setRepeatCount(ValueAnimator.INFINITE);
            mColorAnimator.setDuration(mPulseDurationMs * 2);
            mColorAnimator.addUpdateListener(mUpdateListener);
            mColorAnimator.start();
        }
    
floatgetFocusedZoom()

        return mFocusedZoom;
    
intgetLayoutResourceId()

        return R.layout.lb_search_orb;
    
public intgetOrbColor()
Returns the orb color

return
the RGBA color

        return mColors.color;
    
public android.support.v17.leanback.widget.SearchOrbView$ColorsgetOrbColors()
Returns the {@link Colors} used to display the search orb.

        return mColors;
    
public android.graphics.drawable.DrawablegetOrbIcon()
Returns the orb icon

return
the drawable used as the icon

        return mIconDrawable;
    
public voidonClick(android.view.View view)

        if (null != mListener) {
            mListener.onClick(view);
        }
    
protected voidonDetachedFromWindow()

        // Must stop infinite animation to prevent activity leak
        enableOrbColorAnimation(false);
        super.onDetachedFromWindow();
    
protected voidonFocusChanged(boolean gainFocus, int direction, android.graphics.Rect previouslyFocusedRect)

        super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
        animateOnFocus(gainFocus);
    
voidscaleOrbViewOnly(float scale)

        mSearchOrbView.setScaleX(scale);
        mSearchOrbView.setScaleY(scale);
    
public voidsetOnOrbClickedListener(OnClickListener listener)
Set the on click listener for the orb

param
listener The listener.

        mListener = listener;
        if (null != listener) {
            setVisibility(View.VISIBLE);
        } else {
            setVisibility(View.INVISIBLE);
        }
    
public voidsetOrbColor(int color)
Sets the background color of the search orb. Other colors will be provided by the framework.

param
color the RGBA color

        setOrbColors(new Colors(color, color, Color.TRANSPARENT));
    
public voidsetOrbColor(int color, int brightColor)
Sets the search orb colors. Other colors are provided by the framework.

deprecated
Use {@link #setOrbColors(Colors)} instead.

        setOrbColors(new Colors(color, brightColor, Color.TRANSPARENT));
    
public voidsetOrbColors(android.support.v17.leanback.widget.SearchOrbView$Colors colors)
Set the {@link Colors} used to display the search orb.

        mColors = colors;
        mIcon.setColorFilter(mColors.iconColor);

        if (mColorAnimator == null) {
            setOrbViewColor(mColors.color);
        } else {
            enableOrbColorAnimation(true);
        }
    
public voidsetOrbIcon(android.graphics.drawable.Drawable icon)
Set the orb icon

param
icon the drawable to be used as the icon

        mIconDrawable = icon;
        mIcon.setImageDrawable(mIconDrawable);
    
private voidsetOrbViewColor(int color)

        if (mSearchOrbView.getBackground() instanceof GradientDrawable) {
            ((GradientDrawable) mSearchOrbView.getBackground()).setColor(color);
        }
    
private voidsetSearchOrbZ(float fraction)


        
        ShadowHelper.getInstance().setZ(mSearchOrbView,
                mUnfocusedZ + fraction * (mFocusedZ - mUnfocusedZ));
    
private voidstartShadowFocusAnimation(boolean gainFocus, int duration)

        if (mShadowFocusAnimator == null) {
            mShadowFocusAnimator = ValueAnimator.ofFloat(0f, 1f);
            mShadowFocusAnimator.addUpdateListener(mFocusUpdateListener);
        }
        if (gainFocus) {
            mShadowFocusAnimator.start();
        } else {
            mShadowFocusAnimator.reverse();
        }
        mShadowFocusAnimator.setDuration(duration);