1

Well, all this thing tortures me for long weeks, I set an Image 227 pixels high in scales it to 170 pixels even if I want it to be wrap_content whenever I do.

Ok. Here I take My Image which is 1950 pixels long (I put here a part of it so you can understand how it should look like).

enter image description here

First, I want to scale it back to 227 pixels high because that's how it was designed and how it should be

Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.ver_bottom_panel_tiled_long);
            int width = bitmapOrg.getWidth();
        int height = bitmapOrg.getHeight();
        int newWidth = 200; //this should be parent's whdth later
        int newHeight = 227;

        // calculate the scale
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;

        // create a matrix for the manipulation
        Matrix matrix = new Matrix();
        // resize the bit map
        matrix.postScale(scaleWidth, scaleHeight);

        // recreate the new Bitmap
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, 
                          width, height, matrix, true); 


        BitmapDrawable dmpDrwbl=new BitmapDrawable(resizedBitmap);

    verbottompanelprayer.setBackgroundDrawable(dmpDrwbl);

so... it's not a cropped image at all - no, it's 1950 pixels pressed into 200 pixels. enter image description here

But I want just cut anything besides this 200 pixels or whatever width I'll set - crop it and not press all this long image into 200 pixels area.

Also, BitmapDrawable(Bitmap bitmap); and imageView.setBackgroundDrawable(drawable); are deprecated - how can I change that?

1 Answer 1

5

according to what i see, you create a bitmap of the new size (200x227) , so i'm not sure what you expected. you've even written in the comments that you scale and no word on cropping...

what you can do is :

  1. if the API is at least 10 (gingerbread) , you can use BitmapRegionDecoder , using decodeRegion :

  2. if the API is too old, you need to decode the large bitmap, and then crop it into a new bitmap, using Bitmap.createBitmap

something like this:

final Rect rect =...
if (VERSION.SDK_INT >= VERSION_CODES.GINGERBREAD_MR1)
  {
  BitmapRegionDecoder decoder=BitmapRegionDecoder.newInstance(imageFilePath, true);
  croppedBitmap= decoder.decodeRegion(rect, null);
  decoder.recycle();
  }
else 
  {
  Bitmap bitmapOriginal=BitmapFactory.decodeFile(imageFilePath, null);
  croppedBitmap=Bitmap.createBitmap(bitmapOriginal,rect.left,rect.top,rect.width(),rect.height());
  }
Sign up to request clarification or add additional context in comments.

11 Comments

Can you please give me example how to crop it with Bitmap.CreateBitmap ? It just still don't crop, just try to fit the whole image into it. may be i do something wrong?
have you checked the link i just gave you? you need to use the correct function . anyway, i've written a sample code, though i'm not sure if it compiles well.
well, that's the problem - I wanten to use BitmapRegionDecoder but it works only with files . But I want to use it with resources - different dpi's . Working with files will complicate that. is there any way to use BitmapRegionDecoder with resources?
please read what i've given to you. i can't teach you everything about android all in one place... it's not just for files. i've only shown an example. you can use inputStream or fileDescriptor too. you can, for example use getResources().openRawResource(R.drawable....)
it's insane to me........................ i get an image from resources.. original is 227 pixels.. if I use getHeight - it returns 170 pix. if i put it in a bitmap.xml - it's gonna be 170 pixels. But if I crop it - height is gonna be 227 pixels again!!! What's going on???
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.