0

Guess a picture of man. (Head at top & legs at bottom) :P

I am setting height dynamically using layoutparam.

When I set height for instance 300,

It sets height from HEAD to 300 units toward legs of image.

What I want is to set height from LEGs to top 300 units.

imageView.setHeight()
Chintan Joshi
  • 1,207
  • 1
  • 9
  • 17
  • You need to crop the bitmap with specified (X,Y). See [This](https://stackoverflow.com/questions/3725501/how-to-crop-the-parsed-image-in-android). – ADM Dec 04 '17 at 06:41
  • Is it possible to set height from 0 (bottom) to 100 (top). Like revealing imageview from 0 height to 100% height. Is it possible ? – Chintan Joshi Dec 04 '17 at 12:40
  • Height will be the same (300 or anything you want). Coordinates will varies(X,Y). – ADM Dec 04 '17 at 12:41
  • No I don't want to change coordinates as I said I want to start revealing imageview from bottom towards top by setting its height 0 to 100%. Thanks for quick reply. – Chintan Joshi Dec 04 '17 at 12:42
  • You can use Reveal Animation if its fits your requirement . – ADM Dec 04 '17 at 12:45
  • Sure. Thanks for your helpful suggestion. – Chintan Joshi Dec 04 '17 at 13:02

1 Answers1

1

Use this method in your java class:

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();
            }
Jackey kabra
  • 199
  • 1
  • 9