SpeechOrbView.java revision 9325acc6799fbb0bef84a5ac9b25bfca0701420f
1package android.support.v17.leanback.widget; 2 3import android.content.Context; 4import android.content.res.Resources; 5import android.graphics.Rect; 6import android.graphics.drawable.GradientDrawable; 7import android.support.v17.leanback.R; 8import android.util.AttributeSet; 9import android.view.LayoutInflater; 10import android.view.View; 11import android.widget.FrameLayout; 12import android.widget.ImageView; 13 14public class SpeechOrbView extends FrameLayout implements View.OnClickListener { 15 private OnClickListener mListener; 16 private View mSpeechOrbView; 17 private final float mFocusedZoom; 18 private final float mSoundLevelMaxZoom; 19 private final int mNotRecordingColor; 20 private final int mRecordingColor; 21 private final int mNotRecordingIconColor; 22 private ImageView mIcon; 23 24 private int mCurrentLevel = 0; 25 private boolean mListening = false; 26 27 public SpeechOrbView(Context context) { 28 this(context, null); 29 } 30 31 public SpeechOrbView(Context context, AttributeSet attrs) { 32 this(context, attrs, 0); 33 } 34 35 public SpeechOrbView(Context context, AttributeSet attrs, int defStyle) { 36 super(context, attrs, defStyle); 37 38 LayoutInflater inflater = (LayoutInflater) context 39 .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 40 View root = inflater.inflate(R.layout.lb_speech_orb, this, true); 41 mSpeechOrbView = root.findViewById(R.id.lb_speech_orb); 42 mIcon = (ImageView)root.findViewById(R.id.lb_speech_icon); 43 44 setFocusable(true); 45 setClipChildren(false); 46 47 Resources resources = context.getResources(); 48 mFocusedZoom = 49 resources.getFraction(R.fraction.lb_search_bar_speech_orb_focused_zoom, 1, 1); 50 mSoundLevelMaxZoom = 51 resources.getFraction(R.fraction.lb_search_bar_speech_orb_max_level_zoom, 1, 1); 52 mNotRecordingColor = resources.getColor(R.color.lb_speech_orb_not_recording); 53 mRecordingColor = resources.getColor(R.color.lb_speech_orb_recording); 54 mNotRecordingIconColor = resources.getColor(R.color.lb_speech_orb_not_recording_icon); 55 56 setOnClickListener(this); 57 showNotListening(); 58 } 59 60 @Override 61 public void onClick(View view) { 62 if (null != mListener) { 63 mListener.onClick(view); 64 } 65 } 66 67 @Override 68 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) { 69 super.onFocusChanged(gainFocus, direction, previouslyFocusedRect); 70 final float zoom = gainFocus ? mFocusedZoom : 1f; 71 mSpeechOrbView.animate().scaleX(zoom).scaleY(zoom).setDuration(200).start(); 72 } 73 74 /** 75 * Set the on click listener for the orb 76 * @param listener The listener. 77 */ 78 public void setOnOrbClickedListener(OnClickListener listener) { 79 mListener = listener; 80 } 81 82 public void showListening() { 83 setOrbColor(mRecordingColor); 84 mIcon.setImageResource(R.drawable.lb_ic_search_mic); 85 mIcon.setColorFilter(android.graphics.Color.TRANSPARENT); 86 mSpeechOrbView.setScaleX(1f); 87 mSpeechOrbView.setScaleY(1f); 88 mListening = true; 89 } 90 91 public void showNotListening() { 92 setOrbColor(mNotRecordingColor); 93 mIcon.setImageResource(R.drawable.lb_ic_search_mic_out); 94 mIcon.setColorFilter(mNotRecordingIconColor); 95 mSpeechOrbView.setScaleX(1f); 96 mSpeechOrbView.setScaleY(1f); 97 mListening = false; 98 } 99 100 public void setSoundLevel(int level) { 101 if (!mListening) return; 102 103 // Either ease towards the target level, or decay away from it depending on whether 104 // its higher or lower than the current. 105 if (level > mCurrentLevel) { 106 mCurrentLevel = mCurrentLevel + ((level - mCurrentLevel) / 4); 107 } else { 108 mCurrentLevel = (int) (mCurrentLevel * 0.95f); 109 } 110 111 float zoom = mFocusedZoom + ((mSoundLevelMaxZoom - mFocusedZoom) * mCurrentLevel) / 100; 112 mSpeechOrbView.setScaleX(zoom); 113 mSpeechOrbView.setScaleY(zoom); 114 } 115 116 public void setOrbColor(int color) { 117 if (mSpeechOrbView.getBackground() instanceof GradientDrawable) { 118 ((GradientDrawable) mSpeechOrbView.getBackground()).setColor(color); 119 } 120 } 121 122} 123