适合图像转换成ImageView的,保持宽高比,然后调整ImageView的图像尺寸是多少?图像、转换成、尺寸、适合

由网友(乜許、莪累孓)分享简介:如何适应任意大小的图像到的ImageView ?当:How to fit an image of random size to an ImageView?When:在最初的ImageView 尺寸250dp * 250dp 在图像的较大尺寸应放大/缩小到250dp 图像应保持其高宽比的的ImageView...

如何适应任意大小的图像到的ImageView ? 当:

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

在最初的ImageView 尺寸250dp * 250dp 在图像的较大尺寸应放大/缩小到250dp 图像应保持其高宽比 的的ImageView 尺寸应与缩放后缩放图像的尺寸 Initially ImageView dimensions are 250dp * 250dp The image's larger dimension should be scaled up/down to 250dp The image should keep its aspect ratio The ImageView dimensions should match scaled image's dimensions after scaling

例如。为100 * 150的图像,图像和的ImageView 应该是166 * 250。 例如。为150 * 100的图像,图像和的ImageView 应该是250 * 166。

E.g. for an image of 100*150, the image and the ImageView should be 166*250. E.g. for an image of 150*100, the image and the ImageView should be 250*166.

如果我设定的界限为

<ImageView
    android:id="@+id/picture"
    android:layout_width="250dp"
    android:layout_height="250dp"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="20dp"
    android:adjustViewBounds="true" />

图像合身的的ImageView ,而的ImageView 总是250dp * 250dp。

images fit properly in the ImageView, but the ImageView is always 250dp * 250dp.

推荐答案

(答案澄清后大量修改原来的问题)的

在澄清: 这不能在XML中只有完成。这是不可能的规模既形象和的ImageView ,使图像的一个维度将永远是250dp和的ImageView 将具有相同的尺寸的图像。

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.

这code秤 绘制对象的ImageView 留在方像250dp x 250dp与一个维度恰好250dp和保持宽高比。那么的ImageView 调整大小以匹配缩放图像的尺寸。在code被用在一个活动。我通过点击按钮的处理程序进行了测试。

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.

享受。 :)的

private void scaleImage(ImageView view) throws NoSuchElementException  {
    Bitmap bitmap = null;
    try { 
            Drawable drawing = view.getDrawable();
            bitmap = ((BitmapDrawable) drawing).getBitmap();
    } catch(NullPointerException npx) {
            // +log exception
            throw new NoSuchElementException("No drawable on given view!");
    } catch (ClassCastException cce) { 
            // +log exception 
            /**to check if it 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 npx) {
        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);
}

在XML code为的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"/>

由于本次讨论的尺度code: http://www.anddev.org/resize_and_rotate_image_-_example-t621.html

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

更新7日,2012年11月: 添加空指针检查中提出的意见

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

阅读全文

相关推荐

最新文章