Crop Drawable to Square

落花浮王杯 提交于 2019-12-22 08:54:49

问题


I have a drawable that I have resized to the appropriate desired height (25dp), and the width is scaled proportionally. However, I want an image that is square, with a width of 25dp as well. However, I do not want to distort the image, so I am looking to to crop equal portions of the right and left of the drawable to give me the center 25dpx25dp. Is this possible? How? I have been trying for the past two hours and I'm about ready to throw in the towel...


回答1:



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());



来源:https://stackoverflow.com/questions/9254585/crop-drawable-to-square

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!