title: Drawable: Understanding canvas width and height
body: <p>I try to implement my own drawable that will corss fade from a simple colored rectangle (that should have the same size as the ImageView where the drawable is in) to a loaded bitmap (over http).</p>
  
  <p>So what I do is override the draw() method of my Drawable:</p>
  
  <pre><code>public void draw(Canvas canvas) {
          boolean done = true;
  
          Log.d("Test",
                  "canvas w: " + canvas.getWidth() + " " + canvas.getHeight());
  
  
          final int alpha = mAlpha;
          final boolean crossFade = mCrossFade;
  
          Paint paint = mStartPaint;
          paint.setColor(blueColor);
  
          if (!crossFade || 255 - alpha &gt; 0) {
              if (crossFade) {
                  paint.setAlpha(255 - alpha);
              }
  
              // Draw the rect with the color
              canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), paint);
  
              if (crossFade) {
                  paint.setAlpha(0xFF);
              }
  
          }
  
          if (alpha &gt; 0) {
              Bitmap bitmap = mEnd;
              paint = mEndPaint;
              paint.setAlpha(alpha);
              if (mBitmapScalingType == null)
                  canvas.drawBitmap(bitmap, mEndX, mEndY, paint);
              else
                  mBitmapScalingType.draw(bitmap, canvas.getWidth(),
                          canvas.getHeight(), canvas, paint);
  
              paint.setAlpha(0xFF);
          }
  
          if (!done) {
              mHandler.post(mInvalidater);
          }
      }
  </code></pre>
  
  <p>So the canvas.getWidth() and canvas.getHeight() returns 128 (width) and 150 (height) which is the same as the ImageView has. But the result is:</p>
  
  <p><img src="https://i.stack.imgur.com/ya0XB.png" alt="enter image description here"></p>
  
  <p>The blue rectangle has not been painted over the complete canvas and I can't get figure out why.</p>
  
  <p>The ImageView that displays my fadeable drawable has no specific scaletype set.</p>
  
  <p>Any ideas what could be wrong?</p>
  
  <p>I have also noticed that the width and height calculated from setBounds() is 180 x 212 px</p>
  
  <pre><code>@Override
  public void setBounds(int left, int top, int right, int bottom) {
      super.setBounds(left, top, right, bottom);
  
      final int width = right - left;
      final int height = bottom - top;
              Log.d("Test", "width: "+width+" height: "+height);
       } 
  </code></pre>
  
  <p>and if I draw the color with 180 x 212 px</p>
  
  <pre><code>canvas.drawRect(0, 0, 180, 212, paint);
  </code></pre>
  
  <p>the rectangular is now drawn to fill the canvas completely.</p>
  
  <p>Any idea what could be wrong?</p>
  
  <p>Btw. The ImageView is defined like that:</p>
  
  <pre><code>&lt;ImageView android:id="@+id/playerPic"
             android:layout_width="64dp"
             android:layout_height="75dp"           
             android:layout_marginRight="10dp"
             /&gt;
  </code></pre>
  
  <p>in xhdpi 64dp = 128px and 75dp = 150 px</p>