I have this code:
<ImageView
android:id="@+id/listitem_logo"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
and
imageview_logo.setImageBitmap(bit); // comes from assets
imageview_logo.setAdjustViewBounds(true);
imageview_logo.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageview_logo.setBackgroundColor(0x00000000);
imageview_logo.setPadding(0, 0, 0, 0);
imageview_logo.setVisibility(v.VISIBLE);
When loading the image this way, no scaling seems to have been done. However, if I load an image through setImageDrawable() r.res. the image inside the ImageView
is resized. However, I need to use setImageBitmap() since I load my images through assets folder.
Am I missing some setting here? That will make Android resize and scale the bitmap, so it uses the full width of the ImageView? I guess I can do it myself in code, but offhand I would think what I want to do would be supported just by setting some properties.
Displays image resources, for example Bitmap or Drawable resources. ImageView is also commonly used to apply tints to an image and handle image scaling.
Added in API level 1. public static final ImageView.ScaleType CENTER_CROP. Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or larger than the corresponding dimension of the view (minus padding). The image is then centered in the view ...
XML. This scale type scale the image uniformly . i.e., maintain the image's aspect ratio in order to make the dimensions(width and height) equal to or larger than the ImageView dimension.
As I needed a bit of time to figure out the complete code to make this work, I am posting a full example here relying on the previous answer:
ImageView bikeImage = (ImageView) view.findViewById(R.id.bikeImage);
AssetLoader assetLoader = new AssetLoader(getActivity());
Bitmap bitmap = assetLoader.getBitmapFromAssets(
Constants.BIKES_FOLDER + "/" + bike.getName().toLowerCase()
.replace(' ', '_').replace('-', '_') + ".png");
int width = getActivity().getResources().getDisplayMetrics().widthPixels;
int height = (width*bitmap.getHeight())/bitmap.getWidth();
bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
bikeImage.setImageBitmap(bitmap);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With