title: How to programmatically re-apply a 9-patch image to an ImageView?
body: <p>I have an activity that has an ImageView defined inside a HorizontalScrollView. The image source is a 9-patch file that is constrained to stretch only the right edge to fill the screen. I have implemented a simple zoom feature which allows the user to double tap to zoom in and out, by resizing the bitmap and assigning the new bitmap to the view. My current problem is that when doubling tapping to zoom back out, the 9-patch is not applied when I assign the new resized bitmap to the view. In other words, instead of stretching just the right edge as defined in the 9-patch file, it stretched the entire image. </p>
  
  <p>Here is my XML:</p>
  
  <pre><code>&lt;HorizontalScrollView
              android:id="@+id/hScroll"
              android:fillViewport="true"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:fadingEdge="none" &gt;
  
              &lt;RelativeLayout
                  android:id="@+id/rlayoutScrollMap"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent" &gt;
  
                  &lt;ImageView
                      android:id="@+id/imgResultMap"
                      android:layout_width="fill_parent"
                      android:layout_height="fill_parent"
                      android:scaleType="fitXY"
                      android:src="@drawable/map_base"/&gt;
  
              &lt;/RelativeLayout&gt;
          &lt;/horizontalScrollView&gt;
  </code></pre>
  
  <p>Here is the relevant portion of my code, inside the onDoubleTap() call :</p>
  
  <pre><code>public boolean onDoubleTap(MotionEvent e)  
                  {  
                      if (zoom == 1) {
                          zoom = 2; // zoom out
                      } else {
                          zoom = 1; // zoom in
                      }
                      Bitmap image = BitmapFactory.decodeResource(getResources(),R.drawable.map_base);            
                      Bitmap bmp = Bitmap.createScaledBitmap(image, image.getWidth() * zoom, image.getHeight() * zoom, false);
                      ImageView imgResultMap = (ImageView)findViewById(R.id.imgResultMap);
                      imgResultMap.setImageBitmap(bmp);
  
                      return false; 
                  } 
  </code></pre>
  
  <p><strong>EDIT</strong>: After doing some research, I have it figured out. Instead of just manipulating the bitmap, I need to also include the 9-patch chunk, which is not part of the bitmap image, to re-construct a new 9-patch drawable. See sample code below:</p>
  
  <pre><code>    ...
   else {
          // Zoom out
          zoom = 1;
          Bitmap mapBitmapScaled = mapBitmap; 
          // Load the 9-patch data chunk and apply to the view
          byte[] chunk = mapBitmap.getNinePatchChunk();
          NinePatchDrawable mapNinePatch = new NinePatchDrawable(getResources(), 
              mapBitmapScaled, chunk, new Rect(), null);
          imgResultMap.setImageDrawable(mapNinePatch);
   }
  
    ....
  </code></pre>