Android Open Source - Aviary-Android-SDK Image View Touch And 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   w  w  w. j a v a2 s .  c o  m
import it.sephiroth.android.library.imagezoom.ImageViewTouch;
import it.sephiroth.android.library.imagezoom.graphics.IBitmapDrawable;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;

public class ImageViewTouchAndDraw extends ImageViewTouch {

  public static enum TouchMode {
    IMAGE, DRAW
  };

  public static interface OnDrawStartListener {
    void onDrawStart();
  };

  public static interface OnDrawPathListener {
    void onStart();

    void onMoveTo( float x, float y );

    void onLineTo( float x, float y );

    void onQuadTo( float x, float y, float x1, float y1 );

    void onEnd();
  }

  protected Paint mPaint;

  protected Path tmpPath = new Path();

  protected Canvas mCanvas;

  protected TouchMode mTouchMode = TouchMode.DRAW;

  protected float mX, mY;

  protected Matrix mIdentityMatrix = new Matrix();

  protected Matrix mInvertedMatrix = new Matrix();

  protected Bitmap mCopy;

  protected static final float TOUCH_TOLERANCE = 4;

  private OnDrawStartListener mDrawListener;

  private OnDrawPathListener mDrawPathListener;

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

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

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

  public void setOnDrawPathListener( OnDrawPathListener listener ) {
    mDrawPathListener = listener;
  }

  @Override
  protected void init( Context context, AttributeSet attrs, int defStyle ) {
    super.init( context, attrs, defStyle );

    Log.i( LOG_TAG, "init, paint: " + mPaint );

    mPaint = new Paint( Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG );
    mPaint.setFilterBitmap( false );
    mPaint.setColor( 0xFFFF0000 );
    mPaint.setStyle( Paint.Style.STROKE );
    mPaint.setStrokeJoin( Paint.Join.ROUND );
    mPaint.setStrokeCap( Paint.Cap.ROUND );
    mPaint.setStrokeWidth( 10.0f );

    tmpPath = new Path();
  }

  public TouchMode getDrawMode() {
    return mTouchMode;
  }

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

  protected void onDrawModeChanged() {
    if ( mTouchMode == TouchMode.DRAW ) {

      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 );

      Log.d( LOG_TAG, "scale: " + getScale( mInvertedMatrix ) );
    }
  }

  public float getDrawingScale() {
    return getScale( mInvertedMatrix );
  }

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

  public Paint getPaint() {
    return mPaint;
  }

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

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

    // canvas.drawPath( tmpPath, mPaint );

    if ( mCopy != null ) {
      final int saveCount = canvas.getSaveCount();
      canvas.save();
      canvas.drawBitmap( mCopy, getImageMatrix(), null );
      canvas.restoreToCount( saveCount );
    }
  }

  /**
   * Commit the current changes to the passed {@link Bitmap}.<br />
   * Call this method when you want to save the current status of the drawing operations
   * to an output bitmap.
   * 
   * @param canvas
   */
  public void commit( Canvas canvas ) {
    Drawable drawable = getDrawable();
    if ( null != drawable && drawable instanceof IBitmapDrawable ) {
      canvas.drawBitmap( ( (IBitmapDrawable) drawable ).getBitmap(), new Matrix(), null );
      canvas.drawBitmap( mCopy, new Matrix(), null );
    }
  }

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

    if ( mCopy != null ) {
      mCopy.recycle();
      mCopy = null;
    }

    if ( drawable != null && ( drawable instanceof IBitmapDrawable ) ) {
      final Bitmap bitmap = ( (IBitmapDrawable) drawable ).getBitmap();
      mCopy = Bitmap.createBitmap( bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888 );
      mCanvas = new Canvas( mCopy );
      mCanvas.drawColor( 0 );
      onDrawModeChanged();
    }
  }

  private void touch_start( float x, float y ) {
    tmpPath.reset();
    tmpPath.moveTo( x, y );

    mX = x;
    mY = y;

    if ( mDrawListener != null ) mDrawListener.onDrawStart();
    if ( mDrawPathListener != null ) {

      mDrawPathListener.onStart();

      float[] pts = new float[] { x, y };
      mInvertedMatrix.mapPoints( pts );
      mDrawPathListener.onMoveTo( pts[0], pts[1] );
    }
  }

  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 ) {

      float x1 = ( x + mX ) / 2;
      float y1 = ( y + mY ) / 2;

      tmpPath.quadTo( mX, mY, x1, y1 );

      mCanvas.drawPath( tmpPath, mPaint );
      tmpPath.reset();
      tmpPath.moveTo( x1, y1 );

      if ( mDrawPathListener != null ) {

        float[] pts = new float[] { mX, mY, x1, y1 };
        mInvertedMatrix.mapPoints( pts );
        mDrawPathListener.onQuadTo( pts[0], pts[1], pts[2], pts[3] );
      }

      mX = x;
      mY = y;
    }
  }

  private void touch_up() {

    tmpPath.reset();
    if ( mDrawPathListener != null ) {
      mDrawPathListener.onEnd();
    }
  }

  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;
    }
  }

  public Bitmap getOverlayBitmap() {
    return mCopy;
  }
}




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