ImageView scaling does not work

I searched for this question, but did not like to answer for me.

This is mine ImageView: enter image description here

And this is 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 can I scale this image, for example, on an image?

PS This image is downloaded from the Internet.

UPD 1. Added ImageLoader code.

DisplayImageOptions options = new DisplayImageOptions.Builder()
            .showImageForEmptyUri(R.drawable.place_holder16_9)
            .cacheInMemory(true)
            .cacheOnDisk(true)
            .build();
ImageLoader.getInstance().displayImage(promo.getImageBigUrl(), mCoverImageView, options);
+4
source share
5 answers

Sorry, but you cannot do this anymore using this:

  android:layout_width="match_parent"

This already means "take as much width as you can." Since your width has a maximum value, the image height also takes a maximum value.

You can verify this using:

android:scaleX="3"
android:scaleY="3"

He must increase it three times. If this does not work, use:

android:scaleX="0.2"
android:scaleY="0.2"

. ,

,

+1

LayoutParams .

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);

ImageView, .

+1

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 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);
}
+1
source

You can set Bitmapas 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));
    } 
}); 

Link Link

+1
source

Try using the ScaleType attribute for your ImageView in your xml. for example

android:scaleType="centerCrop".
0
source

Source: https://habr.com/ru/post/1623978/


All Articles