如何以显示图像的一部分?图像

2023-09-06 13:23:25 作者:此笙唯爱你

我有一个大的图像。但我必须要显示它只是一些部分。如何采用Android ImageView的是这可能吗?

I have a large image. But i have to display just some portion of it. How is this possible using Android ImageView?

推荐答案

您总是可以裁剪如果您通过 Bitmap.createBitmap()静态方法所需要的片,和然后将其分配给视图:

You could always crop the piece that you need via the Bitmap.createBitmap() static method, and then assign it to the view:

喜欢的东西...

// Set some constants
private static final Bitmap SOURCE_BITMAP = BitmapFactory.decodeFile(....); // Get the source Bitmap using your favorite method :-)
private static final int START_X = 10;
private static final int START_Y = 15;
private static final int WIDTH_PX = 100;
private static final int HEIGHT_PX = 100;

// Crop bitmap
Bitmap newBitmap = Bitmap.createBitmap(SOURCE_BITMAP, START_X, START_Y, WIDTH_PX, HEIGHT_PX, null, false);

// Assign new bitmap to ImageView
ImageView image = (ImageView)findViewById(R.id.image_view);
image.setImageBitmap(newBitmap);