2013-08-16 56 views
0

我有一个大于屏幕大小的位图图像。我想将图像按像素缩小到屏幕尺寸?我怎样才能做到这一点 ?我想调整它,保持高宽比不只是调整它的大小?将位图缩放到sceen的高度和宽度

亲切的问候

+0

你是否试过秤类型:fitxy? property – KOTIOS

+0

不,我想按比例缩放它 – user1730789

+0

是可以编程appyied也 – KOTIOS

回答

0

使用该功能参数为屏幕宽度和screenHeight

public Bitmap scaleCenterCrop(Bitmap source, int newHeight, int newWidth) { 
int sourceWidth = source.getWidth(); 
int sourceHeight = source.getHeight(); 

// Compute the scaling factors to fit the new height and width, respectively. 
// To cover the final image, the final scaling will be the bigger 
// of these two. 
float xScale = (float) newWidth/sourceWidth; 
float yScale = (float) newHeight/sourceHeight; 
float scale = Math.max(xScale, yScale); 

// Now get the size of the source bitmap when scaled 
float scaledWidth = scale * sourceWidth; 
float scaledHeight = scale * sourceHeight; 

// Let's find out the upper left coordinates if the scaled bitmap 
// should be centered in the new size give by the parameters 
float left = (newWidth - scaledWidth)/2; 
float top = (newHeight - scaledHeight)/2; 

// The target rectangle for the new, scaled version of the source bitmap will now 
// be 
RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight); 

// Finally, we create a new bitmap of the specified size and draw our new, 
// scaled bitmap onto it. 
Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, source.getConfig()); 
Canvas canvas = new Canvas(dest); 
canvas.drawBitmap(source, null, targetRect, null); 

return dest; 
} 

如何让屏幕宽度和高度看this

+0

如何获得屏幕宽度和屏幕高度? – user1730789

+0

看到编辑答案 –

0

获取屏幕尺寸和那些参数传递到以下功能:

Bitmap.createScaledBitmap(originalimage, width, height, true); 
+0

这将给出outOfMemoryError,而我们有高分辨率的图像。然后尝试压缩图像, –

+0

。 – Exceptional

+0

是的,我已经尝试过 –

相关问题