Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display a part of an image?

Tags:

android

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

like image 367
SpunkerBaba Avatar asked Aug 12 '10 09:08

SpunkerBaba


People also ask

How do I show only a part of an image in CSS?

One way to do it is to set the image you want to display as a background in a container (td, div, span etc) and then adjust background-position to get the sprite you want. Just to clarify, you'd set the width and height of the container td, div, span or whatever to 50px to make this work.

How do I hide part of an image in CSS?

You can use the max-height property to specify the maximum height of the image, and then use overflow: hidden; to hide anything else.

How do I crop an image using HTML?

If your image doesn't fit the layout, you can resize it in the HTML. One of the simplest ways to resize an image in the HTML is using the height and width attributes on the img tag. These values specify the height and width of the image element. The values are set in px i.e. CSS pixels.


2 Answers

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

Something like...

// 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);
like image 139
TJF Avatar answered Oct 13 '22 21:10

TJF


Use android:scaleType="centerInside"

like image 43
Denis Palnitsky Avatar answered Oct 13 '22 20:10

Denis Palnitsky