title: set position of programmatically created imageview android
body: <p>I am just trying to position my imageView on desired place on screen, but for some reason, picture is always a little bit off, it is not in the right place. For example, this is my code if I want it to put in the middle of the screen.</p>
  
  <p>Getting the screen size:</p>
  
  <pre><code>    DisplayMetrics displaymetrics = new DisplayMetrics();
      getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
      height = displaymetrics.heightPixels;
      width = displaymetrics.widthPixels;
  </code></pre>
  
  <p>Getting the picture size: </p>
  
  <pre><code>    BitmapFactory.Options o = new BitmapFactory.Options();
      o.inTargetDensity = DisplayMetrics.DENSITY_DEFAULT;
      Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.car1, o);
      int w = bmp.getWidth();
      int h = bmp.getHeight();
  </code></pre>
  
  <p>Creating and positioning imageView:</p>
  
  <pre><code>    image = new ImageView(this);
  
  
      RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                              RelativeLayout.LayoutParams.MATCH_PARENT,
                              RelativeLayout.LayoutParams.MATCH_PARENT);
  
      image.setScaleType(ImageView.ScaleType.MATRIX);
      image.setImageResource(R.drawable.car1);
      image.setId(1);
  
  
      params.setMargins((width/2)-w,(height/2)-h, 0, 0);
  
  
      ((ViewGroup) mainLayout).addView(image, params);
  </code></pre>
  
  <p>My imageView is not on the middle of the screen and I don't know why, any help? I took middle of the screen as an example; I need various positions. </p>