Activity runs slow with a couple of ImageView-s

前端 未结 4 1339
猫巷女王i
猫巷女王i 2020-12-16 23:57

I have an activity containing 4 images in total. They are all matching the resolution of a 1080x1920 device. When I run the activity with those images, which are loaded dire

4条回答
  •  温柔的废话
    2020-12-17 00:51

    Problem is resolution of the image, if you can reduce resolution of the image then work fine, here is some example for reducing image resolution and size.

    If you pass bitmap width and height then use below function.

        public Bitmap getResizedBitmap(Bitmap image, int bitmapWidth,
                int bitmapHeight) {
            return Bitmap.createScaledBitmap(image, bitmapWidth, bitmapHeight,
                    true);
        } 
    

    if you want bitmap ratio same and reduce bitmap size. then pass your maximum size bitmap. you can use this function

    public Bitmap getResizedBitmap(Bitmap image, int maxSize) {
        int width = image.getWidth();
        int height = image.getHeight();
    
        float bitmapRatio = (float)width / (float) height;
        if (bitmapRatio > 0) {
            width = maxSize;
            height = (int) (width / bitmapRatio);
        } else {
            height = maxSize;
            width = (int) (height * bitmapRatio);
        }
        return Bitmap.createScaledBitmap(image, width, height, true);
    }
    

    or if you are using drawable resources then use this method

    public Drawable resizeImage(int imageResource) {// R.drawable.large_image
        // Get device dimensions
        Display display = getWindowManager().getDefaultDisplay();
        double deviceWidth = display.getWidth();
    
        BitmapDrawable bd = (BitmapDrawable) this.getResources().getDrawable(
                imageResource);
        double imageHeight = bd.getBitmap().getHeight();
        double imageWidth = bd.getBitmap().getWidth();
    
        double ratio = deviceWidth / imageWidth;
        int newImageHeight = (int) (imageHeight * ratio);
    
        Bitmap bMap = BitmapFactory.decodeResource(getResources(), imageResource);
        Drawable drawable = new BitmapDrawable(this.getResources(),
                getResizedBitmap(bMap, newImageHeight, (int) deviceWidth));
    
        return drawable;
    }
    
    /************************ Resize Bitmap *********************************/
    public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
    
        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);
    
        return resizedBitmap;
    }
    

提交回复
热议问题