title: setImageBitmap in gridview does not scale the image to fit
body: <p>Note: This problem only happens in gridview, if you setimagebitmap in a single imageview, it works well.</p>
  
  <p>See the following pictures, The left picture is what I want, and the picture on right is what actually I get.</p>
  
  <p><img src="https://i.stack.imgur.com/afejU.png" alt="enter image description here"></p>
  
  <p>The only difference is the way I set the image, if I set the image as below, I get the correct result(left picture above).</p>
  
  <pre><code>holder.image.setImageResource(imagesArray.get(position));
  </code></pre>
  
  <p>But if I set the image with bitmap as below, the image not scaled as I want(the right picture above)</p>
  
  <pre><code>holder.imageView.setImageBitmap(bitmap);
  </code></pre>
  
  <p>Why I set the image as bitmap instead of drawable?
  because my images were encrypted, I need to decrypt them before load into the imageview. the basic step is:</p>
  
  <ol>
  <li>depryct the image into byte arrya. </li>
  <li>create bitmap from the bytes.</li>
  <li>load the bitmap into imageview.</li>
  </ol>
  
  <p>Note, the problem was not caused by the decryption, I have test with an normal image without decryption, it does not work either.</p>
  
  <p>Here is the code</p>
  
  <p>GridViewActivity</p>
  
  <pre><code>public class GridViewActivity extends Activity {
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_gridview);
  
          GridView gridView = (GridView)findViewById(R.id.grid_view);
          ImageAdapter imageAdapter = ImageAdapter.getInstance();
          imageAdapter.setContext(this);
          gridView.setAdapter(imageAdapter);
  
          for (int i = 0; i &lt; 50; i++) {
              int resourceId = getResources().getIdentifier("myimage", "drawable", getPackageName());
              imageAdapter.imagesArray.add(resourceId);
          }
      }
  }
  </code></pre>
  
  <p>ImageAdapter</p>
  
  <pre><code>public class ImageAdapter extends BaseAdapter {
      private static ImageAdapter imageAdapter = null;
      private Bitmap bitmap;
      private Context mContext;
      public ArrayList&lt;Integer&gt; imagesArray = new ArrayList&lt;Integer&gt;();
  
      private ImageAdapter() {
      }
  
      public static ImageAdapter getInstance() {
          if (imageAdapter == null) {
              imageAdapter = new ImageAdapter();
          }
  
          return imageAdapter;
      }
  
      public void setContext(Context context) {
          mContext = context;
      }
  
      @Override
      public int getCount() {
          return imagesArray.size();
      }
  
      @Override
      public Object getItem(int position) {
          return imagesArray.get(position);
      }
  
      @Override
      public long getItemId(int position) {
          return 0;
      }
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
          View row = convertView;
          ViewHolder holder;
  
          if (row == null) {
              LayoutInflater inflater = ((Activity)mContext).getLayoutInflater();
              row = inflater.inflate(R.layout.row_grid, parent, false);
              holder = new ViewHolder();
              holder.imageView = (ImageView)row.findViewById(R.id.image);
              row.setTag(holder);
          }
          else {
              holder = (ViewHolder)row.getTag();
          }
  
          // Create bitmap from image in assets folder.
          String imageFileName = "myimage.jpg";
          AssetManager assetMgr = mContext.getAssets();
          try {
              InputStream fis = assetMgr.open(imageFileName);
              bitmap = BitmapFactory.decodeStream(fis);
          }
          catch (Exception ex) {
              Log.i("zdd", ex.getLocalizedMessage());
          }
  
          holder.imageView.setImageBitmap(bitmap);
          //holder.image.setImageResource(imagesArray.get(position));
  
          return row;
      }
  
      static class ViewHolder {
          ImageView imageView;
      }
  }
  </code></pre>
  
  <p>SquareImageView</p>
  
  <pre><code>public class SquareImageView extends ImageView
  {
      public SquareImageView(Context context)
      {
          super(context);
      }
  
      public SquareImageView(Context context, AttributeSet attrs)
      {
          super(context, attrs);
      }
  
      public SquareImageView(Context context, AttributeSet attrs, int defStyle)
      {
          super(context, attrs, defStyle);
      }
  
      @Override
      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
      {
          super.onMeasure(widthMeasureSpec, heightMeasureSpec);
          setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()); //Snap to width
      }
  }
  </code></pre>
  
  <p>Here is the layout</p>
  
  <p>activity_gridview.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"&gt;
  
      &lt;GridView xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/grid_view"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="#7f7f7f"
          android:numColumns="4"
          android:horizontalSpacing="2dp"
          android:verticalSpacing="2dp"
          android:gravity="center"
          android:stretchMode="columnWidth"
          android:layout_alignParentLeft="true"
          android:layout_alignParentStart="true"
          android:layout_alignParentRight="true"
          android:layout_alignParentEnd="true"&gt;
      &lt;/GridView&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>row_grid.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:orientation="vertical"
      android:padding="5dp"
      android:background="#ffffff"&gt;
      &lt;com.jiyuzhai.setimagebitmaptest.SquareImageView
          android:id="@+id/image"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:scaleType="centerCrop"/&gt;
  &lt;/LinearLayout&gt;
  </code></pre>