Android Drawable --- Bitmap xml

Source: Internet
Author: User

Android supports three bitmap formats: .png(),,,,,,.jpg(you can accept registry.gif (not recommended ).

You can use the bitmap file name as the resource ID to directly reference the bitmap file, or define a resource alias in XML.

Note: during the program build process, the bitmap file will be automatically compressed by the aapt tool to optimize it. For example, if a fully colored PNG Image requires no more than 256 colors, the aapt tool will use a color palette to convert the image into an 8-bit PNG image. The image quality has not changed, but the storage space required is reduced. Therefore, you must note that the binary data of the images in this directory will change during the build process. If you want to use bitstream to read images and convert them to bitmap resources, put these images in the res/raw/directory. Images in the/res/raw/directory are not optimized.

When we place the image file in the res/drawable/directory, Android creates a Drawable resource for each image. These resources will eventually be compiled into BitmapDrawable objects. In Java code, these resources are referenced in the form of R. drawable. filename; in XML, they are referenced in the form of @ [package]: drawable/filename.

Example:

For example, if an image is saved as res/drawable/myimage.png, the following layout file applies the image to a View component:

  


The following code retrieves an image in the form of Drawable:

Resources res = getResources();  Drawable drawable = res.getDrawable(R.drawable.myimage);  

XML Bitmap is a resource defined in an XML file that points to a Bitmap file. This creates an alias for the native bitmap file. You can customize extra attributes such as Jitter or tile for bitmap during XML definition.

We can use Node The child node of the node. For example, if we want to create a state list or layer list The node uses the android: drawable attribute Node embedded Define the drawble reference in the node.

XML Bitmap is eventually compiled into a BitmapDrawable object. Access through R. drawable. filename in Java code, and access via @ [package:] drawable/filename in XML.

XML bitmap Syntax:

   
  
 
 

Android: antialias -- enable or disable anti-aliasing

Android: dither -- enable or disable image jitter (used if the bitmap is different from the pixel configuration of the display screen, such as an ARGB 8888 bitmap and an RGB565 display)

Android: filter -- enable or disable a filter. This is used to make the bitmap look smoother when the bitmap is reduced or stretched.

Android: gravity-when the bitmap size is smaller than the container in which it is located, this attribute is used to determine the location of the bitmap in the container. Optional values: top, bottom, left, right, center_vertical, fill_vertical (Vertical Scaling bitmap to make it equal to the container height), center_horizontal, and fill_horizontal (horizontal scaling bitmap to make it equal to the container width) center, fill (vertical and Vertical Scaling to completely fill the container, which is also the default value), clip_vertical (er ...) , Clip_horizontal (er ...).

Android: tileMode -- defines the tile mode. If it is defined, the bitmap will be repeated and the Gravity attribute will be invalid. Optional values include disabled (default value, not tiled), clamp (copy the color of the bitmap edge to fill the remaining blank parts of the container), and repeat (copy the entire bitmap to fill the container), mirror (similar to repeat, but it is an alternate image copy, that is, two adjacent images are symmetric)

For example:

   
  




Related Article

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.