我不确定你想做什么,但我以前做过类似的事情。此代码缩放位图以匹配屏幕大小。也许它会帮助你。
public Bitmap scaleBitMap(int width, int height, Bitmap b){
// scale down based on the size of the Surface view.
// width and height = canvas.getHeight(), canvas.getWidth();
// this should set the height and width of the bitmap based on a percentage
// the bit map is returned and then displayed by canvas.drawBitmap(scaleBitMap(canvas.getHeight(),canvas.getWidth(),bitmapHere)....
Bitmap bitmap = null;
try{
float scaledvalues[] = scale(b.getWidth(), b.getHeight(), width, height);
bitmap = Bitmap.createScaledBitmap(b, scaledvalues[1], scaledvalues[0], true);
}catch(Exception e){
}
return bitmap;
}
public float[] scale(int bitmapWidth, int bitmapHeight, int viewWidth, int viewHeight){
float scaledheight = -1f;
float scaledwidth = -1f;
float scaledheightpros = -1f;
float scaledwidthpros = -1f;
float finalheight = -1f;
float finalwidth = -1f;
if(bitmapHeight > viewHeight){
scaledheight = bitmapHeight - viewHeight;
float s = scaledheight*100f;
scaledheightpros = s / 100f;
}
if(bitmapWidth > viewWidth){
scaledwidth = bitmapWidth - viewWidth;
float z = scaledwidth * 100f;
scaledwidthpros = z / bitmapWidth;
}
if(scaledheightpros > scaledwidthpros){
float a = bitmapHeight/100f;
float b = bitmapWidth/100f;
finalheight = bitmapHeight - (a * scaledheightpros);
finalwidth = bitmapWidth - (b * scaledheightpros);
}
else{
float a = bitmapHeight/100f;
float b = bitmapWidth/100f;
finalheight = bitmapHeight - (a * scaledwidthpros);
finalwidth = bitmapWidth - (b * scaledwidthpros);
}
float array[] = {finalwidth, finalheight};
return array;
}