SearchOrbViewpublic class SearchOrbView extends android.widget.FrameLayout implements View.OnClickListenerA 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 |
---|
void | animateOnFocus(boolean hasFocus)
final float zoom = hasFocus ? mFocusedZoom : 1f;
mRootView.animate().scaleX(zoom).scaleY(zoom).setDuration(mScaleDurationMs).start();
startShadowFocusAnimation(hasFocus, mScaleDurationMs);
enableOrbColorAnimation(hasFocus);
| public void | enableOrbColorAnimation(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();
}
| float | getFocusedZoom()
return mFocusedZoom;
| int | getLayoutResourceId()
return R.layout.lb_search_orb;
| public int | getOrbColor()Returns the orb color
return mColors.color;
| public android.support.v17.leanback.widget.SearchOrbView$Colors | getOrbColors()Returns the {@link Colors} used to display the search orb.
return mColors;
| public android.graphics.drawable.Drawable | getOrbIcon()Returns the orb icon
return mIconDrawable;
| public void | onClick(android.view.View view)
if (null != mListener) {
mListener.onClick(view);
}
| protected void | onDetachedFromWindow()
// Must stop infinite animation to prevent activity leak
enableOrbColorAnimation(false);
super.onDetachedFromWindow();
| protected void | onFocusChanged(boolean gainFocus, int direction, android.graphics.Rect previouslyFocusedRect)
super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
animateOnFocus(gainFocus);
| void | scaleOrbViewOnly(float scale)
mSearchOrbView.setScaleX(scale);
mSearchOrbView.setScaleY(scale);
| public void | setOnOrbClickedListener(OnClickListener listener)Set the on click listener for the orb
mListener = listener;
if (null != listener) {
setVisibility(View.VISIBLE);
} else {
setVisibility(View.INVISIBLE);
}
| public void | setOrbColor(int color)Sets the background color of the search orb.
Other colors will be provided by the framework.
setOrbColors(new Colors(color, color, Color.TRANSPARENT));
| public void | setOrbColor(int color, int brightColor)Sets the search orb colors.
Other colors are provided by the framework.
setOrbColors(new Colors(color, brightColor, Color.TRANSPARENT));
| public void | setOrbColors(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 void | setOrbIcon(android.graphics.drawable.Drawable icon)Set the orb icon
mIconDrawable = icon;
mIcon.setImageDrawable(mIconDrawable);
| private void | setOrbViewColor(int color)
if (mSearchOrbView.getBackground() instanceof GradientDrawable) {
((GradientDrawable) mSearchOrbView.getBackground()).setColor(color);
}
| private void | setSearchOrbZ(float fraction)
ShadowHelper.getInstance().setZ(mSearchOrbView,
mUnfocusedZ + fraction * (mFocusedZ - mUnfocusedZ));
| private void | startShadowFocusAnimation(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);
|
|