/**
   * Set the width and height of the canvas.
   *
   * @param width
   * @param height
   */
  public void setDimensions(int width, int height) {
    Utils.traceBeginSection("set dimensions");
    if (mWidth == width && mHeight == height) {
      Utils.traceEndSection();
      return;
    }

    mWidth = width;
    mHeight = height;

    mDividedBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    mCanvas = new Canvas(mDividedBitmap);

    for (int i = 0; i < getDivisionCount(); i++) {
      mDivisionImages.set(i, null);
    }
    mBitmapValid = false;
    Utils.traceEndSection();
  }
 /**
  * Get the desired dimensions and scale for the bitmap to be placed in the location corresponding
  * to id. Caller must allocate the Dimensions object.
  *
  * @param key
  * @param outDim a {@link ImageCanvas.Dimensions} object to write results into
  */
 @Override
 public void getDesiredDimensions(Object key, Dimensions outDim) {
   Utils.traceBeginSection("get desired dimensions");
   int w = 0, h = 0;
   float scale = 0;
   final Integer pos = mDivisionMap.get(transformKeyToDivisionId(key));
   if (pos != null && pos >= 0) {
     final int size = mDivisionMap.size();
     switch (size) {
       case 0:
         break;
       case 1:
         w = mWidth;
         h = mHeight;
         scale = Dimensions.SCALE_ONE;
         break;
       case 2:
         w = mWidth / 2;
         h = mHeight;
         scale = Dimensions.SCALE_HALF;
         break;
       case 3:
         switch (pos) {
           case 0:
             w = mWidth / 2;
             h = mHeight;
             scale = Dimensions.SCALE_HALF;
             break;
           default:
             w = mWidth / 2;
             h = mHeight / 2;
             scale = Dimensions.SCALE_QUARTER;
         }
         break;
       case 4:
         w = mWidth / 2;
         h = mHeight / 2;
         scale = Dimensions.SCALE_QUARTER;
         break;
     }
   }
   outDim.width = w;
   outDim.height = h;
   outDim.scale = scale;
   Utils.traceEndSection();
 }
 private void addOrClearDivisionImage(Bitmap b, Object key) {
   Utils.traceBeginSection("add or clear division image");
   final Integer pos = mDivisionMap.get(transformKeyToDivisionId(key));
   if (pos != null && pos >= 0) {
     mDivisionImages.set(pos, b);
     boolean complete = false;
     final int width = mWidth;
     final int height = mHeight;
     // Different layouts depending on count.
     final int size = mDivisionMap.size();
     switch (size) {
       case 0:
         // Do nothing.
         break;
       case 1:
         // Draw the bitmap filling the entire canvas.
         draw(mDivisionImages.get(0), 0, 0, width, height);
         complete = true;
         break;
       case 2:
         // Draw 2 bitmaps split vertically down the middle
         switch (pos) {
           case 0:
             draw(mDivisionImages.get(0), 0, 0, width / 2, height);
             break;
           case 1:
             draw(mDivisionImages.get(1), width / 2, 0, width, height);
             break;
         }
         complete =
             mDivisionImages.get(0) != null && mDivisionImages.get(1) != null
                 || isPartialBitmapComplete();
         if (complete) {
           // Draw dividers
           drawVerticalDivider(width, height);
         }
         break;
       case 3:
         // Draw 3 bitmaps: the first takes up all vertical
         // space, the 2nd and 3rd are stacked in the second vertical
         // position.
         switch (pos) {
           case 0:
             draw(mDivisionImages.get(0), 0, 0, width / 2, height);
             break;
           case 1:
             draw(mDivisionImages.get(1), width / 2, 0, width, height / 2);
             break;
           case 2:
             draw(mDivisionImages.get(2), width / 2, height / 2, width, height);
             break;
         }
         complete =
             mDivisionImages.get(0) != null
                     && mDivisionImages.get(1) != null
                     && mDivisionImages.get(2) != null
                 || isPartialBitmapComplete();
         if (complete) {
           // Draw dividers
           drawVerticalDivider(width, height);
           drawHorizontalDivider(width / 2, height / 2, width, height / 2);
         }
         break;
       default:
         // Draw all 4 bitmaps in a grid
         switch (pos) {
           case 0:
             draw(mDivisionImages.get(0), 0, 0, width / 2, height / 2);
             break;
           case 1:
             draw(mDivisionImages.get(1), width / 2, 0, width, height / 2);
             break;
           case 2:
             draw(mDivisionImages.get(2), 0, height / 2, width / 2, height);
             break;
           case 3:
             draw(mDivisionImages.get(3), width / 2, height / 2, width, height);
             break;
         }
         complete =
             mDivisionImages.get(0) != null
                     && mDivisionImages.get(1) != null
                     && mDivisionImages.get(2) != null
                     && mDivisionImages.get(3) != null
                 || isPartialBitmapComplete();
         if (complete) {
           // Draw dividers
           drawVerticalDivider(width, height);
           drawHorizontalDivider(0, height / 2, width, height / 2);
         }
         break;
     }
     // Create the new image bitmap.
     if (complete) {
       mBitmapValid = true;
       mCallback.invalidate();
     }
   }
   Utils.traceEndSection();
 }