Android Open Source - Aviary-Android-SDK Preview Spot Drawable






From Project

Back to project page Aviary-Android-SDK.

License

The source code is released under:

AVIARY API TERMS OF USE Full Legal Agreement The following terms and conditions and the terms and conditions at http://www.aviary.com/terms (collectively, the ?Terms??) govern your use of any and ...

If you think the Android project Aviary-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 com.aviary.android.feather.graphics;
/*w w w  .j a  va 2  s.c o  m*/
import android.content.Context;
import android.content.res.Resources.Theme;
import android.content.res.TypedArray;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.Shader.TileMode;
import android.graphics.Xfermode;
import android.graphics.drawable.Drawable;
import android.util.Log;

import com.aviary.android.feather.R;

public class PreviewSpotDrawable extends Drawable {

  private static final String LOG_TAG = "PreviewSpotDrawable";

  final int mStrokeWidth, mStrokeWidthOuter;
  final int mGlowColor;
  final int mStrokeColor;
  final int mBackgroundColorUnselected, mBackgroundColorSelected;
  final Paint mPaint;
  float mRadius;
  final Rect mDstRect = new Rect();
  final LinearGradient mGradientShaderUnselected;
  final LinearGradient mGradientShaderSelected;
  final BlurMaskFilter mGlowBlurMaskFilter;
  final Matrix mGradientMatrix;
  boolean mRadiusFixed = false;

  LinearGradient mGradient;

  private boolean mChecked;
  private boolean mPressed;

  private Xfermode mPorterDuffSrcInMode = new PorterDuffXfermode( Mode.SRC_IN );

  public PreviewSpotDrawable ( Context context ) {
    super();

    Theme theme = context.getTheme();
    TypedArray a = theme.obtainStyledAttributes( null, R.styleable.AviaryPreviewSpotDrawable, R.attr.aviaryPreviewSpotDrawableStyle, 0 );
    mStrokeWidth = a.getDimensionPixelSize( R.styleable.AviaryPreviewSpotDrawable_aviary_strokeWidth, 20 );
    mStrokeWidthOuter = (int) ( mStrokeWidth * 1.7 );

    int color1 = a.getColor( R.styleable.AviaryPreviewSpotDrawable_aviary_color1, Color.WHITE );
    int color2 = a.getColor( R.styleable.AviaryPreviewSpotDrawable_aviary_color2, Color.BLACK );
    int color3 = a.getColor( R.styleable.AviaryPreviewSpotDrawable_aviary_color3, Color.BLACK );
    int color4 = a.getColor( R.styleable.AviaryPreviewSpotDrawable_aviary_color4, Color.WHITE );
    mGlowColor = a.getColor( R.styleable.AviaryPreviewSpotDrawable_aviary_highlightColorChecked, Color.WHITE );
    mBackgroundColorUnselected = a.getColor( R.styleable.AviaryPreviewSpotDrawable_aviary_bg_color1, Color.WHITE );
    mBackgroundColorSelected = a.getColor( R.styleable.AviaryPreviewSpotDrawable_aviary_bg_color2, Color.BLACK );
    mStrokeColor = a.getColor( R.styleable.AviaryPreviewSpotDrawable_aviary_strokeColor, Color.BLACK );

    int glowSize = a.getInteger( R.styleable.AviaryPreviewSpotDrawable_aviary_glowSize, 3 );

    a.recycle();

    mPaint = new Paint( Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG | Paint.FILTER_BITMAP_FLAG );
    mPaint.setStyle( Paint.Style.STROKE );

    mGradientShaderUnselected = new LinearGradient( 0, 0, 0, 1, new int[] { color1, color2 }, new float[] { 0.5f, 1 }, TileMode.CLAMP );
    mGradientShaderSelected = new LinearGradient( 0, 0, 0, 1, new int[] { color3, color4 }, new float[] { 0.5f, 1 }, TileMode.CLAMP );
    mGradient = mGradientShaderUnselected;

    mGlowBlurMaskFilter = new BlurMaskFilter( glowSize, BlurMaskFilter.Blur.NORMAL );

    mGradientMatrix = new Matrix();
    mGradientShaderUnselected.setLocalMatrix( mGradientMatrix );
    mRadius = 10;
  }

  public void setRadius( float value ) {
    Log.i( LOG_TAG, "setRadius: " + value );
    mRadius = value;
    invalidateSelf();
  }

  public void setFixedRadius( float value ) {
    mRadiusFixed = true;
    mRadius = value;
    invalidateSelf();
  }

  @Override
  public void draw( Canvas canvas ) {
    copyBounds( mDstRect );

    float radius = mRadius;

    if ( !mRadiusFixed ) {
      radius = Math.min( mDstRect.width(), mDstRect.height() ) * 0.8f * mRadius;
    }

    // reset the paint
    mPaint.setShader( null );
    mPaint.setMaskFilter( null );
    mPaint.setXfermode( null );

    // outer stroke width

    // outside glow when selected
    if ( mChecked ) {
      mPaint.setStrokeWidth( mStrokeWidth );
      mPaint.setMaskFilter( mGlowBlurMaskFilter );
      mPaint.setColor( mGlowColor );
      canvas.drawCircle( mDstRect.centerX(), mDstRect.centerY(), radius, mPaint );
    }

    // draw the black border around the circle
    mPaint.setStrokeWidth( mStrokeWidthOuter );
    mPaint.setMaskFilter( null );
    mPaint.setColor( mStrokeColor );
    canvas.drawCircle( mDstRect.centerX(), mDstRect.centerY(), radius, mPaint );

    // draw the circle fill
    canvas.saveLayer( mDstRect.left, mDstRect.top, mDstRect.right, mDstRect.bottom, mPaint, Canvas.ALL_SAVE_FLAG );

    mPaint.setStrokeWidth( mStrokeWidth );
    mPaint.setColor( mChecked ? mBackgroundColorSelected : mBackgroundColorUnselected );
    canvas.drawCircle( mDstRect.centerX(), mDstRect.centerY(), radius, mPaint );

    mGradientMatrix.reset();
    mGradientMatrix.postScale( 1, radius * 2 );
    mGradientMatrix.postTranslate( 0, mDstRect.centerY() + 3 - radius * 2 );
    mGradient.setLocalMatrix( mGradientMatrix );

    mPaint.setXfermode( mPorterDuffSrcInMode );
    mPaint.setColor( Color.WHITE );
    mPaint.setShader( mGradient );
    canvas.drawCircle( mDstRect.centerX(), mDstRect.centerY() + 3, radius, mPaint );

    // Paint p = new Paint();
    // p.setColor( 0x33ff0000 );
    // canvas.drawRect( mDstRect.centerX() - radius, mDstRect.centerY() - radius,
    // mDstRect.centerX() + radius, mDstRect.centerY() + radius, p );

    canvas.restore();
  }

  @Override
  public boolean isStateful() {
    return true;
  }

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

  @Override
  public void setAlpha( int alpha ) {}

  @Override
  public void setColorFilter( ColorFilter cf ) {}

  @Override
  protected boolean onStateChange( int[] state ) {

    boolean checked = mChecked;
    boolean pressed = mPressed;

    mChecked = false;
    mPressed = false;

    for ( int i = 0; i < state.length; i++ ) {
      if ( state[i] == android.R.attr.state_pressed ) {
        mPressed = true;
      }

      if ( state[i] == android.R.attr.state_selected ) {
        mChecked = true;
      }
    }

    if ( mChecked ) {
      mGradient = mGradientShaderSelected;
    } else {
      mGradient = mGradientShaderUnselected;
    }

    return checked != mChecked || pressed != mPressed;

  }
}




Java Source Code List

com.aviary.android.feather.AlertActivity.java
com.aviary.android.feather.AviaryMainController.java
com.aviary.android.feather.FeatherActivity.java
com.aviary.android.feather.async_tasks.AsyncImageManager.java
com.aviary.android.feather.async_tasks.DownloadImageAsyncTask.java
com.aviary.android.feather.async_tasks.ExifTask.java
com.aviary.android.feather.effects.AbstractContentPanel.java
com.aviary.android.feather.effects.AbstractOptionPanel.java
com.aviary.android.feather.effects.AbstractPanelLoaderService.java
com.aviary.android.feather.effects.AbstractPanel.java
com.aviary.android.feather.effects.AdjustEffectPanel.java
com.aviary.android.feather.effects.BordersPanel.java
com.aviary.android.feather.effects.ColorSplashPanel.java
com.aviary.android.feather.effects.CropPanel.java
com.aviary.android.feather.effects.DelayedSpotDrawPanel.java
com.aviary.android.feather.effects.DrawingPanel.java
com.aviary.android.feather.effects.EffectsPanel.java
com.aviary.android.feather.effects.EnhanceEffectPanel.java
com.aviary.android.feather.effects.MemePanel.java
com.aviary.android.feather.effects.NativeEffectRangePanel.java
com.aviary.android.feather.effects.SimpleStatusMachine.java
com.aviary.android.feather.effects.SliderEffectPanel.java
com.aviary.android.feather.effects.StickersPanel.java
com.aviary.android.feather.effects.TextPanel.java
com.aviary.android.feather.effects.TiltShiftPanel.java
com.aviary.android.feather.graphics.CdsPreviewTransformer.java
com.aviary.android.feather.graphics.GalleryBottomIndicatorDrawable.java
com.aviary.android.feather.graphics.GalleryTopIndicatorDrawable.java
com.aviary.android.feather.graphics.GlowBitmapDrawable.java
com.aviary.android.feather.graphics.GlowDrawable.java
com.aviary.android.feather.graphics.PluginDividerDrawable.java
com.aviary.android.feather.graphics.PreviewFillColorDrawable.java
com.aviary.android.feather.graphics.PreviewSpotDrawable.java
com.aviary.android.feather.graphics.RepeatableHorizontalDrawable.java
com.aviary.android.feather.opengl.AviaryGLSurfaceView.java
com.aviary.android.feather.utils.PackIconCallable.java
com.aviary.android.feather.utils.SimpleBitmapCache.java
com.aviary.android.feather.utils.ThreadUtils.java
com.aviary.android.feather.utils.TypefaceUtils.java
com.aviary.android.feather.utils.UIUtils.java
com.aviary.android.feather.widget.AdjustImageView.java
com.aviary.android.feather.widget.AviaryAbsSpinner.java
com.aviary.android.feather.widget.AviaryAdapterView.java
com.aviary.android.feather.widget.AviaryBadgeToolLayout.java
com.aviary.android.feather.widget.AviaryBottomBarViewFlipper.java
com.aviary.android.feather.widget.AviaryButton.java
com.aviary.android.feather.widget.AviaryEdgeEffect.java
com.aviary.android.feather.widget.AviaryGalleryTopIndicatorView.java
com.aviary.android.feather.widget.AviaryGallery.java
com.aviary.android.feather.widget.AviaryHighlightImageButton.java
com.aviary.android.feather.widget.AviaryImageRestoreSwitcher.java
com.aviary.android.feather.widget.AviaryImageSwitcher.java
com.aviary.android.feather.widget.AviaryNavBarViewFlipper.java
com.aviary.android.feather.widget.AviarySeekBar.java
com.aviary.android.feather.widget.AviaryTextView.java
com.aviary.android.feather.widget.AviaryToast.java
com.aviary.android.feather.widget.AviaryToggleButton.java
com.aviary.android.feather.widget.AviaryWheel.java
com.aviary.android.feather.widget.AviaryWorkspaceIndicator.java
com.aviary.android.feather.widget.AviaryWorkspace.java
com.aviary.android.feather.widget.CellLayout.java
com.aviary.android.feather.widget.CropImageView.java
com.aviary.android.feather.widget.DrawableHighlightView.java
com.aviary.android.feather.widget.EffectThumbLayout.java
com.aviary.android.feather.widget.HighlightView.java
com.aviary.android.feather.widget.IAPBuyButton.java
com.aviary.android.feather.widget.IAPDialogDetail.java
com.aviary.android.feather.widget.IAPDialogList.java
com.aviary.android.feather.widget.IAPDialogMain.java
com.aviary.android.feather.widget.ImageViewDrawableOverlay.java
com.aviary.android.feather.widget.ImageViewSpotDraw.java
com.aviary.android.feather.widget.ImageViewTiltiShiftTouch.java
com.aviary.android.feather.widget.ImageViewTouchAndDraw.java
com.aviary.android.feather.widget.PointCloud.java
com.aviary.android.feather.widget.ScrollerRunnable.java
com.aviary.android.feather.widget.VibrationHelper.java
com.aviary.android.feather.widget.VibrationWidget.java