|
| 1 | +/* |
| 2 | +Copyright (c) 2012, Zubair Khan ([email protected]) |
| 3 | +All rights reserved. |
| 4 | +
|
| 5 | +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: |
| 6 | +
|
| 7 | + * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. |
| 8 | + * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. |
| 9 | + * |
| 10 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 11 | +*/ |
| 12 | + |
| 13 | +package com.ds.avare; |
| 14 | + |
| 15 | +import android.content.Context; |
| 16 | +import android.graphics.Canvas; |
| 17 | +import android.graphics.Color; |
| 18 | +import android.graphics.Paint; |
| 19 | +import android.util.AttributeSet; |
| 20 | +import android.view.GestureDetector; |
| 21 | +import android.view.MotionEvent; |
| 22 | +import android.view.View; |
| 23 | +import android.view.View.OnTouchListener; |
| 24 | + |
| 25 | +import com.ds.avare.position.Pan; |
| 26 | +import com.ds.avare.position.Scale; |
| 27 | +import com.ds.avare.touch.MultiTouchController; |
| 28 | +import com.ds.avare.touch.MultiTouchController.MultiTouchObjectCanvas; |
| 29 | +import com.ds.avare.touch.MultiTouchController.PointInfo; |
| 30 | +import com.ds.avare.touch.MultiTouchController.PositionAndScale; |
| 31 | +import com.ds.avare.utils.BitmapHolder; |
| 32 | + |
| 33 | +/** |
| 34 | + * |
| 35 | + * @author zkhan |
| 36 | + * |
| 37 | + */ |
| 38 | +public class AfdView extends View implements MultiTouchObjectCanvas<Object>, OnTouchListener { |
| 39 | + |
| 40 | + |
| 41 | + private Scale mScale; |
| 42 | + private Pan mPan; |
| 43 | + private Paint mPaint; |
| 44 | + private MultiTouchController<Object> mMultiTouchC; |
| 45 | + private PointInfo mCurrTouchPoint; |
| 46 | + private GestureDetector mGestureDetector; |
| 47 | + private BitmapHolder mBitmap; |
| 48 | + |
| 49 | + /** |
| 50 | + * |
| 51 | + * @param context |
| 52 | + */ |
| 53 | + private void setup(Context context) { |
| 54 | + mPaint = new Paint(); |
| 55 | + mPaint.setAntiAlias(true); |
| 56 | + mPan = new Pan(); |
| 57 | + mScale = new Scale(); |
| 58 | + setOnTouchListener(this); |
| 59 | + mMultiTouchC = new MultiTouchController<Object>(this); |
| 60 | + mCurrTouchPoint = new PointInfo(); |
| 61 | + mGestureDetector = new GestureDetector(context, new GestureListener()); |
| 62 | + setBackgroundColor(Color.BLACK); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * |
| 67 | + * @param context |
| 68 | + */ |
| 69 | + public AfdView(Context context) { |
| 70 | + super(context); |
| 71 | + setup(context); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * |
| 76 | + * @param context |
| 77 | + */ |
| 78 | + public AfdView(Context context, AttributeSet set) { |
| 79 | + super(context, set); |
| 80 | + setup(context); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * |
| 85 | + * @param context |
| 86 | + */ |
| 87 | + public AfdView(Context context, AttributeSet set, int arg) { |
| 88 | + super(context, set, arg); |
| 89 | + setup(context); |
| 90 | + } |
| 91 | + |
| 92 | + /* (non-Javadoc) |
| 93 | + * @see android.view.View.OnTouchListener#onTouch(android.view.View, android.view.MotionEvent) |
| 94 | + */ |
| 95 | + @Override |
| 96 | + public boolean onTouch(View view, MotionEvent e) { |
| 97 | + mGestureDetector.onTouchEvent(e); |
| 98 | + return mMultiTouchC.onTouchEvent(e); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @param name |
| 103 | + */ |
| 104 | + public void setBitmap(BitmapHolder holder) { |
| 105 | + mBitmap = holder; |
| 106 | + postInvalidate(); |
| 107 | + } |
| 108 | + |
| 109 | + /* (non-Javadoc) |
| 110 | + * @see com.ds.avare.MultiTouchController.MultiTouchObjectCanvas#getDraggableObjectAtPoint(com.ds.avare.MultiTouchController.PointInfo) |
| 111 | + */ |
| 112 | + public Object getDraggableObjectAtPoint(PointInfo pt) { |
| 113 | + return mBitmap; |
| 114 | + } |
| 115 | + |
| 116 | + /* (non-Javadoc) |
| 117 | + * @see com.ds.avare.MultiTouchController.MultiTouchObjectCanvas#getPositionAndScale(java.lang.Object, com.ds.avare.MultiTouchController.PositionAndScale) |
| 118 | + */ |
| 119 | + public void getPositionAndScale(Object obj, PositionAndScale objPosAndScaleOut) { |
| 120 | + objPosAndScaleOut.set(mPan.getMoveX(), mPan.getMoveY(), true, |
| 121 | + mScale.getScaleFactor(), false, 0, 0, false, 0); |
| 122 | + } |
| 123 | + |
| 124 | + /* (non-Javadoc) |
| 125 | + * @see com.ds.avare.MultiTouchController.MultiTouchObjectCanvas#selectObject(java.lang.Object, com.ds.avare.MultiTouchController.PointInfo) |
| 126 | + */ |
| 127 | + public void selectObject(Object obj, PointInfo touchPoint) { |
| 128 | + touchPointChanged(touchPoint); |
| 129 | + } |
| 130 | + |
| 131 | + /* (non-Javadoc) |
| 132 | + * @see com.ds.avare.MultiTouchController.MultiTouchObjectCanvas#setPositionAndScale(java.lang.Object, com.ds.avare.MultiTouchController.PositionAndScale, com.ds.avare.MultiTouchController.PointInfo) |
| 133 | + */ |
| 134 | + public boolean setPositionAndScale(Object obj,PositionAndScale newObjPosAndScale, PointInfo touchPoint) { |
| 135 | + touchPointChanged(touchPoint); |
| 136 | + if(false == mCurrTouchPoint.isMultiTouch()) { |
| 137 | + /* |
| 138 | + * Multi-touch is zoom, single touch is pan |
| 139 | + */ |
| 140 | + mPan.setMove(newObjPosAndScale.getXOff(), newObjPosAndScale.getYOff()); |
| 141 | + } |
| 142 | + else { |
| 143 | + /* |
| 144 | + * Clamp scaling. |
| 145 | + */ |
| 146 | + mScale.setScaleFactor(newObjPosAndScale.getScale()); |
| 147 | + } |
| 148 | + invalidate(); |
| 149 | + return true; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * @param touchPoint |
| 154 | + */ |
| 155 | + private void touchPointChanged(PointInfo touchPoint) { |
| 156 | + mCurrTouchPoint.set(touchPoint); |
| 157 | + invalidate(); |
| 158 | + } |
| 159 | + |
| 160 | + /* (non-Javadoc) |
| 161 | + * @see android.view.View#onDraw(android.graphics.Canvas) |
| 162 | + */ |
| 163 | + @Override |
| 164 | + public void onDraw(Canvas canvas) { |
| 165 | + if(mBitmap == null) { |
| 166 | + return; |
| 167 | + } |
| 168 | + if(mBitmap.getBitmap() == null) { |
| 169 | + return; |
| 170 | + } |
| 171 | + |
| 172 | + float min = Math.min(getWidth(), getHeight()) - 8; |
| 173 | + mPaint.setTextSize(min / 20); |
| 174 | + mPaint.setShadowLayer(0, 0, 0, Color.BLACK); |
| 175 | + |
| 176 | + float scale = mScale.getScaleFactor(); |
| 177 | + |
| 178 | + /* |
| 179 | + * A/FD |
| 180 | + */ |
| 181 | + mBitmap.getTransform().setScale(scale, scale); |
| 182 | + mBitmap.getTransform().postTranslate( |
| 183 | + mPan.getMoveX() * scale, |
| 184 | + mPan.getMoveY() * scale); |
| 185 | + canvas.drawBitmap(mBitmap.getBitmap(), mBitmap.getTransform(), mPaint); |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * @author zkhan |
| 190 | + * |
| 191 | + */ |
| 192 | + private class GestureListener extends GestureDetector.SimpleOnGestureListener { |
| 193 | + |
| 194 | + /* (non-Javadoc) |
| 195 | + * @see android.view.GestureDetector.SimpleOnGestureListener#onDoubleTap(android.view.MotionEvent) |
| 196 | + */ |
| 197 | + @Override |
| 198 | + public boolean onDoubleTap(MotionEvent e) { |
| 199 | + /* |
| 200 | + * On double tap, move to center |
| 201 | + */ |
| 202 | + mPan = new Pan(); |
| 203 | + return true; |
| 204 | + } |
| 205 | + |
| 206 | + /* (non-Javadoc) |
| 207 | + * @see android.view.GestureDetector.SimpleOnGestureListener#onLongPress(android.view.MotionEvent) |
| 208 | + */ |
| 209 | + @Override |
| 210 | + public void onLongPress(MotionEvent e) { |
| 211 | + |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | +} |
0 commit comments