Various methods for image data conversion in Android _android

Source: Internet
Author: User

Dealing with images in Android is a very common thing, and it's a way to keep track of some of the things you've used to handle picture data.

Switch to bitmap.

The RGB value turns bitmap

Copy Code code as follows:

Private Bitmap Createcolorbitmap (String rgb, int width, int height) {
Bitmap bmp = Bitmap.createbitmap (width, height, Bitmap.Config.ARGB_8888);
int color = Color.parsecolor (RGB);
Bmp.erasecolor (color);
return BMP;
}

Usage
Bitmap bmp = Createcolorbitmap ("#cce8cf", 200, 50);

The color value turns bitmap

Copy Code code as follows:

Private Bitmap createcolorbitmap (int color, int width, int height) {
Bitmap bmp = Bitmap.createbitmap (width, height, Bitmap.Config.ARGB_8888);
Bmp.erasecolor (color);
return BMP;
}
Usage
Bitmap bmp = Createcolorbitmap (Color.Blue, 200, 50);

byte array to bitmap

Copy Code code as follows:

Private Bitmap Getbitmapfrombytearray (byte[] array) {
return Bitmapfactory.decodebytearray (array, 0, array.length);
}

Read files to bitmap

Copy Code code as follows:

Private Bitmap Getbitmapfromfile (String pathName) {
Return Bitmapfactory.decodefile (PathName);
}

Read resources to transfer bitmap

Copy Code code as follows:

Private Bitmap Getbitmapfromresource (resources res, int resid) {
Return Bitmapfactory.decoderesource (res, resid);
}

Input Flow Bitmap

Copy Code code as follows:

Private Bitmap Getbitmapfromstream (InputStream inputstream) {
Return Bitmapfactory.decodestream (InputStream);
}

drawable Turn bitmap

Copy Code code as follows:

Bitmap icon = Bitmapfactory.decoderesource (Context.getresources (), R.drawable.icon_resource);

Switch to drawable.

Resource Transfer drawable

Copy Code code as follows:

drawable drawable = Getresources (). getdrawable (R.drawable.ic_launcher);

Bitmap Turn drawable
Copy Code code as follows:

drawable d = new Bitmapdrawable (Getresources (), bitmap);

Photo Fillet display

By processing the picture data bitmap, the pixels is the radius of the corner.

Copy Code code as follows:

public static Bitmap Getroundedcornerbitmap (Bitmap Bitmap, int pixels) {
Bitmap output = Bitmap.createbitmap (Bitmap.getwidth (), Bitmap
. GetHeight (), config.argb_8888);
Canvas Canvas = new Canvas (output);

final int color = 0xff424242;
Final Paint Paint = new Paint ();
Final Rect Rect = new Rect (0, 0, bitmap.getwidth (), Bitmap.getheight ());
Final RECTF RECTF = new RECTF (rect);
Final float roundpx = pixels;

Paint.setantialias (TRUE);
Canvas.drawargb (0, 0, 0, 0);
Paint.setcolor (color);
Canvas.drawroundrect (RECTF, ROUNDPX, ROUNDPX, paint);

Paint.setxfermode (New Porterduffxfermode (mode.src_in));
Canvas.drawbitmap (Bitmap, rect, rect, paint);

return output;
}

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.