首页 文章

如何调整Android中的位图大小?

提问于
浏览
276

我从远程数据库中获取了一个Base64字符串的位图,( encodedImage 是用Base64表示图像的字符串):

profileImage = (ImageView)findViewById(R.id.profileImage);

byte[] imageAsBytes=null;
try {
    imageAsBytes = Base64.decode(encodedImage.getBytes());
} catch (IOException e) {e.printStackTrace();}

profileImage.setImageBitmap(
    BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
);

profileImage is my ImageView

好的,但是我必须在我的 ImageView 布局上显示之前调整此图像的大小 . 我必须将其调整为120x120 .

有人能告诉我调整大小的代码吗?

我找到的示例无法应用于base64字符串获取的位图 .

13 回答

  • 7
    public static Bitmap resizeBitmapByScale(
                Bitmap bitmap, float scale, boolean recycle) {
            int width = Math.round(bitmap.getWidth() * scale);
            int height = Math.round(bitmap.getHeight() * scale);
            if (width == bitmap.getWidth()
                    && height == bitmap.getHeight()) return bitmap;
            Bitmap target = Bitmap.createBitmap(width, height, getConfig(bitmap));
            Canvas canvas = new Canvas(target);
            canvas.scale(scale, scale);
            Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
            canvas.drawBitmap(bitmap, 0, 0, paint);
            if (recycle) bitmap.recycle();
            return target;
        }
        private static Bitmap.Config getConfig(Bitmap bitmap) {
            Bitmap.Config config = bitmap.getConfig();
            if (config == null) {
                config = Bitmap.Config.ARGB_8888;
            }
            return config;
        }
    
  • 3
    profileImage.setImageBitmap(
        Bitmap.createScaledBitmap(
            BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length), 
            80, 80, false
        )
    );
    
  • 3

    在这种情况下有人问如何保持纵横比:

    计算用于缩放的因子,并将其用于两个维度 . 假设您希望图像的高度为屏幕的20%

    int scaleToUse = 20; // this will be our percentage
    Bitmap bmp = BitmapFactory.decodeResource(
        context.getResources(), R.drawable.mypng);
    int sizeY = screenResolution.y * scaleToUse / 100;
    int sizeX = bmp.getWidth() * sizeY / bmp.getHeight();
    Bitmap scaled = Bitmap.createScaledBitmap(bmp, sizeX, sizeY, false);
    

    获得屏幕分辨率,你有这个解决方案:Get screen dimensions in pixels

  • 1
    public Bitmap scaleBitmap(Bitmap mBitmap) {
            int ScaleSize = 250;//max Height or width to Scale
            int width = mBitmap.getWidth();
            int height = mBitmap.getHeight();
            float excessSizeRatio = width > height ? width / ScaleSize : height / ScaleSize;
             Bitmap bitmap = Bitmap.createBitmap(
                    mBitmap, 0, 0,(int) (width/excessSizeRatio),(int) (height/excessSizeRatio));
            //mBitmap.recycle(); if you are not using mBitmap Obj
            return bitmap;
        }
    
  • 6
    public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
        int width = bm.getWidth();
        int height = bm.getHeight();
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        // CREATE A MATRIX FOR THE MANIPULATION
        Matrix matrix = new Matrix();
        // RESIZE THE BIT MAP
        matrix.postScale(scaleWidth, scaleHeight);
    
        // "RECREATE" THE NEW BITMAP
        Bitmap resizedBitmap = Bitmap.createBitmap(
            bm, 0, 0, width, height, matrix, false);
        bm.recycle();
        return resizedBitmap;
    }
    

    编辑:正如@aveschini所建议的那样,我已经为内存泄漏添加了 bm.recycle(); . 请注意,如果您将上一个对象用于其他目的,请相应处理 .

  • 22

    位图根据任何显示大小调整大小

    public Bitmap bitmapResize(Bitmap imageBitmap) {
    
        Bitmap bitmap = imageBitmap;
        float heightbmp = bitmap.getHeight();
        float widthbmp = bitmap.getWidth();
    
        // Get Screen width
        DisplayMetrics displaymetrics = new DisplayMetrics();
        this.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        float height = displaymetrics.heightPixels / 3;
        float width = displaymetrics.widthPixels / 3;
    
        int convertHeight = (int) hight, convertWidth = (int) width;
    
        // higher
        if (heightbmp > height) {
            convertHeight = (int) height - 20;
            bitmap = Bitmap.createScaledBitmap(bitmap, convertWidth,
                    convertHighet, true);
        }
    
        // wider
        if (widthbmp > width) {
            convertWidth = (int) width - 20;
            bitmap = Bitmap.createScaledBitmap(bitmap, convertWidth,
                    convertHeight, true);
        }
    
        return bitmap;
    }
    
  • 471

    如果您已有位图,则可以使用以下代码调整大小:

    Bitmap originalBitmap = <original initialization>;
    Bitmap resizedBitmap = Bitmap.createScaledBitmap(
        originalBitmap, newWidth, newHeight, false);
    
  • 101

    从API 19开始,存在Bitmap setWidth(int width)和setHeight(int height) . http://developer.android.com/reference/android/graphics/Bitmap.html

  • 32

    使用目标最大大小和宽度缩放位图,同时保持纵横比:

    int maxHeight = 2000;
    int maxWidth = 2000;    
    float scale = Math.min(((float)maxHeight / bitmap.getWidth()), ((float)maxWidth / bitmap.getHeight()));
    
    Matrix matrix = new Matrix();
    matrix.postScale(scale, scale);
    
    bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    
  • 3

    基于 aspect ratio 的比例:

    float aspectRatio = yourSelectedImage.getWidth() / 
        (float) yourSelectedImage.getHeight();
    int width = 480;
    int height = Math.round(width / aspectRatio);
    
    yourSelectedImage = Bitmap.createScaledBitmap(
        yourSelectedImage, width, height, false);
    

    使用height作为宽度的基本intead更改为:

    int height = 480;
    int width = Math.round(height * aspectRatio);
    
  • 2

    试试这个:这个函数按比例调整位图大小 . 当最后一个参数设置为"X"时, newDimensionXorY 被视为s新宽度,当设置为"Y"时为新高度 .

    public Bitmap getProportionalBitmap(Bitmap bitmap, 
                                        int newDimensionXorY, 
                                        String XorY) {
        if (bitmap == null) {
            return null;
        }
    
        float xyRatio = 0;
        int newWidth = 0;
        int newHeight = 0;
    
        if (XorY.toLowerCase().equals("x")) {
            xyRatio = (float) newDimensionXorY / bitmap.getWidth();
            newHeight = (int) (bitmap.getHeight() * xyRatio);
            bitmap = Bitmap.createScaledBitmap(
                bitmap, newDimensionXorY, newHeight, true);
        } else if (XorY.toLowerCase().equals("y")) {
            xyRatio = (float) newDimensionXorY / bitmap.getHeight();
            newWidth = (int) (bitmap.getWidth() * xyRatio);
            bitmap = Bitmap.createScaledBitmap(
                bitmap, newWidth, newDimensionXorY, true);
        }
        return bitmap;
    }
    
  • 1

    更改:

    profileImage.setImageBitmap(
        BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
    

    至:

    Bitmap b = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
    profileImage.setImageBitmap(Bitmap.createScaledBitmap(b, 120, 120, false));
    
  • 252

    试试这个代码:

    BitmapDrawable drawable = (BitmapDrawable) imgview.getDrawable();
    Bitmap bmp = drawable.getBitmap();
    Bitmap b = Bitmap.createScaledBitmap(bmp, 120, 120, false);
    

    我希望它有用 .

相关问题