Android Learning-graphic image processing (Bitmap, bitmapfactory) (i)

Source: Internet
Author: User

Reprinted from http://blog.csdn.net/csxwc/article/details/10345235

Bitmap is one of the most important classes of image processing in an Android system. It can obtain image file information, rotate the image, cut, enlarge and reduce the operation.

Bitmap represents a bitmap that allows us to use the resources commonly used in development, and the following is a brief introduction to bitmap.

Bitmap method: 1, the use of bitmapdrawablebitmapdrawable encapsulated image is a bitmap object, we want to wrap bitmap into Bitmapdrawable object, You can call the construction method of bitmapdrawable: Bitmapdrawbale drawable = new Bitmapdrawable (bitmap), or if you want bitmapdrawable objects wrapped You can call Bitmapdrawable's Getbitmap () method: Bitmap Bitmap = Drawbale.getbitmap (); 2, Bitmap provides some static methods to create Bitmap objects (just a few):
    • CreateBitmap (Bitmap source,int x,int y,int width,int height): Starting from the specified coordinates (x, y) of the source of the in-place map, the wide width is dug from it, and the high heigtht piece comes out, Creates a new Bitmap object.
    • Createscaledbitmap (Bitmap source,int width,ing height,boolean fliter): Scales the source bitmap, scales the new bitmap that is called width width, high heigth.
    • CreateBitmap (int width,int height,bitmap.config Config): Creates a variable new bitmap with wide width and high height.
    • CreateBitmap (Bitmap source, int x,int y,int width,int height, Matrix m,boolean fliter): Starting with the specified coordinates (x, y) of source bitmap sources, digging wide width, A piece of high height creates a new bitmap object and transforms it according to the rules specified by the matrix.
3, through the analysis of the resource file to get Bitmap object, here will use the Bitmapfactory this tool class, provides the following methods:
    • Decodebytearray (byte[] data, int offset,int length): Parses byte data of length to bitmap object starting from the offset position of the specified byte array.
    • DecodeFile (String pathName): Resolves, creates bitmap object from pathName specified file.
    • Decodefiledescriptor (FileDescriptor FD): Used to parse, create bitmap objects from filedescriptor corresponding files.
    • Decoderesource (Resource res,int ID): Used to parse, create bitmap objects from the specified resource file, based on the given resource ID.
    • Decodestream (InputStream is): Used to resolve, create bitmap objects from the specified input stream mediation.
However, 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 bitmap with a method of memory reclamation: void Recycle (): Forces the collection of bitmap objects. There is also a method for judging whether the bitmap object is recycled: Boolean isrecycle (); If the Android app needs to access the system album, it needs to parse and create the bitmap object with Bitmapfactory. Click to open the link this article is slightly related to this. The following are simple applications for bitmap and bitmapfactory. Description: View the image resources in the assets directory. Because the layout file is very simple, here is not given, the source code is as follows:

public class Mainactivity extends Activity {

string[] images = null;
Gets the object that accesses the assets file
Assetmanager assets = NULL;
int currentimg = 0;
ImageView img;


@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);

img = (ImageView) Findviewbyid (r.id.img_show);
try {
Gets the object that accesses the file under assets
Assets = Getassets ();
Images = Assets.list ("");
} catch (Exception e) {
E.printstacktrace ();
}

Button Btn_next = (button) Findviewbyid (R.id.btn_next);
Btn_next.setonclicklistener (New Onclicklistener () {

@Override
public void OnClick (View v) {
If the corner mark is out of bounds
if (currentimg>=images.length) {
currentimg = 0;

}
Find the next picture
while (!images[currentimg].endswith (". jpg")) {
currentimg++;
if (currentimg>=images.length) {
currentimg = 0;
}
}
InputStream assetfile = null;

try {
Opens the input stream corresponding to the specified resource
Assetfile = Assets.open (images[currentimg++]);
} catch (Exception e) {
E.printstacktrace ();
}

Recycling pictures
Bitmapdrawable bitmapdrawable = (bitmapdrawable) img.getdrawable ();
if (Bitmapdrawable!=null&&!bitmapdrawable.getbitmap (). isRecycled ()) {
Bitmapdrawable.getbitmap (). Recycle ();
}

Show pictures
Img.setimagebitmap (Bitmapfactory.decodestream (assetfile));
}
});
}

In addition, in some games constantly moving background, such as the classic "Thunder" aircraft game, through the continuous excavation of a part of the background image, giving the senses caused by the illusion of the aircraft constantly moving. Can be achieved by CreateBitmap (Bitmap bitmap,int x,int y,int width,int height) method. As follows:

public class Mainactivity extends Activity {

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (New MyView (this));
}

Class MyView extends view{

Record the actual height of the background bitmap
Final int back_height = 1700;
Background image
Private Bitmap back;
Private Bitmap plane;

Start position of background picture
Final int WIDTH = 320;
Final int HEIGHT = 440;

private int starty = Back_height-height;

Public MyView (Context context) {
Super (context);

back = Bitmapfactory.decoderesource (Context.getresources (), r.drawable.back_img);

Plane = Bitmapfactory.decoderesource (Context.getresources (), R.drawable.plane);

Final Handler Mhandler = new Handler () {
@Override
public void Handlemessage (Message msg) {

if (Msg.what ==0x123) {
Start moving again
if (starty<=0) {
Starty = Back_height-height;
}
Else
Starty-= 3;
}
Invalidate ();
}
};
New Timer (). Schedule (new TimerTask () {

@Override
public void Run () {
Mhandler.sendemptymessage (0x123);
}
}, 0,100);


}
@Override
protected void OnDraw (canvas canvas) {
Create a new picture based on the original bitmap and matrix
Bitmap bitmap_2 = Bitmap.createbitmap (back, 0, Starty, WIDTH, HEIGHT);

Draw a new bitmap
Canvas.drawbitmap (bitmap_2, 0,0,null);

Drawing airplanes
Canvas.drawbitmap (plane, 380,null);
}

}

}

Some graphic effects

Matrix matrix = new Matrix ();

Mirror effect
Matrix.setscale (-1, 1);
Matrix.posttranslate (Bitmap.getwidth (), 0);

Reflection effect
Matrix.setscale (1,-1);
Matrix.posttranslate (0, Bitmap.getheight ());

Effect of image compositing (Android.graphics.PorterDuff.Mode)

Set the effect of overlapping parts
Paint.setxfermode (New Porterduffxfermode (Android.graphics.PorterDuff.Mode.DST_ATOP));


Canvas.drawbitmap (Bitmapa, New Matrix (), paint);

Canvas.drawbitmap (BITMAPB, New Matrix (), paint);

Some value of Android.graphics.PorterDuff.Mode 1.porterduff.mode.clear
The drawing is not submitted to the canvas.

2.porterduff.mode.src
Show Top Draw picture

3.porterduff.mode.dst
Show Lower drawing pictures

4.porterduff.mode.src_over
Normal drawing shows that the upper and lower layers are drawn with overlapping covers.

5.porterduff.mode.dst_over
The upper and lower layers are displayed. The lower level is shown on the home.

6.porterduff.mode.src_in
Draw the intersection of two layers. Displays the upper layer.

7.porterduff.mode.dst_in
Draw the intersection of two layers. Displays the lower layer.

8.porterduff.mode.src_out
Draw the non-intersecting portion of the upper layer.

9.porterduff.mode.dst_out
Remove the layer to draw the non-intersecting part.

10.porterduff.mode.src_atop
Remove the non-intersecting part of the layer from the upper intersection section

11.porterduff.mode.dst_atop
Take the upper non-intersecting part and the lower part of the intersection

12.porterduff.mode.xor
Draw a non-intersection of two layers. The two layers draw non-intersecting.

13.porterduff.mode.darken
The upper and lower layers are displayed. Darken

14.porterduff.mode.lighten
The upper and lower layers are displayed. Variable

15.porterduff.mode.multiply
Draw the intersection of two layers

16.porterduff.mode.screen
The upper and lower layers are displayed.

Android Learning-graphic image processing (Bitmap, bitmapfactory) (i)

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.