Sunday 22 May 2016

Image Scaling as to the Aspect Ratio - Android


Image Scaling will be huge blocker for the android developers, In our modern world applications are never been without the dynamic images.  So the images has be fitted as to the aspect ration into the views and correspondingly it needs to work on multiple devices and its sizes.

The below code snippet will be really helpful for us.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
public static Bitmap scaleImage(Bitmap view, Context ctx, int size) throws NoSuchElementException {
        Bitmap bitmap = null;
        try {
            bitmap = view;
        } catch (NullPointerException e) {
            throw new NoSuchElementException("No drawable on given view");
        } catch (Exception e) {
   throw new NoSuchElementException("Error: "+e);
        }

        // Get current dimensions AND the desired bounding box
        int width = 0;

        try {
            width = bitmap.getWidth();
        } catch (NullPointerException e) {
            throw new NoSuchElementException("Can't find bitmap on given view/drawable");
        }

        int height = bitmap.getHeight();
        int bounding = dpToPx(size,ctx);
        
        // Determine how much to scale: the dimension requiring less scaling is
        // closer to the its side. This way the image always stays inside your
        // bounding box AND either x/y axis touches it.
        float xScale = ((float) bounding) / width;
        float yScale = ((float) bounding) / height;
        float scale = (xScale <= yScale) ? xScale : yScale;
        Log.i("Test", "xScale = " + Float.toString(xScale));
        Log.i("Test", "yScale = " + Float.toString(yScale));
        Log.i("Test", "scale = " + Float.toString(scale));

        // Create a matrix for the scaling and add the scaling data
        Matrix matrix = new Matrix();
        matrix.postScale(scale, scale);

        // Create a new bitmap and convert it to a format understood by the ImageView
        Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
        width = scaledBitmap.getWidth(); // re-use
        height = scaledBitmap.getHeight(); // re-use

        BitmapDrawable result = new BitmapDrawable(scaledBitmap);
        
        // Apply the scaled bitmap
       // view.setImageDrawable(result);
        return scaledBitmap;
    }

1
2
3
4
public static int dpToPx(int dp, Context ctx) {
        float density = ctx.getResources().getDisplayMetrics().density;
        return Math.round((float)dp * density);
    }

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
 Glide.with(context)
                .load(url)
                .asBitmap()
                .centerCrop()
                .skipMemoryCache(true)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .error(R.drawable.profile)
                .placeholder(R.drawable.profile)
                .into(new BitmapImageViewTarget(viewHolder.txt_jobseekerImage) {
                    @Override
                    protected void setResource(Bitmap resource) {
                // Do bitmap magic here
                        resource = CommonUtil.scaleImage(resource, context);
                        super.setResource(resource);
                    }
                });

No comments: