Use of Bitmap for android game development, androidbitmap

Source: Internet
Author: User

Use of Bitmap for android game development, androidbitmap
Bitmap is one of the most important classes in the Android system for image processing.
Bitmap can be used to obtain image file information and rotate, cut, zoom in, and zoom out the image.
The following formats are supported in the Android SDK: png, jpg, gif, and bmp.


1. Create
1. Obtain a bitmap from a resource
1.1 Use BitmapDrawable to obtain a bitmap
A uses BitmapDrawable (InputStream is) to construct a BitmapDrawable;
B. Use getBitmap () of the BitmapDrawable class to obtain the bitmap;


// Read InputStream and obtain the bitmap InputStream is = res. openRawResource (R. drawable. pic180); BitmapDrawable bmp draw = new BitmapDrawable (is); Bitmap bmp = bmp draw. getBitmap (); or BitmapDrawable BMP draw = (BitmapDrawable) res. getDrawable (R. drawable. pic180); Bitmap bmp = bmp draw. getBitmap ();

1.2 use BitmapFactory to obtain bitmap
All functions of BitmapFactory are static. This helper class can obtain bitmap through resource ID, path, file, and data stream.
DecodeByteArray (byte [] data, int offset, int length) parses the byte data whose length is length into a Bitmap object starting from the offset position of the specified byte array.
DecodeFIle (String pathName) parses and creates a Bitmap object from the file specified by pathName.
DecodeFileDescriptor (FileDescriptor fd) is used to parse and create Bitmap objects from the file corresponding to FileDescriptor.
DecodeResource (Resource res, int id) is used to parse and create a Bitmap object from the specified Resource file based on the given Resource ID.
DecodeStream (InputStream is) is used to parse and create Bitmap objects from the specified input stream.

For example:
Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.pic180);


During the process of parsing and creating Bitmap, the system may encounter an OutOfMemory error when the program runs due to a small memory size or other reasons.
Therefore, Android provides a memory reclaim Method for Bitmap:
Void recycle (); forcibly recycles Bitmap objects.
Boolean isRecycle (); method for determining whether a Bitmap object is recycled:


2. Obtain bitmap Information
Public final int getWidth () to obtain the bitmap width
Public final int getHeight () to obtain the height of the bitmap
Public final boolean isMutable () whether the image can be modified
Public int getScaledWidth (Canvas canvas) gets the width of the converted image with the specified density.
Public int getScaledHeight (Canvas canvas) gets the height of the image after the specified density conversion.
Public boolean compress (CompressFormat format, int quality, OutputStream stream) -- converts an image to an output stream based on the specified image format and image quality.
Format: Bitmap. CompressFormat. PNG or Bitmap. CompressFormat. JPEG
Quality: image quality. 0-100 indicates the minimum image quality compression, and indicates the highest image quality compression. This setting is ignored for images in PNG or other lossless formats.


Two additional points:
Bitmap is used for RGB color formats in Bitmap. the Config definition only includes ALPHA_8, ARGB_4444, ARGB_8888, and RGB_565. Some other problems are missing, such as RGB_555. You may need to pay attention to this problem during development;
Bitmap also provides the compress () interface to compress images. However, AndroidSAK only supports compression in PNG and JPG formats. Android Developers need to add other formats.


Three-display bitmap
The display bitmap can use the core Canvas class to display the bitmap through the drawBirmap () of the Canvas class.
Or use BitmapDrawable to draw Bitmap to the Canvas.
Of course, BitmapDrawable can also be used to display the bitmap to the View.


1. Convert to BitmapDrawable object display bitmap
// Obtain Bitmap bmp = BitmapFactory. decodeResource (res, R. drawable. pic180); // convert to BitmapDrawable object BitmapDrawable bmp draw = new BitmapDrawable (bmp); // display the bitmap ImageView iv2 = (ImageView) findViewById (R. id. imageView02); iv2.setImageDrawable (BMP draw );

2. Use the Canvas class to display bitmap
public void onDraw(Canvas canvas){  Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pic180);canvas.drawColor(Color.BLACK);canvas.drawBitmap(bmp, 10, 10, null);}


Four-bitmap Scaling
1. redraw a Bitmap as needed. The bitmap after painting is what we need. It is almost the same as the Bitmap display: drawBitmap (bitmap Bitmap, Rect src, Rect dst, paint paint ).
2. Based on the original Bitmap, scale the original Bitmap to create a new Bitmap: CreateBitmap (Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)
3. Use the scale (float sx, float sy) (Preconcat the current matrix with the specified scale.) of the Canvas, but note that the entire Canvas is scaled.
4 using Matrix:
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pic180);  Matrix matrix=new Matrix();matrix.postScale(0.2f, 0.2f);Bitmap dstbmp=Bitmap.createBitmap(bmp,0,0,bmp.getWidth(),bmp.getHeight(),matrix,true);canvas.drawColor(Color.BLACK);  canvas.drawBitmap(dstbmp, 10, 10, null);  


5-bitmap Rotation
Likewise, bitmap rotation can also be achieved through Matrix or Canvas.
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pic180);  Matrix matrix=new Matrix();matrix.postScale(0.8f, 0.8f);matrix.postRotate(45);Bitmap dstbmp=Bitmap.createBitmap(bmp,0,0,bmp.getWidth(),bmp.getHeight(),matrix,true);canvas.drawColor(Color.BLACK); canvas.drawBitmap(dstbmp, 10, 10, null); 


References:
Http://www.cnblogs.com/feisky/archive/2010/01/10/1643460.html
Http://blog.csdn.net/csxwc/article/details/10345235
Http://coollast.blog.51cto.com/6319475/1120760

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.