Football Fans: Download the 2012 Schedule App from Google Play!


Go Back   Android Forums > Android Development > Application Development > Developer 101

Developer 101 101 Tutorials



Reply
 
LinkBack Thread Tools
Old March 26th, 2010, 02:36 PM   #1 (permalink)
New Member
 
Join Date: Mar 2010
Posts: 1
 
Device(s):
Thanks: 0
Thanked 0 Times in 0 Posts
Default Bitmap Scaling crashes application

Hey everyone, first time posting here.

Im working on an android app for a project at university and ive come across a bit of a bugger. I have the application loading images from the gallery (buggy but thats another issue), and then lets them draw over that image.

The problem i have is trying to rescale the bitmap so that it fits onto the screen. After a bit of trial an error i found the only place to load the bitmap properly without it crashing the app (or just not displaying it at all) was in the onSizeChanged function which now looks like this.


Code:
@Override
	        protected void onSizeChanged(int w, int h, int oldw,int oldh)
	        {
	            int curW = mBitmap != null ? mBitmap.getWidth() : 0;
	            int curH = mBitmap != null ? mBitmap.getHeight() : 0;
	            if (curW >= w && curH >= h) return;
	            if (curW < w) curW = w;
	            if (curH < h) curH = h;

	            BitmapFactory.Options bfo = new BitmapFactory.Options();
	            bfo.inSampleSize = 2;
	            mBitmap = BitmapFactory.decodeFile(mFilename, bfo);
	            bmpHeight = mBitmap.getHeight();
	            bmpWidth = mBitmap.getWidth();
	            if((bmpWidth>bmpHeight)&&(bmpWidth>screenWidth))
	            {
	                double ratio = screenWidth/bmpWidth;
	                bmpWidth=(int)screenWidth;
	                bmpHeight=(int)(ratio*bmpHeight);
	            }
	            else if((bmpHeight>bmpWidth)&&(bmpHeight>screenHeight))
	            {
	                double ratio = screenHeight/bmpHeight;
	                bmpHeight=(int)screenHeight;
	                bmpWidth=(int)(ratio*bmpWidth);
	            }
	            
	            //mBitmap = Bitmap.createScaledBitmap(mBitmap, bmpWidth, bmpHeight, true);
	            //Bitmap newBitmap = Bitmap.createScaledBitmap(mBitmap, bmpWidth, bmpHeight, true);
	            
	            Bitmap newBitmap = Bitmap.createBitmap(curW, curH,Bitmap.Config.RGB_565);
	            
	            Canvas newCanvas = new Canvas();
	            newCanvas.setBitmap(newBitmap);
	            if (mBitmap != null) {
	                newCanvas.drawBitmap(mBitmap, 0, 0, null);
	            }
	            mBitmap = newBitmap;
	            mCanvas = newCanvas;
	        }
The code as it is here draws the bitmap image to the canvas, but doesn't scale it. The two commented out lines are things i tried to get the image to scale. neither worked.

any ideas would be very much appreciated.

fat-wednesday is offline  
Reply With Quote
Sponsors
Reply

Bookmarks


Go Back   Android Forums > Android Development > Application Development > Developer 101 User CP
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
GW620 Crashes & Crashes & Crashes..... James24 Eve - Support and Troubleshooting 2 July 12th, 2010 07:11 AM
how to merge an array of bitmap together in one bitmap ? freeaks Android Applications 0 June 28th, 2010 01:06 AM
Application scaling/resolution problem? MrDonBonJovi Motorola Droid 0 May 12th, 2010 10:30 AM
Android application Crashes Deewanagan Android Applications 1 January 18th, 2010 06:28 PM
Phone crashes when Camera application launched sampablokuper G1 - Support 6 November 27th, 2009 09:06 PM



All times are GMT -5. The time now is 08:27 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
Custom vBulletin Skins by: Relivo