3

I am developing an RDP client application using android-V11.

Server: The screen is divided into 4 parts and sending the image data in byte[], left,top, right, bottom, Screen resolution(width --> 1024/1280, height --? 768/ 1024) values for each frame to client.

Client: I am using surface view to display the images receiving from server. I need to display the 4frames(one screen of server) to fit exactly into the tablet screen.

The Sample code:

 class mySurfaceView extends SurfaceView implements SurfaceHolder.Callback
    {



       class TutorialThread extends Thread 
       {
         @Override
         public void run() 
         {
           Canvas c = null;
          // Socket commnuication
           ......
         Bitmap bmp;
          while(true){
           c=null;
           //logic to get the details from server
          .....
         bmp = BitmapFactory.decodeByteArray(imageBytes, 0,imageBytes.length);

        //Logic to calculate the Top Left, right bottom corners to divide the tablet screen into   4parts for 4 frames receiving from server
          .....

    //Frame rectangle for each frame

        Rect rect = new Rect(newLeft, newTop,newWidth,newHeight);

    //display image

       try{

        c  = _surfaceHolder.lockCanvas(rect);

        if(c!=null){
            synchronized (_surfaceHolder) 
            {
                       c.drawBitmap(scaledBitmap, newLeft,newTop, paint);
                    }
              }
          }
          finally{
        if(c!=null){                                     _surfaceHolder.unlockCanvasAndPost(c);
               }
        }
        //End of while
    }
    //End of run()
    }
    //End Tutorial Thread
    }
    //End of surfaceView
    }

We are unable to fit the bitmap exactly into the rectangle. The frames are displayed in tablet with gaps between them.

After debugging code it seems like the retrieved bitmap(bmp) width is 514, and the rectangle (rect) width is 640. So, the bitmap doesn't fit into the rectangle.

Could please tell me how to scale the bitmap to fit exactly into rectangle.

Note: I also need Pinch Zoom the image.

Thanks & Regards Yamini.

mini
  • 855
  • 4
  • 15
  • 22
  • 2
    YOu have to stretch the bitmap or get a little bigger bitmap and then scale it according to your window size. its a little complicated than you htink. Bcoz android has varying screen sizes. you have to be careful with it yamini. – Andro Selva Jan 24 '13 at 06:48
  • Thank u, you are exactly correct, How to scale the bitmap without using Bitmap.createScaledBitmap(bmp, newWidth, newHeight, false); – mini Jan 24 '13 at 07:19
  • that is not possible yamini.. that's the way. But why don't you want to use it??!! – Andro Selva Jan 24 '13 at 07:24
  • then facing problem with pinch zoom, After pinch zoom I am getting "Out of memory exception" at Bitmap.createScaledBitmap(bmp, newWidth, newHeight, false);. – mini Jan 24 '13 at 08:17
  • 1
    yes. it is common with bitmaps. You have to try and learn on how to use bitmap recycle. Its complicated. I have been through. But no other way. – Andro Selva Jan 24 '13 at 08:21
  • Thank you, but I have recycled the bitmaps using bmp.recycle(), scaleBitmap.recycle() (scaleBitmap-->Bitmap getting after scaling using Bitmap.createScaledBitmap(bmp, newWidth, newHeight, false)). Even though I am getting "out of memory eception" – mini Jan 24 '13 at 09:04

1 Answers1

3

First get the height and width of the bitmap,

final int height= bitmap.getHeight() ;
final int width= bitmap.getWidth();
float h= (float) height;
float w= (float) width;

Now lets the position of your rectangle is ( newLeft, newTop ) and height, width is newHeight and newWidth respectively

Now set the position and scaling factor to a matrix object

Matrix mat=new Matrix();
mat.setTranslate( newLeft, newTop );
mat.setScale(newWidth/w ,newHeight/h);

now draw your bitmap with the matrix

canvas.drawBitmap(bitmap, mat, new paint());

Now your bitmap will fill the rectangle..

try it, all the best.!

Scarecrow
  • 4,057
  • 3
  • 30
  • 56
  • Thank you, your answer works amazing to the fit bitmap in rectangle with following modification c.translate(newLeft, newTop);c.drawBitmap(bmp, matrix, paint);. But problem with pinch zoom functionality (the dirty parts are not equally pinchzoomed). – mini Jan 28 '13 at 07:07