如何调整使用Universal Image Loader加载的位图位图的大小

时间:2015-01-28 18:56:30

标签: android bitmap universal-image-loader

现在我有这个:

       new BaseImageDecoder(false) {
            @Override
            protected BitmapFactory.Options prepareDecodingOptions(
                    ImageSize imageSize, ImageDecodingInfo decodingInfo) {
                // pass in a different imageSize, it might just work.
                ImageSize targetSize = new ImageSize(120, 80);
                return super.prepareDecodingOptions(targetSize, decodingInfo);
            }
        };
        DisplayImageOptions displayOptions = new DisplayImageOptions.Builder()
                .postProcessor(BitmapProcessor BaseImageDecoder)
                .build();

        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(this));
        imageLoader.displayImage(url, watermark, displayOptions, new ImageLoadingListener() {
            @Override
            public void onLoadingStarted(String imageUri, View view) {
                myBitmap = ((BitmapDrawable) watermark.getDrawable()).getBitmap();
            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                myBitmap = ((BitmapDrawable) watermark.getDrawable()).getBitmap();
//                saveBitmap(myBitmap);
            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                myBitmap = loadedImage;
//                saveBitmap(myBitmap);
            }

            @Override
            public void onLoadingCancelled(String imageUri, View view) {
                myBitmap = ((BitmapDrawable) watermark.getDrawable()).getBitmap();
//                saveBitmap(myBitmap);
            }
        });

但是它提供了预期的表达式#34;在.postProocessor( BaseImageDecoder)这一行,所以我认为我搞砸了这部分实施的一部分?如何将其传递给选项以便调整大小?

2 个答案:

答案 0 :(得分:1)

您可以像这样调整任何位图的大小:

    int oldWidth = origImage.getWidth();
    int oldHeight = origImage.getHeight();

    int newWidth =  // 
    int newHeight = // some values you determine

    // calculate the scale
    float scaleWidth = ((float) newWidth) / oldWidth;
    float scaleHeight = ((float)newHeight) / oldHeight;

    // create a matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);


    // recreate the new scaled Bitmap
    Bitmap newImage = Bitmap.createBitmap(origImage, 0, 0,  oldWidth, oldHeight, matrix, true); 

答案 1 :(得分:0)

拿上面给我的那些代码。然后将其插入此处:

@Override
        public void onLoadingComplete(String imageUri, View view,   Bitmap loadedImage) {
            myBitmap = loadedImage;

            // insert code here to adjust image size
        }