Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Custom image view breaks in Honeycomb
- public class CustomMapView extends View {
- Bitmap map;
- // These matrices will be used to move and zoom image
- Matrix matrix = new Matrix();
- Matrix savedMatrix = new Matrix();
- // We can be in one of these 3 states
- static final int NONE = 0;
- static final int DRAG = 1;
- static final int ZOOM = 2;
- private static final float LOWER_BOUND = 0;
- int mode = NONE;
- PointF start = new PointF();
- PointF mid = new PointF();
- float oldDist = 0;
- private int width;
- private int height;
- public CustomMapView(final Context context, AttributeSet attrs) throws NoMapDefinedException {
- super(context, attrs);
- // currently grabbing from assets directory, but same artifacts appear when
- // grabbing from a drawable
- AssetManager assets = context.getResources().getAssets();
- InputStream buffer = null;
- try {
- buffer = new BufferedInputStream((assets.open("images/custom_map.jpg")));
- map = BitmapFactory.decodeStream(buffer);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public final void zoomOut() {
- float scale = 0.9f;
- matrix.postScale(scale, scale, mid.x, mid.y);
- invalidate();
- }
- public final void zoomIn() {
- float scale = 1.1f;
- matrix.postScale(scale, scale, mid.x, mid.y);
- invalidate();
- }
- @Override
- protected final void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- super.onMeasure(widthMeasureSpec, heightMeasureSpec);
- int width = MeasureSpec.getSize(widthMeasureSpec);
- int height = MeasureSpec.getSize(heightMeasureSpec);
- if (height > 0) {
- this.setMeasuredDimension(width, height);
- this.width = width;
- this.height = height;
- }
- }
- @Override
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- canvas.drawBitmap(map, matrix, new Paint());
- }
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- // Handle touch events here...
- campusMapInterface.hideKey();
- switch (event.getAction() & MotionEvent.ACTION_MASK) {
- case MotionEvent.ACTION_DOWN:
- savedMatrix.set(matrix);
- start.set(event.getX(), event.getY());
- mode = DRAG;
- break;
- case MotionEvent.ACTION_UP:
- case MotionEvent.ACTION_POINTER_UP:
- mode = NONE;
- break;
- case MotionEvent.ACTION_MOVE:
- if (mode == DRAG) {
- matrix.set(savedMatrix);
- matrix.postTranslate(event.getX() - start.x, event.getY() - start.y);
- } else if (mode == ZOOM) {
- float newDist = spacing(event);
- if (newDist > 10f) {
- matrix.set(savedMatrix);
- float scale = newDist / oldDist;
- matrix.postScale(scale, scale, mid.x, mid.y);
- }
- }
- break;
- case MotionEvent.ACTION_POINTER_DOWN:
- oldDist = spacing(event);
- if (oldDist > 10f) {
- savedMatrix.set(matrix);
- midPoint(mid, event);
- mode = ZOOM;
- }
- break;
- }
- invalidate();
- return true; // indicate event was handled
- }
- private float spacing(MotionEvent event) {
- float x = event.getX(0) - event.getX(1);
- float y = event.getY(0) - event.getY(1);
- return FloatMath.sqrt((x * x) + (y * y));
- }
- private void midPoint(PointF point, MotionEvent event) {
- float x = event.getX(0) + event.getX(1);
- float y = event.getY(0) + event.getY(1);
- point.set(x / 2, y / 2);
- }
- }
Add Comment
Please, Sign In to add comment