Fit image into ImageView, keep aspect ratio and then resize ImageView to image dimensions?

前端 未结 17 869
无人共我
无人共我 2020-11-22 17:07

How to fit an image of random size to an ImageView?
When:

  • Initially ImageView dimensions are 250dp * 250dp
  • The image\'
相关标签:
17条回答
  • 2020-11-22 17:23

    This did it for my case.

                 <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:scaleType="centerCrop"
                    android:adjustViewBounds="true"
                    />
    
    0 讨论(0)
  • 2020-11-22 17:24

    (The answer was heavily modified after clarifications to the original question)

    After clarifications:
    This cannot be done in xml only. It is not possible to scale both the image and the ImageView so that image's one dimension would always be 250dp and the ImageView would have the same dimensions as the image.

    This code scales Drawable of an ImageView to stay in a square like 250dp x 250dp with one dimension exactly 250dp and keeping the aspect ratio. Then the ImageView is resized to match the dimensions of the scaled image. The code is used in an activity. I tested it via button click handler.

    Enjoy. :)

    private void scaleImage(ImageView view) throws NoSuchElementException  {
        // Get bitmap from the the ImageView.
        Bitmap bitmap = null;
    
        try {
            Drawable drawing = view.getDrawable();
            bitmap = ((BitmapDrawable) drawing).getBitmap();
        } catch (NullPointerException e) {
            throw new NoSuchElementException("No drawable on given view");
        } catch (ClassCastException e) {
            // Check bitmap is Ion drawable
            bitmap = Ion.with(view).getBitmap();
        }
    
        // Get current dimensions AND the desired bounding box
        int width = 0;
    
        try {
            width = bitmap.getWidth();
        } catch (NullPointerException e) {
            throw new NoSuchElementException("Can't find bitmap on given view/drawable");
        }
    
        int height = bitmap.getHeight();
        int bounding = dpToPx(250);
        Log.i("Test", "original width = " + Integer.toString(width));
        Log.i("Test", "original height = " + Integer.toString(height));
        Log.i("Test", "bounding = " + Integer.toString(bounding));
    
        // 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) bounding) / width;
        float yScale = ((float) bounding) / height;
        float scale = (xScale <= yScale) ? xScale : yScale;
        Log.i("Test", "xScale = " + Float.toString(xScale));
        Log.i("Test", "yScale = " + Float.toString(yScale));
        Log.i("Test", "scale = " + Float.toString(scale));
    
        // 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);
        width = scaledBitmap.getWidth(); // re-use
        height = scaledBitmap.getHeight(); // re-use
        BitmapDrawable result = new BitmapDrawable(scaledBitmap);
        Log.i("Test", "scaled width = " + Integer.toString(width));
        Log.i("Test", "scaled height = " + Integer.toString(height));
    
        // 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);
    
        Log.i("Test", "done");
    }
    
    private int dpToPx(int dp) {
        float density = getApplicationContext().getResources().getDisplayMetrics().density;
        return Math.round((float)dp * density);
    }
    

    The xml code for the ImageView:

    <ImageView a:id="@+id/image_box"
        a:background="#ff0000"
        a:src="@drawable/star"
        a:layout_width="wrap_content"
        a:layout_height="wrap_content"
        a:layout_marginTop="20dp"
        a:layout_gravity="center_horizontal"/>
    


    Thanks to this discussion for the scaling code:
    http://www.anddev.org/resize_and_rotate_image_-_example-t621.html


    UPDATE 7th, November 2012:
    Added null pointer check as suggested in comments

    0 讨论(0)
  • 2020-11-22 17:24

    Quick answer:

    <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="center"
            android:src="@drawable/yourImage"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
    0 讨论(0)
  • 2020-11-22 17:25

    try adding android:scaleType="fitXY" to your ImageView.

    0 讨论(0)
  • 2020-11-22 17:27

    After searching for a day, I think this is the easiest solution:

    imageView.getLayoutParams().width = 250;
    imageView.getLayoutParams().height = 250;
    imageView.setAdjustViewBounds(true);
    
    0 讨论(0)
  • 2020-11-22 17:28

    this can all be done using XML... the other methods seem pretty complicated. Anyway, you just set the height to what ever you want in dp, then set the width to wrap content or visa versa. Use scaleType fitCenter to adjust the size of the image.

    <ImageView
        android:layout_height="200dp"
        android:layout_width="wrap_content"
        android:scaleType="fitCenter"
        android:adjustViewBounds="true"
        android:src="@mipmap/ic_launcher"
        android:layout_below="@+id/title"
        android:layout_margin="5dip"
        android:id="@+id/imageView1">
    
    0 讨论(0)
提交回复
热议问题