Reputation: 852
I googled this question but didn't fond answer for me.
And this this ImageView in xml:
<ImageView
android:id="@+id/stock_cover"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="@drawable/place_holder16_9" />
How I can scale this image like on example image?
P.S. This image I download from internet.
UPD 1. ImageLoader code added.
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageForEmptyUri(R.drawable.place_holder16_9)
.cacheInMemory(true)
.cacheOnDisk(true)
.build();
ImageLoader.getInstance().displayImage(promo.getImageBigUrl(), mCoverImageView, options);
Upvotes: 4
Views: 1478
Reputation: 2825
You can set Bitmap
as background.
// Load image, decode it to Bitmap and return Bitmap to callback
imageLoader.loadImage(imageUri, new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
// Do whatever you want with Bitmap
mCoverImageView.setBackground(new BitmapDrawable(getResources(), bitmap));
}
});
Upvotes: 1
Reputation: 2007
Try this code:
private void scaleImage(ImageView view, int boundBoxInDp)
{
// Get the ImageView and its bitmap
Drawable drawing = view.getDrawable();
Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap();
// Get current dimensions
int width = bitmap.getWidth();
int height = bitmap.getHeight();
// Determine how much to scale: the dimension requiring less scaling is
// closer to the its side. This way the image always stays inside your
// bounding box AND either x/y axis touches it.
float xScale = ((float) boundBoxInDp) / width;
float yScale = ((float) boundBoxInDp) / height;
float scale = (xScale <= yScale) ? xScale : yScale;
// Create a matrix for the scaling and add the scaling data
Matrix matrix = new Matrix();
matrix.postScale(scale, scale);
// Create a new bitmap and convert it to a format understood by the ImageView
Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
BitmapDrawable result = new BitmapDrawable(scaledBitmap);
width = scaledBitmap.getWidth();
height = scaledBitmap.getHeight();
// Apply the scaled bitmap
view.setImageDrawable(result);
// Now change ImageView's dimensions to match the scaled image
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
params.width = width;
params.height = height;
view.setLayoutParams(params);
}
private int dpToPx(int dp)
{
float density = getApplicationContext().getResources().getDisplayMetrics().density;
return Math.round((float)dp * density);
}
Upvotes: 1
Reputation: 3346
Set LayoutParams
from code.
ImageView image = new ImageView(this);
image.setImageResource(getResources().getIdentifier('file_name', "drawable", getPackageName()));
image.setAdjustViewBounds(true);
image.setScaleType(ImageView.ScaleType.CENTER_CROP);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
image.setLayoutParams(layoutParams);
parent.addView(image);
It's a known issue with ImageView, which won't upscale small images.
Upvotes: 1
Reputation: 19351
Sorry, but you cannot make it bigger as you use this:
android:layout_width="match_parent"
It already means "take how much width as you can". As your width has the maximum value, your image height also takes a maximum value.
You can check it by using:
android:scaleX="3"
android:scaleY="3"
It should make it bigger three times. If it won't work use:
android:scaleX="0.2"
android:scaleY="0.2"
It should make it smaller five times. I'm pretty sure that it would work
Hope it help
Upvotes: 1
Reputation: 4091
Try using ScaleType attribute for your ImageView in your xml. For example
android:scaleType="centerCrop".
Upvotes: 0