Bitmaps are the most commonly used resource in our development, after all, a beautiful interface is the most attractive to the user.
1. Getting a bitmap from a resource
You can use bitmapdrawable or bitmapfactory to get a bitmap from a resource.
Of course, you need to get resources first:
Resources res=getresources ();
Get a bitmap using bitmapdrawable
- Use Bitmapdrawable (InputStream is) to construct a bitmapdrawable;
- 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 use the following method:
Bitmapdrawable bmpdraw= (bitmapdrawable) res.getdrawable (r.drawable.pic180); Bitmap Bmp=bmpdraw.getbitmap ();
Get a bitmap using Bitmapfactory
(creates Bitmap objects from various sources, including files, streams, and byte-arrays.)
Use the Bitmapfactory class Decodestream (InputStream is) to decode the bitmap resource and get the bitmap.
Bitmap Bmp=bitmapfactory.decoderesource (res, r.drawable.pic180);
All the functions of bitmapfactory are static, and this helper class can get bitmaps through resource IDs, paths, files, data streams, and so on.
The above method is free to choose when programming, in the Android SDK description can support the image format as follows: PNG (preferred), JPG (acceptable), GIF (discouraged), and BMP (Android SDK Support Media Format).
2. Get information about a bitmap
To get the bitmap information, such as bitmap size, pixels, density, transparency, color format, and so on, get the bitmap will be solved, this information in bitmap's manual, here is only auxiliary to explain the following 2 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.
3. Displaying bitmaps
Display bitmaps can use the core class canvas, display bitmaps through the canvas class's Drawbirmap (), or draw bitmap to canvas with the help of bitmapdrawable. Of course, the bitmap can also be displayed in the view by Bitmapdrawable.
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);
Show bitmap
ImageView iv2 = (ImageView) Findviewbyid (R.ID.IMAGEVIEW02);
Iv2.setimagedrawable (Bmpdraw);
displaying bitmaps using the canvas class
Here is a subclass panel that inherits from view, which is displayed in the OnDraw of the subclass
public class Mainactivity extends activity { /** called when the activity is first created. */ @Override publi c void OnCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (New Panel (this)); } Class Panel extends view{public Panel (context context) { super (context); } public void OnDraw (canvas canvas) { Bitmap bmp = Bitmapfactory.decoderesource (Getresources (), r.drawable.pic180); Canvas.drawcolor (color.black); Canvas.drawbitmap (BMP, Ten, ten, null); } } }
4. Bitmap 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) on the basis of 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 help of the canvas scale (float SX, float sy) (Preconcat the present matrix with the specified scales), but be aware that the entire canvas is scaled at this time.
(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);
5. Bitmap 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); Matrix matrix=new Matrix (); Matrix.postscale (0.8f, 0.8f); Matrix.postrotate (); Bitmap Dstbmp=bitmap.createbitmap (Bmp,0,0,bmp.getwidth (), bmp.getheight (), matrix,true); Canvas.drawcolor (Color.Black);
Rotation effect:
6. How to generate image watermark
The process of generating a watermark. In fact, there are three links: first, load the original picture; second, load the watermark picture; third, save the new picture.
- /**
- * Create the bitmap from a byte array
- *
- * @param src The Bitmap object you want Proecss
- * @param watermark The water mark above the SRC
- * @return return a Bitmap object, if paramter ' s length is 0,return null
- */
- Private Bitmap CreateBitmap (Bitmap src, Bitmap watermark)
- {
- String tag = "CreateBitmap";
- LOG.D (Tag, "Create a new bitmap");
- if (src = = null )
- {
- return null;
- }
- int w = src.getwidth ();
- int h = src.getheight ();
- int ww = watermark.getwidth ();
- int wh = watermark.getheight ();
- Create the new blank bitmap
- Bitmap newb = Bitmap.createbitmap (W, H, config.argb_8888);//Create a new bitmap with the same src length width
- Canvas CV = new canvas (NEWB);
- Draw Src into
- Cv.drawbitmap (SRC, 0, 0, null );//at 0, 0 coordinates begin to draw into SRC
- Draw Watermark into
- Cv.drawbitmap (watermark, W-WW + 5, H-WH + 5, null );//Draw a watermark in the lower right corner of SRC
- Save All Clip
- Cv.save (Canvas.all_save_flag);//Save
- Store
- Cv.restore ();//Storage
- return newb;
- }
7.Canvas of Save and restore
The OnDraw method passes in a canvas object, which is the one you use to draw the visual interface of the control.
In the OnDraw method, we often see calls to the Save and restore methods, what exactly are they used for?
? Save: Used to save the state of the canvas. After save, you can invoke canvas panning, indenting, rotating, trimming, cropping, and so on.
? Restore: The state used to restore the canvas before it is saved. Preventing the actions that are performed on the canvas after save has an impact on subsequent drawing.
Save and restore are paired (restore can be less than save, but not many), and error will be raised if the restore is called more times than save. between save and restore, there is often a special manipulation of the canvas.
For example: We would like to draw a right-pointing triangular arrow on the canvas, of course, we can draw directly, in addition, we can also rotate the canvas 90 °, draw an upward arrow, and then rotate back (this rotation is very useful for the marker on the circumference of the drawing). Then, we want to have a 20-pixel circle in the lower-right corner, so the core code in OnDraw is:
int px = Getmeasuredwidth ();
int py = Getmeasuredwidth ();
Draw background
Canvas.drawrect (0, 0, px, py, backgroundpaint);
Canvas.save ();
Canvas.rotate (PX/2, PY/2);
Draw Up ARROW
Canvas.drawline (PX/2, 0, 0, PY/2, linepaint);
Canvas.drawline (PX/2, 0, px, PY/2, linepaint);
Canvas.drawline (PX/2, 0, PX/2, py, linepaint);
Canvas.restore ();
Draw Circle
Canvas.drawcircle (px-10, py-10, ten, linepaint);
The effect 1 shows:
What would it look like if we didn't call save and restore? 2 is shown below:
From these two graphs, we can see the obvious difference in the position of the circle. Without the save and restore operations of the canvas, all images are drawn on the canvas after the canvas rotates 90°. When the OnDraw method is executed, the system automatically restores the canvas back. When the save and restore operations are executed differently, they can cause the drawing to be different.
Android bitmap and Canvas learning notes