android开发步步为营之111:将图片转换成指定宽和高

来源:互联网 发布:手机截取在线视频软件 编辑:程序博客网 时间:2024/05/16 04:34
//将bitmap转成和GlSurfaceView一样的宽和高Bitmap bitmap = BitmapFactory.decodeStream(getAssets().open(fileName));//方法一:使用MatrixMatrix 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 8bitmap= 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;        }

发现其实还是按照方法一来实现的

1 0