/**
   * Get a new instance of the dialog fragment with an image in it
   *
   * @param arg argument for the bundle
   * @param bmp {@link Bitmap} image to be put in the dialog
   * @param activity {@link Activity} get the parent activity
   * @return the fragment to be shown
   */
  public static ImageDialogFragment newInstance(int arg, Bitmap bmp, Activity activity) {
    frag = new ImageDialogFragment();
    Bundle args = new Bundle();
    args.putInt("count", arg);
    frag.setArguments(args);

    // Get width and height of device
    act = activity;
    displaymetrics = new DisplayMetrics();
    frag.setBitmap(bmp);
    fullScreen();
    return frag;
  }
  @Override
  public View onCreateView(
      final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.image_dialog_fragment, container, false);

    ImageView mImageView = (ImageView) view.findViewById(R.id.image_dialog);
    if (orient != getResources().getConfiguration().orientation) {
      if (orient == LANDSCAPE) {
        orient = PORTRAIT;
        fullScreen();
      } else if (orient == PORTRAIT) {
        orient = LANDSCAPE;
        fullScreen();
      } else {
        orient = getResources().getConfiguration().orientation;
      }
    }

    mImageView.setImageBitmap(mBitmap);

    mImageView.setOnTouchListener(
        new View.OnTouchListener() {
          @Override
          public boolean onTouch(View v, MotionEvent event) {
            ImageView view = (ImageView) v;

            // Handle touch events here...
            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_POINTER_DOWN:
                oldDist = spacing(event);
                if (oldDist > 10f) {
                  savedMatrix.set(matrix);
                  midPoint(mid, event);
                  mode = ZOOM;
                }
                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[] f = new float[9];

                  float newDist = spacing(event);
                  if (newDist > 10f) {
                    matrix.set(savedMatrix);
                    float scale = newDist / oldDist;
                    matrix.postScale(scale, scale, mid.x, mid.y);
                  }

                  matrix.getValues(f);
                  float scaleX = f[Matrix.MSCALE_X];
                  float scaleY = f[Matrix.MSCALE_Y];

                  if (scaleX <= 0.7f) {
                    matrix.postScale((0.7f) / scaleX, (0.7f) / scaleY, mid.x, mid.y);
                  } else if (scaleX >= 2.5f) {
                    matrix.postScale((2.5f) / scaleX, (2.5f) / scaleY, mid.x, mid.y);
                  }
                }
                break;
            }
            limitDrag(matrix, view, view.getWidth(), view.getHeight());
            view.setImageMatrix(matrix);
            return true;
          }
        });

    return view;
  }