Android Open Source - api-ai-android-sdk Sound Level Circle Drawable






From Project

Back to project page api-ai-android-sdk.

License

The source code is released under:

Apache License

If you think the Android project api-ai-android-sdk 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 ai.api.ui;
//from  w  w w. java2s .  c  o m
/***********************************************************************************************************************
 *
 * API.AI Android SDK - client-side libraries for API.AI
 * =================================================
 *
 * Copyright (C) 2014 by Speaktoit, Inc. (https://www.speaktoit.com)
 * https://www.api.ai
 *
 ***********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 *
 ***********************************************************************************************************************/

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.Log;

public class SoundLevelCircleDrawable extends Drawable {
  private static final String TAG = SoundLevelCircleDrawable.class.getName();

  private final float maxRadius;
  private final float minRadius;
  private final float circleCenterX;
  private final float circleCenterY;
  private float minMicLevel = 1f / 2;
  private float maxMicLevel = 10;
  public static final int HALO_COLOR_DEF = Color.argb(16, 0x0, 0x0, 0x0);
  public static final int CENTER_COLOR_DEF = 0xffF26C29;
  private static final float INITIAL_VALUE = 2.5f;

  private boolean drawSoundLevel = false;

  private final Paint paintIndicatorHalo;
  private final Paint paintIndicatorCenter;
  private float smoothedLevel = INITIAL_VALUE;
  private final Rect bounds = new Rect();
  private boolean drawCenter = false;

  private static Paint newColorPaint(final int color) {
    final Paint paint = new Paint();
    paint.setStyle(Paint.Style.FILL);
    paint.setAntiAlias(true);
    paint.setColor(color);
    return paint;
  }

  public SoundLevelCircleDrawable() {
    this(null);
  }

  public SoundLevelCircleDrawable(@Nullable final Params params) {
    final int centerColor;
    final int haloColor;
    if (params != null) {
      this.maxRadius = params.maxRadius;
      this.minRadius = params.minRadius;
      this.circleCenterX = params.circleCenterX;
      this.circleCenterY = params.circleCenterY;
      centerColor = params.centerColor;
      haloColor = params.haloColor;
    } else {
      this.maxRadius = -1;
      this.minRadius = -1;
      this.circleCenterX = -1;
      this.circleCenterY = -1;
      centerColor = CENTER_COLOR_DEF;
      haloColor = HALO_COLOR_DEF;
    }
    paintIndicatorHalo = newColorPaint(haloColor);
    paintIndicatorCenter = newColorPaint(centerColor);
  }

  @SuppressWarnings("MagicNumber")
  @Override
  public void draw(final Canvas canvas) {
    if (drawSoundLevel || drawCenter) {
      canvas.save();
      try {
        if (this.maxRadius < 0 || this.circleCenterX < 0 || this.circleCenterY < 0)
          canvas.getClipBounds(bounds);

        canvas.drawColor(Color.TRANSPARENT);
        final float levelInFraction = (smoothedLevel - minMicLevel) / (maxMicLevel - minMicLevel);
        final float maxRadius = this.maxRadius < 0 ? bounds.width() / 2f : this.maxRadius;
        final float minRadius = this.minRadius < 0 ? maxRadius * (65 / 112.5f) : this.minRadius;
        final float soundMinRadius = minRadius * 0.8f; //to hide halo on silence
        final float rangeRadius = maxRadius - soundMinRadius;
        final float soundRadius = soundMinRadius + rangeRadius * levelInFraction;
        final float x = this.circleCenterX < 0 ? bounds.width() / 2f : this.circleCenterX;
        final float y = this.circleCenterY < 0 ? bounds.height() / 2f : this.circleCenterY;
        if (drawSoundLevel)
          canvas.drawCircle(x, y, soundRadius, paintIndicatorHalo);
        if (drawCenter || drawSoundLevel)
          canvas.drawCircle(x, y, minRadius, paintIndicatorCenter);
      } finally {
        canvas.restore();
      }
    }
  }

  @Override
  public void setAlpha(final int ignored) {
    //ignore
  }

  @Override
  public void setColorFilter(final ColorFilter ignored) {
    //ignore
  }

  @Override
  public int getOpacity() {
    return PixelFormat.TRANSLUCENT;
  }

  /**
   * @param drawSoundLevel true to draw
   * @return true if drawSoundLevel changed
   */
  public boolean setDrawSoundLevel(final boolean drawSoundLevel) {
    if (this.drawSoundLevel != drawSoundLevel) {
      this.drawSoundLevel = drawSoundLevel;
      if (drawSoundLevel) {
        smoothedLevel = INITIAL_VALUE;
      }
      return true;
    } else
      return false;
  }

  public void setDrawCenter(final boolean drawCenter) {
    this.drawCenter = drawCenter;
  }

  public void setSoundLevel(final float soundLevel) {
    final float positiveSoundLevel = Math.abs(soundLevel);

    if (positiveSoundLevel < minMicLevel) {
      minMicLevel = (minMicLevel + positiveSoundLevel) / 2; // average
    }

    if (positiveSoundLevel > maxMicLevel) {
      maxMicLevel = (maxMicLevel + positiveSoundLevel) / 2; // average
    }

    final float alpha = 0.2f;
    smoothedLevel = (smoothedLevel * (1 - alpha) + (positiveSoundLevel * alpha));

    if (smoothedLevel > maxMicLevel) {
      smoothedLevel = maxMicLevel;
    } else if (smoothedLevel < minMicLevel) {
      smoothedLevel = minMicLevel;
    }
  }

  public float getMinRadius() {
    return minRadius;
  }

  public static class Params {
    public final float maxRadius;
    public final float minRadius;
    public final float circleCenterX;
    public final float circleCenterY;
    private final int centerColor;
    private final int haloColor;

    public Params(final float maxRadius, final float minRadius, final float circleCenterX, final float circleCenterY, final int centerColor, final int haloColor) {
      this.maxRadius = maxRadius;
      this.minRadius = minRadius;
      this.circleCenterX = circleCenterX;
      this.circleCenterY = circleCenterY;
      this.centerColor = centerColor;
      this.haloColor = haloColor;
    }
  }
}




Java Source Code List

ai.api.AIConfiguration.java
ai.api.AIDataService.java
ai.api.AIListener.java
ai.api.AIServiceException.java
ai.api.AIService.java
ai.api.AudioRequestTask.java
ai.api.AudioRequest.java
ai.api.GoogleRecognitionServiceImpl.java
ai.api.GsonFactory.java
ai.api.RecognitionEngine.java
ai.api.RequestTask.java
ai.api.SpeaktoitRecognitionServiceImpl.java
ai.api.http.HttpClient.java
ai.api.model.AIContext.java
ai.api.model.AIError.java
ai.api.model.AIRequest.java
ai.api.model.AIResponse.java
ai.api.model.Constants.java
ai.api.model.Metadata.java
ai.api.model.QuestionMetadata.java
ai.api.model.Result.java
ai.api.model.Status.java
ai.api.sample.AIButtonSampleActivity.java
ai.api.sample.AIServiceSampleActivity.java
ai.api.sample.Config.java
ai.api.sample.MainActivity.java
ai.api.ui.AIButton.java
ai.api.ui.MaskedColorView.java
ai.api.ui.SoundLevelButton.java
ai.api.ui.SoundLevelCircleDrawable.java
ai.api.util.RecognizerChecker.java
ai.api.util.VoiceActivityDetector.java