我有一个drawable,我已调整到适当的所需高度(25dp),宽度按比例缩放.但是,我想要一个方形的图像,宽度也是25dp.但是,我不想扭曲图像,所以我希望裁剪可绘制的右侧和左侧的相等部分给我中心25dpx25dp.这可能吗?怎么样?过去两个小时我一直在努力,我准备好了…
解决方法:
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());