• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps 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.
 
Back
Top Bottom