Android Open Source - Arvutaja Mic Button






From Project

Back to project page Arvutaja.

License

The source code is released under:

Apache License

If you think the Android project Arvutaja listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package ee.ioc.phon.android.arvutaja;
//from   w ww .  j  a va2  s  .c  om
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageButton;

public class MicButton extends ImageButton {

  // TODO: take these from some configuration
  private static final float DB_MIN = 15.0f;
  private static final float DB_MAX = 30.0f;

  private Drawable mDrawableMic;
  private Drawable mDrawableMicTranscribing;

  private List<Drawable> mVolumeLevels;

  private Animation mAnimFadeIn;
  private Animation mAnimFadeOut;
  private Animation mAnimFadeInOutInf;

  private int mVolumeLevel = 0;
  private int mMaxLevel;

  public MicButton(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    initAnimations(context);
  }

  public MicButton(Context context, AttributeSet attrs) {
    super(context, attrs);
    initAnimations(context);
  }


  public MicButton(Context context) {
    super(context);
    initAnimations(context);
  }


  public void setState(Constants.State state) {
    switch(state) {
    case INIT:
      clearAnimation();
      setBackgroundDrawable(mDrawableMic);
      break;
    case RECORDING:
      setBackgroundDrawable(mVolumeLevels.get(0));
      break;
    case LISTENING:
      break;
    case TRANSCRIBING:
      setBackgroundDrawable(mDrawableMicTranscribing);
      startAnimation(mAnimFadeInOutInf);
      break;
    case ERROR:
      clearAnimation();
      setBackgroundDrawable(mDrawableMic);
      break;
    default:
      break;
    }
  }


  public void setVolumeLevel(float rmsdB) {
    int index = (int) ((rmsdB - DB_MIN) / (DB_MAX - DB_MIN) * mMaxLevel);
    int level = Math.min(Math.max(0, index), mMaxLevel);
    if (level != mVolumeLevel) {
      mVolumeLevel = level;
      setBackgroundDrawable(mVolumeLevels.get(level));
    }
  }


  public void fadeIn() {
    Animations.startFadeAnimation(mAnimFadeIn, this, View.VISIBLE);
  }


  public void fadeOut() {
    Animations.startFadeAnimation(mAnimFadeOut, this, View.INVISIBLE);
  }


  private void initAnimations(Context context) {
    Resources res = getResources();
    mDrawableMic = res.getDrawable(R.drawable.button_mic);
    mDrawableMicTranscribing = res.getDrawable(R.drawable.button_mic_transcribing);

    mVolumeLevels = new ArrayList<Drawable>();
    mVolumeLevels.add(res.getDrawable(R.drawable.button_mic_recording_0));
    mVolumeLevels.add(res.getDrawable(R.drawable.button_mic_recording_1));
    mVolumeLevels.add(res.getDrawable(R.drawable.button_mic_recording_2));
    mVolumeLevels.add(res.getDrawable(R.drawable.button_mic_recording_3));
    mMaxLevel = mVolumeLevels.size() - 1;

    mAnimFadeIn = AnimationUtils.loadAnimation(context, R.anim.fade_in);
    mAnimFadeOut = AnimationUtils.loadAnimation(context, R.anim.fade_out);
    mAnimFadeInOutInf = AnimationUtils.loadAnimation(context, R.anim.fade_inout_inf);
  }
}




Java Source Code List

ee.ioc.phon.android.arvutaja.AbstractRecognizerActivity.java
ee.ioc.phon.android.arvutaja.Animations.java
ee.ioc.phon.android.arvutaja.ArvutajaActivity.java
ee.ioc.phon.android.arvutaja.AudioCue.java
ee.ioc.phon.android.arvutaja.Constants.java
ee.ioc.phon.android.arvutaja.ExamplesActivity.java
ee.ioc.phon.android.arvutaja.Executable.java
ee.ioc.phon.android.arvutaja.LocalizedStrings.java
ee.ioc.phon.android.arvutaja.Log.java
ee.ioc.phon.android.arvutaja.MicButton.java
ee.ioc.phon.android.arvutaja.SettingsActivity.java
ee.ioc.phon.android.arvutaja.SettingsFragment.java
ee.ioc.phon.android.arvutaja.ShowActivity.java
ee.ioc.phon.android.arvutaja.SubActivity.java
ee.ioc.phon.android.arvutaja.Utils.java
ee.ioc.phon.android.arvutaja.command.Alarm.java
ee.ioc.phon.android.arvutaja.command.CommandParseException.java
ee.ioc.phon.android.arvutaja.command.CommandParser.java
ee.ioc.phon.android.arvutaja.command.Command.java
ee.ioc.phon.android.arvutaja.command.DefaultCommand.java
ee.ioc.phon.android.arvutaja.command.Dial.java
ee.ioc.phon.android.arvutaja.command.Direction.java
ee.ioc.phon.android.arvutaja.command.Expr.java
ee.ioc.phon.android.arvutaja.command.MathEval.java
ee.ioc.phon.android.arvutaja.command.Search.java
ee.ioc.phon.android.arvutaja.command.Unitconv.java
ee.ioc.phon.android.arvutaja.command.View.java
ee.ioc.phon.android.arvutaja.command.WebSearch.java
ee.ioc.phon.android.arvutaja.provider.ArvutajaAppWidgetProvider.java
ee.ioc.phon.android.arvutaja.provider.Qeval.java
ee.ioc.phon.android.arvutaja.provider.QueriesContentProvider.java
ee.ioc.phon.android.arvutaja.provider.Query.java