Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 取出或裁剪矩形覆盖的图像(上、左、右、下),就像使用相机裁剪一样_Android_Android Canvas - Fatal编程技术网

Android 取出或裁剪矩形覆盖的图像(上、左、右、下),就像使用相机裁剪一样

Android 取出或裁剪矩形覆盖的图像(上、左、右、下),就像使用相机裁剪一样,android,android-canvas,Android,Android Canvas,我有一个ImageView,在这个ImageView上我画了一个矩形顶部、左侧、右侧和底部,可以通过触摸事件移动。现在我想根据矩形覆盖的区域裁剪ImageView的源图像。。就像修剪…我想用 drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint); 但是如果您有任何建议,我们将不胜感激。您可以使用 Bitmap.createBitmap(android.graphics.Bitmap source, int x, int y, in

我有一个ImageView,在这个ImageView上我画了一个矩形顶部、左侧、右侧和底部,可以通过触摸事件移动。现在我想根据矩形覆盖的区域裁剪ImageView的源图像。。就像修剪…我想用

drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint);
但是如果您有任何建议,我们将不胜感激。

您可以使用

Bitmap.createBitmap(android.graphics.Bitmap source, int x, int y, int width, int height)

欢迎来到堆栈溢出!除了粘贴代码,您还可以添加解释。这可能对你很有意义,但其他人可能在理解上有问题。这段代码只提供左上角的裁剪,但不提供右下角的裁剪…所以我还需要一个合适的代码。
 public static Bitmap CropImage(Image source, int x, int y, int width, int height)
        {
            Rectangle crop = new Rectangle(x, y, width, height);

            var bmp = new Bitmap(crop.Width, crop.Height);
            using (var gr = Graphics.FromImage(bmp))
            {
                gr.DrawImage(source, new Rectangle(0, 0, bmp.Width, bmp.Height), crop, GraphicsUnit.Pixel);
            }
            return bmp;
}