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?

user2234594
  • 305
  • 1
  • 10
  • 19

1 Answers1

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());
  }
android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • 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? – user2234594 Aug 19 '13 at 12:45
  • 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. – android developer Aug 19 '13 at 13:14
  • 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? – user2234594 Aug 19 '13 at 13:20
  • 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....) – android developer Aug 19 '13 at 13:32
  • 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??? – user2234594 Aug 19 '13 at 14:58
  • could it be a similar problem to this: http://stackoverflow.com/questions/18255572/android-bitmap-cache-takes-a-lot-of-memory/18280989#18280989 ? – android developer Aug 19 '13 at 15:06
  • Thank alot for the link ! That downscalings spoiled a lot of my blood in recent weeks – user2234594 Aug 19 '13 at 15:09
  • so it was the problem? you've put the bitmap file in a place you didn't want to ? btw, the other answer was also made by me... – android developer Aug 19 '13 at 15:16
  • Well , the problem is : you have a png file. it's 227pix high.you put it in a bitmap.xml or bitmap decoded from resources, you place it on a layout .. and it's 170 pix high.But if you crop it so it doesn't fill the screen by width -it's back to 227 pix high.. I think that's a bug – user2234594 Aug 19 '13 at 20:17
  • again, are you sure you've read what i've written? in which folder did you put the image file, and why did you create an xml file ? what is in the xml file? i think you should re-edit your question to show exactly what you are trying to do and what you've done so far. – android developer Aug 19 '13 at 20:31
  • what is bitmap.xml file ? well.... where do I begin? image is in res/drawable/xhdpi , I test it on xhdpi screen. If it fits the screen - it is scaled down by ~30% if it's cropped - it's actual size. I don't know how much clearer I can explain this to you – user2234594 Aug 20 '13 at 11:26