Android Open Source - Aviary-Android-SDK Image View Spot Draw






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.widget;
//from ww  w.  j a v a 2 s .  c om
import it.sephiroth.android.library.imagezoom.ImageViewTouch;
import it.sephiroth.android.library.imagezoom.graphics.IBitmapDrawable;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;

public class ImageViewSpotDraw extends ImageViewTouch {

  public static enum TouchMode {
    // mode for pan and zoom
    IMAGE,
    // mode for drawing
    DRAW
  };

  public static interface OnDrawListener {
    void onDrawStart( float points[], int radius );

    void onDrawing( float points[], int radius );

    void onDrawEnd();
  };

  protected boolean mPaintEnabled = true;
  protected Paint mPaint;
  protected float mCurrentScale = 1;
  protected float mBrushSize = 30;
  protected Path tmpPath = new Path();
  protected Canvas mCanvas;
  protected TouchMode mTouchMode = TouchMode.DRAW;
  protected float mX, mY;
  protected float mStartX, mStartY;
  protected Matrix mIdentityMatrix = new Matrix();
  protected Matrix mInvertedMatrix = new Matrix();
  protected static final float TOUCH_TOLERANCE = 2;
  private OnDrawListener mDrawListener;
  private double mRestiction = 0;

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

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

  public void setOnDrawStartListener( OnDrawListener listener ) {
    mDrawListener = listener;
  }

  @Override
  protected void init( Context context, AttributeSet attrs, int defStyle ) {
    super.init( context, attrs, defStyle );
    mPaint = new Paint( Paint.ANTI_ALIAS_FLAG );
    mPaint.setFilterBitmap( false );
    mPaint.setDither( true );
    mPaint.setColor( 0x66FFFFCC );
    mPaint.setStyle( Paint.Style.STROKE );
    mPaint.setStrokeCap( Paint.Cap.ROUND );
    tmpPath = new Path();
  }

  public void setDrawLimit( double value ) {
    mRestiction = value;
  }

  public void setBrushSize( float value ) {
    mBrushSize = value;

    if ( mPaint != null ) {
      mPaint.setStrokeWidth( mBrushSize );
    }
  }

  public TouchMode getDrawMode() {
    return mTouchMode;
  }

  public void setDrawMode( TouchMode mode ) {
    if ( mode != mTouchMode ) {
      mTouchMode = mode;
      onDrawModeChanged();
    }
  }

  protected void onDrawModeChanged() {
    if ( mTouchMode == TouchMode.DRAW ) {
      Log.i( LOG_TAG, "onDrawModeChanged" );

      Matrix m1 = new Matrix( getImageMatrix() );
      mInvertedMatrix.reset();

      float[] v1 = getMatrixValues( m1 );
      m1.invert( m1 );
      float[] v2 = getMatrixValues( m1 );

      mInvertedMatrix.postTranslate( -v1[Matrix.MTRANS_X], -v1[Matrix.MTRANS_Y] );
      mInvertedMatrix.postScale( v2[Matrix.MSCALE_X], v2[Matrix.MSCALE_Y] );
      mCanvas.setMatrix( mInvertedMatrix );

      mCurrentScale = getScale() * getBaseScale();

      mPaint.setStrokeWidth( mBrushSize );
    }
  }

  public Paint getPaint() {
    return mPaint;
  }

  public void setPaint( Paint paint ) {
    mPaint.set( paint );
  }

  public void setPaintEnabled( boolean enabled ) {
    mPaintEnabled = enabled;
  }

  @Override
  protected void onDraw( Canvas canvas ) {
    super.onDraw( canvas );

    if ( mPaintEnabled ) canvas.drawPath( tmpPath, mPaint );
  }

  public RectF getImageRect() {
    if ( getDrawable() != null ) {
      return new RectF( 0, 0, getDrawable().getIntrinsicWidth(), getDrawable().getIntrinsicHeight() );
    } else {
      return null;
    }
  }

  @Override
  protected void onDrawableChanged( Drawable drawable ) {
    super.onDrawableChanged( drawable );

    if ( drawable != null && ( drawable instanceof IBitmapDrawable ) ) {
      mCanvas = new Canvas();
      mCanvas.drawColor( 0 );
      onDrawModeChanged();
    }
  }

  @Override
  protected void onLayoutChanged( int left, int top, int right, int bottom ) {
    super.onLayoutChanged( left, top, right, bottom );

    if ( null != getDrawable() ) {
      onDrawModeChanged();
    }
  }

  private boolean mMoved = false;

  private void touch_start( float x, float y ) {

    mMoved = false;

    tmpPath.reset();
    tmpPath.moveTo( x, y );

    mX = x;
    mY = y;
    mStartX = x;
    mStartY = y;

    if ( mDrawListener != null ) {
      float mappedPoints[] = new float[2];
      mappedPoints[0] = x;
      mappedPoints[1] = y;
      mInvertedMatrix.mapPoints( mappedPoints );
      tmpPath.lineTo( x + .1f, y );
      Log.d( LOG_TAG, "brushSize: " + mBrushSize + ", currentScale: " + mCurrentScale + ", basescale: " + getBaseScale() );
      mDrawListener.onDrawStart( mappedPoints, (int) ( mBrushSize / mCurrentScale ) );
    }
  }

  private void touch_move( float x, float y ) {

    float dx = Math.abs( x - mX );
    float dy = Math.abs( y - mY );

    if ( dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE ) {

      if ( !mMoved ) {
        tmpPath.setLastPoint( mX, mY );
      }

      mMoved = true;

      if ( mRestiction > 0 ) {
        double r = Math.sqrt( Math.pow( x - mStartX, 2 ) + Math.pow( y - mStartY, 2 ) );
        double theta = Math.atan2( y - mStartY, x - mStartX );

        final float w = getWidth();
        final float h = getHeight();

        double scale = ( mRestiction / mCurrentScale ) / (double) ( w + h ) / ( mBrushSize / mCurrentScale );
        double rNew = Math.log( r * scale + 1 ) / scale;

        x = (float) ( mStartX + rNew * Math.cos( theta ) );
        y = (float) ( mStartY + rNew * Math.sin( theta ) );
      }

      tmpPath.quadTo( mX, mY, ( x + mX ) / 2, ( y + mY ) / 2 );
      mX = x;
      mY = y;
    }

    if ( mDrawListener != null ) {
      float mappedPoints[] = new float[2];
      mappedPoints[0] = x;
      mappedPoints[1] = y;
      mInvertedMatrix.mapPoints( mappedPoints );
      mDrawListener.onDrawing( mappedPoints, (int) ( mBrushSize / mCurrentScale ) );
    }
  }

  private void touch_up() {

    tmpPath.reset();

    if ( mDrawListener != null ) {
      mDrawListener.onDrawEnd();
    }
  }

  public static float[] getMatrixValues( Matrix m ) {
    float[] values = new float[9];
    m.getValues( values );
    return values;
  }

  @Override
  public boolean onTouchEvent( MotionEvent event ) {
    if ( mTouchMode == TouchMode.DRAW && event.getPointerCount() == 1 ) {
      float x = event.getX();
      float y = event.getY();

      switch ( event.getAction() ) {
        case MotionEvent.ACTION_DOWN:
          touch_start( x, y );
          invalidate();
          break;
        case MotionEvent.ACTION_MOVE:
          touch_move( x, y );
          invalidate();
          break;
        case MotionEvent.ACTION_UP:
          touch_up();
          invalidate();
          break;
      }
      return true;
    } else {
      if ( mTouchMode == TouchMode.IMAGE ) return super.onTouchEvent( event );
      else return false;
    }
  }
}




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