我正在使用 android-V11 开发 RDP 客户端应用程序。
服务器:屏幕被分成4个部分并以byte[], left,top, right, bottom, Screen resolution(width --> 1024/1280, height --? 768/1024) 值发送图像数据对于每一帧给客户端。
客户端:我正在使用表面 View 来显示从服务器接收的图像。我需要显示 4 帧(服务器的一个屏幕)以完全适合平板电脑屏幕。
示例代码:
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
}
我们无法将位图准确地放入矩形中。帧显示在平板电脑中,它们之间有间隙。
调试代码后,似乎检索到的位图 (bmp) 宽度为 514,矩形 (rect) 宽度为 640。因此,位图不适合矩形。
请告诉我如何缩放位图以完全适合矩形。
注意:我还需要双指缩放图像。
感谢和问候 亚米尼。
最佳答案
首先获取位图的高和宽,
final int height= bitmap.getHeight() ;
final int width= bitmap.getWidth();
float h= (float) height;
float w= (float) width;
现在让你的矩形的位置是( newLeft, newTop ) 和高度,宽度分别为newHeight和newWidth
现在将位置和比例因子设置为矩阵对象
Matrix mat=new Matrix();
mat.setTranslate( newLeft, newTop );
mat.setScale(newWidth/w ,newHeight/h);
现在用矩阵绘制位图
canvas.drawBitmap(bitmap, mat, new paint());
现在您的位图将填充矩形..
试试吧,祝一切顺利!
关于android - 位图不完全适合android中的矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14495356/