How to display part of the image?

I have a large image. But I have to display only part of it. How is this possible with Android ImageView?

+5
source share
2 answers

You can always trim the piece you need with the static method Bitmap.createBitmap()and then assign it to the view:

Sort of...

// 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);
+14
source

Using android: scaleType = "centerInside"

+4
source

All Articles