SearchOrbView.java revision 4cf79b1c4d38a190317961891f9fd052836710fd
1/* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.support.v17.leanback.widget; 18 19 20import android.content.Context; 21import android.graphics.Rect; 22import android.graphics.drawable.GradientDrawable; 23import android.support.v17.leanback.R; 24import android.util.AttributeSet; 25import android.view.LayoutInflater; 26import android.view.View; 27import android.widget.FrameLayout; 28 29public class SearchOrbView extends FrameLayout implements View.OnClickListener { 30 private OnClickListener mListener; 31 private View mSearchOrbView; 32 private final float mFocusedZoom; 33 34 public SearchOrbView(Context context) { 35 this(context, null); 36 } 37 38 public SearchOrbView(Context context, AttributeSet attrs) { 39 this(context, attrs, 0); 40 } 41 42 public SearchOrbView(Context context, AttributeSet attrs, int defStyle) { 43 super(context, attrs, defStyle); 44 45 LayoutInflater inflater = (LayoutInflater) context 46 .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 47 View root = inflater.inflate(R.layout.lb_search_orb, this, true); 48 mSearchOrbView = root.findViewById(R.id.search_orb); 49 50 // By default we are not visible 51 setVisibility(INVISIBLE); 52 setFocusable(true); 53 setClipChildren(false); 54 55 mFocusedZoom = context.getResources().getFraction( 56 R.fraction.lb_search_orb_focused_zoom, 1, 1); 57 58 setOnClickListener(this); 59 } 60 61 @Override 62 public void onClick(View view) { 63 if (null != mListener) { 64 mListener.onClick(view); 65 } 66 } 67 68 @Override 69 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) { 70 super.onFocusChanged(gainFocus, direction, previouslyFocusedRect); 71 final float zoom = gainFocus ? mFocusedZoom : 1f; 72 mSearchOrbView.animate().scaleX(zoom).scaleY(zoom).setDuration(200).start(); 73 } 74 75 /** 76 * Set the on click listener for the orb 77 * @param listener The listener. 78 */ 79 public void setOnOrbClickedListener(OnClickListener listener) { 80 mListener = listener; 81 if (null != listener) { 82 setVisibility(View.VISIBLE); 83 } else { 84 setVisibility(View.INVISIBLE); 85 } 86 } 87 88 public void setOrbColor(int color) { 89 if (mSearchOrbView.getBackground() instanceof GradientDrawable) { 90 ((GradientDrawable) mSearchOrbView.getBackground()).setColor(color); 91 } 92 } 93} 94