Android imageview summary [reprinted]

Source: Internet
Author: User
Android imageview summary [reprinted] I. Introduction Imageview is used to display any image. You can customize the display size and color. Ii. xml attributes Android: whether adjustviewbounds maintains the aspect ratio. It must be used with maxwidth and maxheight. It is ineffective to use it independently. Android: whether croptopadding intercepts a specified area and replaces it with a blank area. The setting is ineffective and must be used with scrolly. Android: maxheight defines the maximum height of a view. It must be used with adjustviewbounds. If you want to set a fixed image size and keep the image aspect ratio, you need to set it as follows: 1) set adjustviewbounds to true; 2) Set maxwidth and maxheight; 3) set layout_width and layout_height to wrap_content. Android: maxwidth sets the maximum width of a view. Android: scaletype sets the filling mode of the image. Android: SRC sets the image or color of the view. Android: tint renders the image into a specified color.
Bytes --------------------------------------------------------------------------------------------- Use the postscale () method in the martix (Android. Graphics. Matrix) Class combined with bitmap to scale the image.

Bitmap BMP = bitmapfactory. decoderesource (getresource (), R. drawalbe. icon1)

Int BMP width = BMP. getwidth ();

Int bmpheight = BMP. getheight ();

Matrix matrix = new matrix ();

Matrix. postscale (width, height );

Bitmap Bm = bitmap. createbitmap (BMP, 0, 0, BMP width, bmpheight, matrix, true );

Imageview. setimagebitmap (BM );


In Android, imageview length and width cannot be dynamically modified after being generated,

Therefore, to reduce the image size, you must remove the original imageview,

Generate a new imageview, specify the image source for it, and put it in layout.

Bytes --------------------------------------------------------------------------------------------------

1. Public voidsetvisibility (INT visibility)
Visibility

OneVISIBLE,INVISIBLE,
OrGONE

However, when this method is called

Image. setvisibility (visibility)

Visibility is an int parameter. Corresponding to the above: visible = 0x00000000; invisible = 0x00000004; gone = 0x00000008.

That is:

Image. setvisibility (0x00000000)/image. setvisibility (view. Visible); indicates display;

Image. setvisibility (0x00000004)/image. setvisibility (view. Invisible); indicates hiding;

Image. setvisibility (0x00000008)/image. setvisibility (view. Gone); indicates that the view does not exist.

2. Different Ways to set colors

Color. RGB (1, 255,255,255 );

Color. Red;

Color. parsecolor (colorstring); where colorstring can be: # rrggbb # 20.rggbb 'red', 'Blue ', 'green', 'black', 'White', 'Gray ', 'cyany', 'magenta ', 'yellow', 'lightgray', 'dark', etc.

3. Set the specified image size

Protected bitmap scaleimg (Bitmap BM, int newwidth, int newheight ){
// Image Source
// Bitmap Bm = bitmapfactory. decodestream (getresources ()
//. Openrawresource (ID ));
// Obtain the image width and height
Int width = BM. getwidth ();
Int Height = BM. getheight ();
// Set the desired size
Int newwidth1 = newwidth;
Int newheight1 = newheight;
// Calculate the scaling ratio
Float scalewidth = (float) newwidth1)/width;
Float scaleheight = (float) newheight1)/height;
// Obtain the matrix parameter to be scaled.
Matrix matrix = new matrix ();
Matrix. postscale (scalewidth, scaleheight );
// Get a new image
Bitmap newbm = bitmap. createbitmap (BM, 0, 0, width, height, matrix,
True );
Return newbm;

}

Call:

Get 18x18 Images

Bitmap Bm = bitmapfactory. decodestream (getresources (). openrawresource (R. drawable. Icon ));

Bitmap newbm = scaleimg (bmimg, 18, 18 );

Imageview. setimagebitmap (newbm );

Android: scaletype:

Android: scaletype controls how images are resized/moved to match the size of imageview. Imageview. scaletype/Android: the differences between scaletype values:

Center/center is centered according to the original size of the image. When the length/width of the image exceeds the length/width of the view, the center part of the captured image is displayed.

Center_crop/centercrop scales up the image size in the center, so that the image length (width) is equal to or greater than the view length (width)

Center_inside/centerinside shows the entire content of the image in the center. The image length/width is equal to or less than the view length/width by proportional reduction or the original size.

Fit_center/fitcenter scales up/down the image proportionally to the width of the view and center the image.

Fit_end/fitend: scales up or down an image to the width of the view, which is displayed in the lower part of the view.

Fit_start/fitstart scales up/down an image proportionally to the width of the view.

Fit_xy/fitxy increase or decrease the image size proportionally to the view size.

Matrix/matrix is drawn using a matrix to dynamically zoom in and zoom in images.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.