Crop drawable to square

I have a stretchable size that I have resized to the required required height (25dp), and the width is scaled proportionally. However, I want the image to be square, with a width of 25dp. However, I don't want to distort the image, so I'm going to crop equal parts to the right and left of the drawable to give me the center of 25dpx25dp. Is it possible? How? I have tried the last two hours and I am ready to throw a towel ...

+4
source share
1 answer
Bitmap target= Bitmap.createBitmap(width, height,Config.ARGB_8888); Canvas canvas = new Canvas(target) float hScale = width/(float)source.getWidth(); float vScale = height/(float)source.getHeight(); float scale = Math.min(hScale, vScale); Matrix matrix = new Matrix(); matrix.setScale(scale, scale); matrix.postTranslate(width/2 - source.getWidth()/2 * scale, height/2 - source.getHeight()/2 * scale); canvas.drawColor(Color.BLACK); canvas.drawBitmap(source, matrix, new Paint()); 
+3
source

Source: https://habr.com/ru/post/1396102/


All Articles