The use of Android game development bitmap

Source: Internet
Author: User

Bitmap is one of the most important classes of image processing in an Android system.
Using bitmap can obtain image file information, rotate, cut, enlarge and reduce the image.
The following image formats can be supported in the Android SDK: PNG, JPG, GIF, and BMP.


A Create
1 getting a bitmap from a resource
1.1 Getting bitmaps using bitmapdrawable
A bitmapdrawable is constructed using bitmapdrawable (InputStream is);
b using the Bitmapdrawable class of Getbitmap () to obtain the bitmap;


Read InputStream and get bitmap InputStream is = Res.openrawresource (r.drawable.pic180); Bitmapdrawable Bmpdraw = new Bitmapdrawable (IS); Bitmap bmp = Bmpdraw.getbitmap (), or bitmapdrawable bmpdraw= (bitmapdrawable) res.getdrawable (r.drawable.pic180); Bitmap Bmp=bmpdraw.getbitmap ();

1.2 Getting bitmaps using bitmapfactory
All the functions of bitmapfactory are static, and this helper class can get bitmaps through resource IDs, paths, files, data streams, and so on.
Decodebytearray (byte[] data, int offset,int length) starts with the offset position of the specified byte array and parses the byte data length to the bitmap object.
DecodeFile (String pathName) parses, creates bitmap objects from PathName specified file.
Decodefiledescriptor (FileDescriptor FD) is used to parse, create bitmap objects from filedescriptor corresponding files.
The Decoderesource (Resource res,int ID) is used to parse, create bitmap objects from the specified resource file, based on the given resource ID.
Decodestream (InputStream is) for parsing, creating bitmap objects from the specified input stream mediation.

For example:
Bitmap BMP = Bitmapfactory.decoderesource (res, r.drawable.pic180);


During the constant parsing of the system and the creation of bitmap, there may be a outofmemory error in the program running due to small memory or other reasons.
To do this, Android provides a memory recovery method for bitmap:
void recycle (); Bitmap object is forced to be reclaimed.
Boolean isrecycle (); The method that determines whether the bitmap object is recycled:


Two getting the bitmap information
Public final int getwidth () Gets the width of the bitmap
Public final int getheight () Gets the height of the bitmap
Public Final Boolean ismutable () whether the picture can be modified
public int getscaledwidth (canvas canvas) Gets the width of the image after the specified density conversion
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 a picture to an output stream in the specified picture format and quality.
Format:Bitmap.CompressFormat.PNG or Bitmap.CompressFormat.JPEG
Quality: Picture quality, 0-100.0 represents the lowest picture quality compression, 100 with the highest picture quality compression. For images in lossless format such as PNG, this setting is ignored.


Add another two points:
Using the Bitmap.config definition for RGB color format in bitmap, only includes Alpha_8, argb_4444, argb_8888, rgb_565, missing some other, such as rgb_555, may need to pay attention to this small problem in the development;
Bitmap also provides a compress () interface to compress images, but Androidsak only supports PNG, JPG compression, and other formats that need to be supplemented by Android developers themselves.


Three-display bitmap
Display bitmaps can use the core class canvas to display bitmaps through the canvas class's Drawbirmap ().
or draw bitmap to canvas with the help of a bitmapdrawable.
Of course, the bitmap can also be displayed in the view by Bitmapdrawable.


1 Convert to Bitmapdrawable object display bitmap
Get bitmap bitmap Bmp=bitmapfactory.decoderesource (res, r.drawable.pic180);//Convert to Bitmapdrawable object bitmapdrawable BmpDraw =new bitmapdrawable (BMP);//display bitmap ImageView iv2 = (ImageView) Findviewbyid (r.id.imageview02); Iv2.setimagedrawable ( Bmpdraw);

2 displaying bitmaps using the canvas class
public void OnDraw (canvas canvas) {  Bitmap bmp = Bitmapfactory.decoderesource (Getresources (), r.drawable.pic180); Canvas.drawcolor (Color.Black); Canvas.drawbitmap (BMP, ten, NULL);}


Four-bit graph scaling
1 re-draw a bitmap as required, the bitmap is what we need, and the bitmap display almost the same: Drawbitmap (Bitmap Bitmap, rect src, rect dst, paint paint).
2 based on the original bitmap, scale the in-place map to create a new bitmap: CreateBitmap (Bitmap source, int x, int y, int width, int height, Matrix m, Boolean filter)
3 with the canvas's scale (float SX, float sy) (Preconcat the current matrix with the specified scales), but be aware that the entire canvas is scaled at this point.
4 with 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, ten, ten, null);  


Five-bit graph rotation
Similarly, the rotation of the bitmap can be achieved with the help of matrix or canvas.
Bitmap bmp = Bitmapfactory.decoderesource (Getresources (), r.drawable.pic180);  


Reference article:
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

The use of Android game development bitmap

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.