设置BitmapFactory.options后未绘制位图

时间:2018-07-26 01:49:16

标签: android android-bitmap

我正在尝试更有效地加载位图,具体方法如下:https://developer.android.com/topic/performance/graphics/load-bitmap

但是当我计算样本大小并将该建议传递给BitmapFactory.Options时,不再绘制位图了吗?传递的样本大小为1,因此不需要缩放。

这是我的AsyncTask,它尝试通过inputstream下载图像并返回位图:

package com.xyz.InboxManager;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.widget.ImageView;

import java.io.IOException;
import java.io.InputStream;

import com.xyz.helpers.ImageUtility;

public class GetBitmapDataFromInputStreamTask extends AsyncTask<InputStream,Void,Bitmap> {

    ImageView imageView = null;

    public GetBitmapDataFromInputStreamTask(ImageView _imageView) {
        super();

        imageView = _imageView;

    }

    @Override
    protected Bitmap doInBackground(InputStream... inputStreams) {

        InputStream is = inputStreams[0];
        Bitmap bitmap = null;
        if (is != null) {

            ImageUtility.ImageScalingData imageScalingData = ImageUtility.optimalDimension(ImageUtility.bitmapWidthAndHeight(is).outWidth,ImageUtility.bitmapWidthAndHeight(is).outHeight,ImageUtility.dpScreenWidth(),ImageUtility.dpScreenHeight());
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = imageScalingData.getSampleSize();

            bitmap = BitmapFactory.decodeStream(is);

            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return bitmap;
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        super.onPostExecute(bitmap);
        if (bitmap!=null && imageView!=null)
        {
            imageView.setImageBitmap(bitmap);
        }


    }
}

此类ImageUtility用于基于https://developer.android.com/topic/performance/graphics/load-bitmap计算位图的图像缩放比例:

package com.xyz.helpers;

import android.graphics.BitmapFactory;

import android.util.DisplayMetrics;
import android.util.Log;

import java.io.InputStream;

import com.xyz.Application.NgfrApp;

public class ImageUtility {

    private static final String TAG = "ImageUtility";

    public static float dpScreenHeight()
    {

        DisplayMetrics displayMetrics = NgfrApp.getContext().getResources().getDisplayMetrics();
        return displayMetrics.heightPixels / displayMetrics.density;

    }

    public static float dpScreenWidth()
    {

        DisplayMetrics displayMetrics = NgfrApp.getContext().getResources().getDisplayMetrics();

        return  displayMetrics.widthPixels / displayMetrics.density;
    }

    public static BitmapFactory.Options bitmapWidthAndHeight (InputStream is)
    {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(is);
        //set back to false
        options.inJustDecodeBounds = false;

        return options;
    }

    public static ImageScalingData optimalDimension(double imageWidth, double imageHeight, double screenWidth, double screenHeight, float imageToScreenScaleFactor)
    {

        Log.i(TAG,"imageWidth: "+imageWidth+",imageHeight:"+imageHeight+",screenWidth:"+screenWidth+",screenHeight:"+screenHeight);

        ImageScalingData imageScalingData = null;
        double imageFrameHeight = screenHeight * imageToScreenScaleFactor;
        double imageFrameWidth = screenWidth * imageToScreenScaleFactor;
        int counter = 1;

            //image is larger than frame , decrease dimension
            if (imageHeight > screenHeight || imageWidth > screenWidth)
            {
              while (imageHeight > screenHeight || imageWidth > screenWidth)
              {
                  imageHeight = imageHeight / 2;
                  imageWidth = imageWidth / 2;
                  counter *= 2;
              }

                imageScalingData = new ImageScalingData(imageWidth,imageHeight,counter);
            }
            else
            {
                //same size do nothing
                imageScalingData = new ImageScalingData(imageWidth,imageHeight,counter);
            }

        return imageScalingData;
    }

    public static class ImageScalingData {

        private  double width;
        private double height;
        private int sampleSize;

        private ImageScalingData(double _width, double _height, int _sampleSize)
        {
            width = _width;
            height = _height;
            sampleSize = _sampleSize;
        }

        public double getWidth() {
            return width;
        }

        public double getHeight() {
            return height;
        }

        public int getSampleSize() {
            return sampleSize;
        }
    }
}

我还注意到我的日志,显示图像的高度和宽度为0,这没有意义。

I / ImageUtility:图像宽度:0.0,图像高度:0.0 ,屏幕宽度:360.0,屏幕高度:640.0

当我删除异步类GetBitmapDataFromInputStreamTask中的代码时,一切正常,但是Im不会降低采样率,并且当您有100条消息时会降低性能: 收件箱。

  ImageUtility.ImageScalingData imageScalingData = ImageUtility.optimalDimension(ImageUtility.bitmapWidthAndHeight(is).outWidth,ImageUtility.bitmapWidthAndHeight(is).outHeight,ImageUtility.dpScreenWidth(),ImageUtility.dpScreenHeight());
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = imageScalingData.getSampleSize();

非常感谢。

0 个答案:

没有答案