title: Android drawing a custom view onDraw(). only draw a half of the image
body: <p>Im extending the View class in order to have a "drawable" view witch contains the desired background. </p>
  
  <p>The problem is when this class works fine in other activity (same app). </p>
  
  <p>Here is the class that extends View: </p>
  
  <pre><code>public class BackgroundMap extends View {
  
  private String strmap;
  private int totalWidth;
  
  public BackgroundMap(Context context, String map, int w, int h) {
      super(context);
      super.setLayoutParams(new LayoutParams(w, h));
      totalWidth = w;
      strmap = map;
  
  }
  @Override
  protected void onDraw(Canvas canvas) {
      super.onDraw(canvas);
  
      Resources res = getResources();
      int eachBoxSize = totalWidth/10;
      float left = 0;
      float top = 0;
  
      Paint paint = new Paint();
  
      for(char c : strmap.toCharArray()){
  
          Bitmap bm = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(res, Terrain.getResource(c)), eachBoxSize, eachBoxSize, false);
  
          canvas.drawBitmap(bm, left, top, paint);
  
          if(left == totalWidth - eachBoxSize){
              left = 0;
              top += eachBoxSize; 
          }else{
              left += eachBoxSize;
          }
  
      }
  
  
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>This view works like this: somelayout.addView(new BackgroundMap(arg..));</p>
  
  <p>the String map argument means an array of terrains (char) ids that point to several little png images.</p>
  
  <p>and this is how i use it in my Activity: </p>
  
  <pre><code>ly_rbtn_image = (FrameLayout) findViewById(R.id.ly_rbtn_image);
  
          ly_rbtn_image.post(new Runnable() {
  
              @Override
              public void run() {
                       imagewidth = ly_rbtn_image.getWidth();
                       ly_rbtn_image.addView(new BackgroundMap(getApplicationContext(), str, imagewidth, imagewidth));
  
              }
          });
  </code></pre>
  
  <p>i check with logcat the width of parent layout and it has 225px. As you can see the image  below only build the first row of bitmaps... but this IS NOT a loop problem</p>
  
  <p>I check inside the custom view loop at onDraw method if there was a problem here but it iterate the whole string (100 chars, 10x10 bitmaps)</p>
  
  <p>hope u can help me :S</p>
  
  <p><a href="http://deviantsart.com/1afcam8.png" rel="nofollow noreferrer">http://deviantsart.com/1afcam8.png</a></p>