【问题标题】:How do I crop portions of large image to an ImageView如何将大图像的部分裁剪到 ImageView
【发布时间】:2016-06-19 15:55:48
【问题描述】:

假设我有一张看起来像左边的图片... 每个标志是 30x20px。

如何显示区域 0,60 到 30,80?我也希望以后能够如此高档。

【问题讨论】:

    标签: android imageview crop


    【解决方案1】:

    您可以使用此方法从您的偏移量创建另一个位图

    /**
     * Returns an immutable bitmap from the specified subset of the source
     * bitmap. The new bitmap may be the same object as source, or a copy may
     * have been made. It is initialized with the same density as the original
     * bitmap.
     *
     * @param source   The bitmap we are subsetting
     * @param x        The x coordinate of the first pixel in source
     * @param y        The y coordinate of the first pixel in source
     * @param width    The number of pixels in each row
     * @param height   The number of rows
     * @return A copy of a subset of the source bitmap or the source bitmap itself.
     * @throws IllegalArgumentException if the x, y, width, height values are
     *         outside of the dimensions of the source bitmap, or width is <= 0,
     *         or height is <= 0
     */
    public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height) {
        return createBitmap(source, x, y, width, height, null, false);
    }
    

    另一个选项是创建一个自定义 ImageView,然后覆盖 onDraw() 方法以从您的偏移量在 Canvas 上绘制位图

    使用Canvas类的这个方法

    public void drawBitmap(@NonNull Bitmap bitmap, float left, float top, @Nullable Paint paint) { }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-04
      • 2015-01-15
      • 1970-01-01
      • 2013-08-16
      • 2015-07-08
      • 1970-01-01
      相关资源
      最近更新 更多