1,setcompounddrawables (drawable left, drawable top,drawable right, drawable bottom)
Sets the picture to appear in each direction of the Textview,button,edittext. Where left is the drawable type.
2. How to obtain the above drawable
drawable drawable = Getresources (). getdrawable (R.drawable.username);
Drawable.setbounds (5,1,60,50);
Set the coordinates of the drawable to 5, 1, Width and height: 60 and 50
3.
When you manually set the position of text relative to the picture, the following methods are commonly used:
Setcompounddrawables (left, top, right, bottom)
Setcompounddrawableswithintrinsicbounds (left, top, right, bottom)
This means that the settings drawable appear in the left, top, right, and bottom positions of text.
But there are some differences:
The width height of the drawable of the setcompounddrawables painting is set by Drawable.setbound (),
So only the drawables must already have had SetBounds (Rect) called.
You must use Drawable.setbounds to set the drawable width before you use it.
Setcompounddrawableswithintrinsicbounds is a painted drawable wide height which is fixed by drawable,
So only the drawables ' bounds will is set to their intrinsic bounds.
Which is obtained through Getintrinsicwidth () and Getintrinsicheight (),
4.
1, drawable is a can be drawn object, it may be a bitmap (bitmapdrawable), it may be a graph (shapedrawable), there may be a layer (layerdrawable), we based on the demand for paint, Create the appropriate object to draw
2, canvas canvas, the destination area of the drawing, for drawing
3, Bitmap bitmap, for the processing of graphs
4. Matrix matrices
1. Get bitmap from the resources1. Resources res = getresources (); 2. Bitmap bmp = Bitmapfactory.decoderesource (res, r.drawable.icon);
2, bitmap→byte[] Public byte[] Bitmap2bytes (Bitmap BM) {
Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
Bm.compress (Bitmap.CompressFormat.PNG, BAOs);
return Baos.tobytearray ();
}
Public Bitmap Bytes2bimap (byte[] b) {if (b.length! = 0) {return Bitmapfactory.decodebytearray (b, 0, b.length); } else {return null; } } Public static Bitmap Zoombitmap (Bitmap Bitmap, int width, int height) {int w = bitmap.getwidth (); int h = bitmap.getheight (); Matrix matrix = new Matrix (); Float ScaleWidth = ((float) width/w); Float ScaleHeight = ((float) height/h); Matrix.postscale (ScaleWidth, ScaleHeight); Bitmap newbmp = Bitmap.createbitmap (Bitmap, 0, 0, W, h, Matrix, true); return newbmp; } drawable converted into bitmap Public static Bitmap Drawabletobitmap (drawable drawable) {//Take drawable of the length of the widthint w = drawable.getintrinsicwidth (); int h = drawable.getintrinsicheight (); //Take drawable color formatbitmap.config Config = drawable.getopacity ()! = Pixelformat.opaque? Bitmap.Config.ARGB_8888: Bitmap.Config.RGB_565; //Establish correspondence bitmapBitmap Bitmap = Bitmap.createbitmap (w, H, config); //Create a canvas corresponding to bitmapCanvas Canvas = new canvas (bitmap); drawable.setbounds (0, 0, W, h); //Draw the drawable content to the canvasDrawable.draw (canvas); return bitmap; } Get rounded pictures Public static Bitmap Getroundedcornerbitmap (Bitmap Bitmap, float roundpx) {int w = bitmap.getwidth (); int h = bitmap.getheight (); Bitmap output = Bitmap.createbitmap (W, H, config.argb_8888); Canvas Canvas = new canvas (output); final int color = 0xff424242; Final Paint paint = new paint (); Final Rect rect = new Rect (0, 0, W, h); final RECTF RECTF = new RECTF (rect); 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; } 1, bitmap converted into drawable . Bitmap bm=xxx; XXX based on your situation to obtainbitmapdrawable bd= New Bitmapdrawable (GetResource (), BM); because Btimapdrawable is a subclass of drawable, it is finally possible to use the BD object directly. 2, Drawable zoomPublic static drawable zoomdrawable (drawable drawable, int w, int h) {int width = drawable.getintrinsicwidth (); int height = drawable.getintrinsicheight (); //drawable converted to bitmapBitmap oldbmp = Drawabletobitmap (drawable); //Create a Matrix object for manipulating picturesMatrix matrix = new Matrix (); //Calculate zoom ratioFloat SX = ((float) w/width); Float sy = ((float) h/height); //Set zoom ratioMatrix.postscale (SX, SY); //Create a new bitmap, whose content is a scaled graph of the original bitmapBitmap newbmp = Bitmap.createbitmap (oldbmp, 0, 0, width, height,matrix, True); return new bitmapdrawable (newbmp); }
Some easy-to-forget knowledge points in Android