//将bitmap转成和GlSurfaceView一样的宽和高
Bitmap bitmap = BitmapFactory.decodeStream(getAssets().open(fileName));
//方法一:使用Matrix
Matrix matrix=new Matrix();
matrix.postScale((float)glView.getWidth()/bitmap.getWidth(), (float)glView.getHeight()/bitmap.getHeight());
bitmap=Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(),matrix,true);
//方法二:使用系统自动的工具类 add in api 8
bitmap= ThumbnailUtils.extractThumbnail(bitmap, glView.getWidth(), glView.getHeight());
查看方法二的源代码
/**
* Creates a centered bitmap of the desired size.
*
* @param source original bitmap source
* @param width targeted width
* @param height targeted height
* @param options options used during thumbnail extraction
*/
public static Bitmap extractThumbnail(
Bitmap source, int width, int height, int options) {
if (source == null) {
return null;
}
float scale;
if (source.getWidth() < source.getHeight()) {
scale = width / (float) source.getWidth();
} else {
scale = height / (float) source.getHeight();
}
Matrix matrix = new Matrix();
matrix.setScale(scale, scale);
Bitmap thumbnail = transform(matrix, source, width, height,
OPTIONS_SCALE_UP | options);
return thumbnail;
}
Bitmap b1;
if (scaler != null) {
// this is used for minithumb and crop, so we want to filter here.
b1 = Bitmap.createBitmap(source, 0, 0,
source.getWidth(), source.getHeight(), scaler, true);
} else {
b1 = source;
}
发现其实还是按照方法一来实现的