Android Bitmap 图片裁剪

private void clipBitmap() {
    // 创建原始位图对象
    Bitmap originalImage = BitmapFactory.decodeResource(getResources(),
            R.drawable.ic_cast_background);
    // 创建新的空白画布对象
    int width = ScreenUtil.dp2px(this,550);
    int height = ScreenUtil.dp2px(this,452);
    // 创建新的画布大小为550X452位图对象
    Bitmap croppedImage = Bitmap.createBitmap(width, height, originalImage.getConfig());
    // 创建画布并将其与新的位图关联起来
    Canvas canvas = new Canvas(croppedImage);
    canvas.drawARGB(0, 0, 0, 0); // 设置画布透明度为完全不透明(默认值)
    // 定义要剪切的区域
    int left = ScreenUtil.dp2px(this,120);
    int top = ScreenUtil.dp2px(this,116);
    int right = ScreenUtil.dp2px(this,120 + width);
    int bottom = ScreenUtil.dp2px(this,116 + height);
    // 左上角到右下角的矩形区域
    Rect srcRect = new Rect(left, top, right, bottom);
    // 目标矩形区域
    Rect destRect = new Rect(0, 0, width, height);
    // 调用 drawBitmap() 函数进行剪切操作
    canvas.drawBitmap(originalImage, srcRect, destRect, null);
    // 显示或保存剪切后的图片
    ImageView imageView = findViewById(R.id.cast_bg);
    imageView.setImageBitmap(croppedImage);
}

相关推荐

  1. Android Bitmap 图片裁剪

    2024-01-27 08:40:01       33 阅读
  2. PHP 图片裁剪类封装

    2024-01-27 08:40:01       12 阅读
  3. 【Python】图像裁剪与匹配

    2024-01-27 08:40:01       31 阅读
  4. Vue使用vue-img-cropper实现图片裁剪

    2024-01-27 08:40:01       38 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-27 08:40:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-27 08:40:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-27 08:40:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-27 08:40:01       20 阅读

热门阅读

  1. Node+Express写分页接口

    2024-01-27 08:40:01       31 阅读
  2. 前端组件封装

    2024-01-27 08:40:01       29 阅读
  3. wordpress连接azure MySQL

    2024-01-27 08:40:01       40 阅读
  4. 范围查询优化:索引跳跃扫描

    2024-01-27 08:40:01       29 阅读
  5. Redis的SDS你了解吗?

    2024-01-27 08:40:01       31 阅读
  6. GBASE南大通用分享-mysql初始化命令

    2024-01-27 08:40:01       36 阅读