Bitmap doesn't quite fit into rectangle in android
I am developing an RDP client application using android-V11.
Server: The screen is divided into 4 parts and sends the image data in bytes [], left, top, right, bottom, screen resolution (width -> 1024/1280, height -? 768/1024) values ββfor each frame to the client.
Customer. I am using surface view to display images from the server. I need to display 4 frames (one server screen) to fit exactly into the tablet screen.
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 cannot exactly fit the bitmap into the rectangle. Frames are displayed in the tablet with gaps between them.
After debugging the code, it appears that the width of the resulting bitmap (bmp) is 514 and the width of the rectangle (rectangle) is 640. Thus, the bitmap does not fit into the rectangle.
Could you please tell me how to scale the bitmap to fit exactly into the rectangle.
Note. I also need a Pinch Zoom image.
Thanks and Regards Yamini.
source to share
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 let's position your rectangle (newLeft, newTop) and height, width newHeight and newWidth respectively
Now set the position and scale factor to the matrix object
Matrix mat=new Matrix();
mat.setTranslate( newLeft, newTop );
mat.setScale(newWidth/w ,newHeight/h);
now draw the bitmap with matrix
canvas.drawBitmap(bitmap, mat, new paint());
Now your bitmap will fill the rectangle.
try, all the best.!
source to share