1 2 package com.android.gallery3d.filtershow.imageshow; 3 4 import android.content.Context; 5 import android.graphics.Canvas; 6 import android.graphics.Color; 7 import android.graphics.Matrix; 8 import android.graphics.drawable.Drawable; 9 import android.util.AttributeSet; 10 import android.view.MotionEvent; 11 12 import com.android.gallery3d.filtershow.editors.EditorDraw; 13 import com.android.gallery3d.filtershow.filters.FilterDrawRepresentation; 14 import com.android.gallery3d.filtershow.filters.ImageFilterDraw; 15 16 public class ImageDraw extends ImageShow { 17 18 private static final String LOGTAG = "ImageDraw"; 19 private int mCurrentColor = Color.RED; 20 final static float INITAL_STROKE_RADIUS = 40; 21 private float mCurrentSize = INITAL_STROKE_RADIUS; 22 private byte mType = 0; 23 private FilterDrawRepresentation mFRep; 24 private EditorDraw mEditorDraw; 25 26 public ImageDraw(Context context, AttributeSet attrs) { 27 super(context, attrs); 28 resetParameter(); 29 super.setOriginalDisabled(true); 30 } 31 32 public ImageDraw(Context context) { 33 super(context); 34 resetParameter(); 35 super.setOriginalDisabled(true); 36 } 37 38 public void setEditor(EditorDraw editorDraw) { 39 mEditorDraw = editorDraw; 40 } 41 public void setFilterDrawRepresentation(FilterDrawRepresentation fr) { 42 mFRep = fr; 43 } 44 45 public Drawable getIcon(Context context) { 46 47 return null; 48 } 49 50 @Override 51 public void resetParameter() { 52 if (mFRep != null) { 53 mFRep.clear(); 54 } 55 } 56 57 public void setColor(int color) { 58 mCurrentColor = color; 59 } 60 61 public void setSize(int size) { 62 mCurrentSize = size; 63 } 64 65 public void setStyle(byte style) { 66 mType = (byte) (style % ImageFilterDraw.NUMBER_OF_STYLES); 67 } 68 69 public int getStyle() { 70 return mType; 71 } 72 73 public int getSize() { 74 return (int) mCurrentSize; 75 } 76 77 @Override 78 public void updateImage() { 79 super.updateImage(); 80 invalidate(); 81 } 82 83 float[] mTmpPoint = new float[2]; // so we do not malloc 84 @Override 85 public boolean onTouchEvent(MotionEvent event) { 86 if (event.getPointerCount() > 1) { 87 boolean ret = super.onTouchEvent(event); 88 if (mFRep.getCurrentDrawing() != null) { 89 mFRep.clearCurrentSection(); 90 mEditorDraw.commitLocalRepresentation(); 91 } 92 return ret; 93 } 94 if (event.getAction() != MotionEvent.ACTION_DOWN) { 95 if (mFRep.getCurrentDrawing() == null) { 96 return super.onTouchEvent(event); 97 } 98 } 99 100 ImageFilterDraw filter = (ImageFilterDraw) getCurrentFilter(); 101 102 if (event.getAction() == MotionEvent.ACTION_DOWN) { 103 calcScreenMapping(); 104 mTmpPoint[0] = event.getX(); 105 mTmpPoint[1] = event.getY(); 106 mToOrig.mapPoints(mTmpPoint); 107 mFRep.startNewSection(mType, mCurrentColor, mCurrentSize, mTmpPoint[0], mTmpPoint[1]); 108 } 109 110 if (event.getAction() == MotionEvent.ACTION_MOVE) { 111 112 int historySize = event.getHistorySize(); 113 final int pointerCount = event.getPointerCount(); 114 for (int h = 0; h < historySize; h++) { 115 int p = 0; 116 { 117 mTmpPoint[0] = event.getHistoricalX(p, h); 118 mTmpPoint[1] = event.getHistoricalY(p, h); 119 mToOrig.mapPoints(mTmpPoint); 120 mFRep.addPoint(mTmpPoint[0], mTmpPoint[1]); 121 } 122 } 123 } 124 125 if (event.getAction() == MotionEvent.ACTION_UP) { 126 mTmpPoint[0] = event.getX(); 127 mTmpPoint[1] = event.getY(); 128 mToOrig.mapPoints(mTmpPoint); 129 mFRep.endSection(mTmpPoint[0], mTmpPoint[1]); 130 } 131 mEditorDraw.commitLocalRepresentation(); 132 invalidate(); 133 return true; 134 } 135 136 Matrix mRotateToScreen = new Matrix(); 137 Matrix mToOrig; 138 private void calcScreenMapping() { 139 mToOrig = getScreenToImageMatrix(true); 140 mToOrig.invert(mRotateToScreen); 141 } 142 143 @Override 144 public void onDraw(Canvas canvas) { 145 super.onDraw(canvas); 146 calcScreenMapping(); 147 148 } 149 150 } 151