裁剪到正方形

时间:2012-02-13 01:36:26

标签: android image drawable crop

我有一个可绘制的,我已调整到适当的所需高度(25dp),并且宽度按比例缩放。但是,我想要一个方形的图像,宽度也是25dp。但是,我不想扭曲图像,所以我希望裁剪可绘制的右侧和左侧的相等部分给我中心25dpx25dp。这可能吗?怎么样?我在过去的两个小时里一直在努力,我准备放弃......

1 个答案:

答案 0 :(得分:3)


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