简体   繁体   English

通过动态设置高度来裁剪图像

[英]Cropping image by setting height dynamically

Guess a picture of man. 猜猜男人的照片。 (Head at top & legs at bottom) :P (头在上,腿在下):P

I am setting height dynamically using layoutparam. 我正在使用layoutparam动态设置高度。

When I set height for instance 300, 当我设定高度为300时,

It sets height from HEAD to 300 units toward legs of image. 它将从HEAD到图像腿的高度设置为300个单位。

What I want is to set height from LEGs to top 300 units. 我想要的是将LEG的高度设置为前300个单位。

imageView.setHeight()

Use this method in your java class: 在您的java类中使用此方法:

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(150);// set height
    Logger.i("Test", "original width = " + Integer.toString(width));
    Logger.i("Test", "original height = " + Integer.toString(height));
    Logger.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;
    Logger.i("Test", "xScale = " + Float.toString(xScale));
    Logger.i("Test", "yScale = " + Float.toString(yScale));
    Logger.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);
    Logger.i("Test", "scaled width = " + Integer.toString(width));
    Logger.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);

    Logger.i("Test", "done");
    }

    private int dpToPx(int dp) {
    float density = getApplicationContext().getResources().getDisplayMetrics().density;
    return Math.round((float) dp * density);
    }

Now, how to call this methord : 现在,如何调用此方法:

        Bitmap bitmap;

            try {
                bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImage);
                imageView.setImageBitmap(bitmap);
                scaleImage(imageView);// call your imageview
            } catch (IOException e) {
                e.printStackTrace();
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM