Resizing a Bitmap

Problems with Canvas, OpenGL, etc...

Resizing a Bitmap

Postby Darkie » Mon Jun 07, 2010 11:08 pm

Sorry if this is an already answered question. I'm new to Android programming and to this (fantastic) board.
I'm trying to implement some stupid solitaire game just for the sake of learning something about Android, but I realized my images are too big. I tried searching for methods to simply do this but I cannot find anything useful.
Does any of you knows a very simple way to resize the image?

I have something like this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. canvas.drawBitmap(c.getBitmap(), c.getX(), c.getY(), null);
Parsed in 0.030 seconds, using GeSHi 1.0.8.4

Is it possible to resize the bitmap inside c with very simple steps?
Thanks
Darkie
Freshman
Freshman
 
Posts: 3
Joined: Mon Jun 07, 2010 8:36 pm

Top

Re: Resizing a Bitmap

Postby Darkie » Tue Jun 08, 2010 8:45 pm

I solved the problem using a big function that resizes it. I think it would be better for the developers of the API to introduce some kind of function in the Bitmap class like .changeWidth(int value) or .changeHeight(int value).
Anyhow to whom it may concern, here is the solution I adopted.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 //actual width of the image (img is a Bitmap object)
  2.                 int width = img.getWidth();
  3.                 int height = img.getHeight();
  4.              
  5.                 //new width / heigth
  6.                 int newWidth = 40;
  7.                 int newHeight = 60;
  8.  
  9.                 // calculate the scale
  10.                 float scaleWidth = (float) newWidth / width;
  11.                 float scaleHeight = (float) newHeight / height;
  12.  
  13.                 // create a matrix for the manipulation
  14.                 Matrix matrix = new Matrix();
  15.  
  16.                 // resize the bit map
  17.                 matrix.postScale(scaleWidth, scaleHeight);
  18.  
  19.                 // recreate the new Bitmap and set it back
  20.                 Bitmap resizedBitmap = Bitmap.createBitmap(img, 0, 0,width, height, matrix, true);
  21.                 img = resizedBitmap;
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


Hope it helps! :-)
Darkie
Freshman
Freshman
 
Posts: 3
Joined: Mon Jun 07, 2010 8:36 pm

Re: Resizing a Bitmap

Postby stece » Thu Aug 05, 2010 11:46 am

thanks for the share of your function :D I was looking for something like this ^^
User avatar
stece
Experienced Developer
Experienced Developer
 
Posts: 62
Joined: Wed Apr 21, 2010 10:50 am
Location: Belgium

Re: Resizing a Bitmap

Postby pskink » Thu Aug 05, 2010 12:27 pm

nice, but instead of 20 lines use one-liner: createScaledBitmap

http://developer.android.com/intl/fr/re ... ics.Bitmap, int, int, boolean)
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Re: Resizing a Bitmap

Postby stece » Thu Aug 05, 2010 12:33 pm

Yeah i just found that one too.

Only I wanted to use this code to resize a bitmap to use for a wallpaper.
Code: Select all
Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), uri);
                try {                   
                   Bitmap resized;     
                   Display display = getWindowManager().getDefaultDisplay();
                   int newHeight = display.getHeight();
                   int newWidth = display.getWidth(); 
                   resized = Bitmap.createScaledBitmap(mBitmap, newWidth, newHeight, false);                   
         wallpaperManager.setBitmap(resized);

         int temp = resized.getHeight();
                   int temp2 = mBitmap.getHeight();


It resizes the bitmap, but I suspect Android OS resizing it again when you use it as a wallpaper -.-

temp contains 400 and temp contains 440. But both situations look the same when I set them as bitmap for WallPaperManager :roll:
Back to investigating this problem =(
User avatar
stece
Experienced Developer
Experienced Developer
 
Posts: 62
Joined: Wed Apr 21, 2010 10:50 am
Location: Belgium

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 2 guests