title: Applying a BitmapShader using relative coordinates when using canvas.drawBitmap
body: <p>I'm trying to Use a BitmapShader to draw on a Canvas. I have 2 images of the same dimensions (one for the alfa channel and one for RGB), that I want to draw on a canvas that was created with the same size. The problem is that it seem as though canvas.drawBitmap users absolute coordinates for the paint. is there a way to draw with relative coordinates.</p>
  
  <pre><code>public class MaskedImageDrawable extends Drawable {
      private final Bitmap mMask;
  
      private final Paint mPaint = new Paint();
  
      public MaskedImageDrawable(Bitmap mask, Bitmap image) {
          mMask = mask;
  
          Shader targetShader =
              new BitmapShader(image, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
          mPaint.setShader(targetShader);
      }
  
      @Override
      public void draw(Canvas canvas) {
          canvas.drawBitmap(mMask, 0.0f, 0.0f, mPaint);
      }
  
      @Override
      public int getOpacity() {
          return PixelFormat.TRANSLUCENT;
      }
  
      @Override
      public void setAlpha(int alpha) {
          mPaint.setAlpha(alpha);
      }
  
      @Override
      public void setColorFilter(ColorFilter cf) {
          mPaint.setColorFilter(cf);
      }
  }
  </code></pre>
  
  <p>I can't seem to be able to get the drawable's absolute coordinates on the screen. I know I can use getLocationOnScreen/InWindow from the view that holds the drawable but that doesn't take animations into account. </p>
  
  <p>I tried tried applying relative coordinates by applying a translation on the shader in onBoundsChange but the coordinates here seems to be relative as well.</p>
  
  <pre><code>  Matrix mTranslationMatrix =  new Matrix()
  
    @Override
    protected void onBoundsChange(Rect bounds) {
        super.onBoundsChange(bounds);
        mTranslationMatrix.setTranslate(bounds.left, bounds.top);
        mPaint.getShader().setLocalMatrix(mTranslationMatrix);
    }
  </code></pre>
  
  <p>As a reference - it seems drawRect with the same paint will use relative coordinates. so replacing the draw method with the following will render the RGB image on the drawable correctly (but not the mask)</p>
  
  <pre><code>@Override
  public void draw(Canvas canvas) {
      // getBounds returns the relative coordinates as well
      canvas.drawRect(getBounds, mPaint);
  }
  </code></pre>