The picture is proportionally scaled, and the cut starts from the left of 0.0 to ensure that the picture is not deformed /** * @Description * @Created by YCH on 2015/11/9. */ public class CropImageView extends ImageView { /** * the width of the canvas */ private int width; /** * the height of the canvas */ private int height; float scaleWidth = 1.0f; float scaleHeight = 1.0f; public CropImageView(Context context) { super(context); } public CropImageView(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); width = canvas.getWidth(); height = canvas.getHeight(); Drawable drawable = getDrawable(); if (drawable != null) { Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap(); if(bitmap != null){ int bmWidth = bitmap.getWidth(); int bmHeight = bitmap.getHeight(); float scale = 1.0f; float scaleBitmap = (float)bmWidth/(float)bmHeight;//Image aspect ratio float scaleContainer = (float)width/(float)height;//Container aspect ratio //If the scale of the container is greater than the scale of the image if(scaleContainer > scaleBitmap){ //calculate based on width scale = (float) width / (float) bmWidth; }else { //calculate based on height scale = (float) height / (float) bmHeight; } Matrix matrix = new Matrix(); matrix.postScale(scaleWidth * scale, scaleHeight * scale); Rect rect = new Rect(); rect.left = 0; rect.top = 0; rect.right = width; rect.bottom = height; Bitmap bm = Bitmap.createBitmap(bitmap, 0, 0, bmWidth, bmHeight, matrix, true); canvas.drawBitmap(bm, rect, rect, null); } } } }