public class

SpeechOrbView

extends SearchOrbView

 java.lang.Object

↳FrameLayout

androidx.leanback.widget.SearchOrbView

↳androidx.leanback.widget.SpeechOrbView

Gradle dependencies

compile group: 'androidx.leanback', name: 'leanback', version: '1.2.0-alpha02'

  • groupId: androidx.leanback
  • artifactId: leanback
  • version: 1.2.0-alpha02

Artifact androidx.leanback:leanback:1.2.0-alpha02 it located at Google repository (https://maven.google.com/)

Androidx artifact mapping:

androidx.leanback:leanback com.android.support:leanback-v17

Androidx class mapping:

androidx.leanback.widget.SpeechOrbView android.support.v17.leanback.widget.SpeechOrbView

Overview

A subclass of SearchOrbView that visualizes the state of an ongoing speech recognition.

Summary

Constructors
publicSpeechOrbView(Context context)

publicSpeechOrbView(Context context, AttributeSet attrs)

publicSpeechOrbView(Context context, AttributeSet attrs, int defStyle)

Methods
public voidsetListeningOrbColors(SearchOrbView.Colors colors)

Sets default listening state orb color.

public voidsetNotListeningOrbColors(SearchOrbView.Colors colors)

Sets default not-listening state orb color.

public voidsetSoundLevel(int level)

Sets the sound level while listening to speech.

public voidshowListening()

Sets the view to display listening state.

public voidshowNotListening()

Sets the view to display the not-listening state.

from SearchOrbViewenableOrbColorAnimation, getOrbColor, getOrbColors, getOrbIcon, onAttachedToWindow, onClick, onDetachedFromWindow, onFocusChanged, setOnOrbClickedListener, setOrbColor, setOrbColor, setOrbColors, setOrbIcon
from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public SpeechOrbView(Context context)

public SpeechOrbView(Context context, AttributeSet attrs)

public SpeechOrbView(Context context, AttributeSet attrs, int defStyle)

Methods

public void setListeningOrbColors(SearchOrbView.Colors colors)

Sets default listening state orb color.

Parameters:

colors: SearchOrbView.Colors.

public void setNotListeningOrbColors(SearchOrbView.Colors colors)

Sets default not-listening state orb color.

Parameters:

colors: SearchOrbView.Colors.

public void showListening()

Sets the view to display listening state.

public void showNotListening()

Sets the view to display the not-listening state.

public void setSoundLevel(int level)

Sets the sound level while listening to speech.

Source

package androidx.leanback.widget;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.util.AttributeSet;

import androidx.leanback.R;

/**
 * A subclass of {@link SearchOrbView} that visualizes the state of an ongoing speech recognition.
 */
public class SpeechOrbView extends SearchOrbView {
    private final float mSoundLevelMaxZoom;
    private Colors mListeningOrbColors;
    private Colors mNotListeningOrbColors;

    private int mCurrentLevel = 0;
    private boolean mListening = false;

    public SpeechOrbView(Context context) {
        this(context, null);
    }

    public SpeechOrbView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SpeechOrbView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        Resources resources = context.getResources();
        mSoundLevelMaxZoom =
                resources.getFraction(R.fraction.lb_search_bar_speech_orb_max_level_zoom, 1, 1);

        mNotListeningOrbColors = new Colors(resources.getColor(R.color.lb_speech_orb_not_recording),
                resources.getColor(R.color.lb_speech_orb_not_recording_pulsed),
                resources.getColor(R.color.lb_speech_orb_not_recording_icon));
        mListeningOrbColors = new Colors(resources.getColor(R.color.lb_speech_orb_recording),
                resources.getColor(R.color.lb_speech_orb_recording),
                Color.TRANSPARENT);

        showNotListening();
    }

    @Override
    int getLayoutResourceId() {
        return R.layout.lb_speech_orb;
    }

    /**
     * Sets default listening state orb color.
     *
     * @param colors SearchOrbView.Colors.
     */
    public void setListeningOrbColors(Colors colors) {
        mListeningOrbColors = colors;
    }

    /**
     * Sets default not-listening state orb color.
     *
     * @param colors SearchOrbView.Colors.
     */
    public void setNotListeningOrbColors(Colors colors) {
        mNotListeningOrbColors = colors;
    }

    /**
     * Sets the view to display listening state.
     */
    public void showListening() {
        setOrbColors(mListeningOrbColors);
        setOrbIcon(getResources().getDrawable(R.drawable.lb_ic_search_mic));
        // Assume focused
        animateOnFocus(true);
        enableOrbColorAnimation(false);
        scaleOrbViewOnly(1f);
        mCurrentLevel = 0;
        mListening = true;
    }

    /**
     * Sets the view to display the not-listening state.
     */
    public void showNotListening() {
        setOrbColors(mNotListeningOrbColors);
        setOrbIcon(getResources().getDrawable(R.drawable.lb_ic_search_mic_out));
        animateOnFocus(hasFocus());
        scaleOrbViewOnly(1f);
        mListening = false;
    }

    /**
     * Sets the sound level while listening to speech.
     */
    public void setSoundLevel(int level) {
        if (!mListening) return;

        // Either ease towards the target level, or decay away from it depending on whether
        // its higher or lower than the current.
        if (level > mCurrentLevel) {
            mCurrentLevel = mCurrentLevel + ((level - mCurrentLevel) / 2);
        } else {
            mCurrentLevel = (int) (mCurrentLevel * 0.7f);
        }

        float zoom = 1f + (mSoundLevelMaxZoom - getFocusedZoom()) * mCurrentLevel / 100;

        scaleOrbViewOnly(zoom);
    }
}