如何将部分大图像裁剪为ImageView

时间:2016-06-19 15:55:48

标签: android imageview crop

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

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

1 个答案:

答案 0 :(得分:0)

您可以使用此方法从偏移量

创建另一个位图
/**
 * 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 class

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