Some easy-to-Forget knowledge points in android and Forgotten knowledge points in android
1, setCompoundDrawables (Drawable left, Drawable top, Drawable right, Drawable bottom)
Set the orientation of the image to appear in textView, button, and editText. left indicates drawable.
2. how to obtain the above drawable
Drawable drawable = getResources (). getDrawable (R. drawable. username );
Drawable. setBounds (5, 1, 60, 50 );
Set the coordinates of drawable to 5, 1, width, and height to 60 and 50.
3.
You can use the following methods to manually set the relative position of text and image:
SetCompoundDrawables (left, top, right, bottom)
SetCompoundDrawablesWithIntrinsicBounds (left, top, right, bottom)
Set the left, top, right, and bottom positions of the Drawable text.
But there are some differences between the two:
SetCompoundDrawablesThe width and height of the drawn drawable are the width and height set by drawable. setBound,
Therefore, The Drawables must already have had setBounds (Rect) called is available.
Before use, you must use Drawable. setBounds to set the length and width of Drawable.
SetCompoundDrawablesWithIntrinsicBoundsThe width and height of the drawable is fixed by drawable,
Therefore, The Drawables 'bounds will be set to their intrinsic bounds.
It is obtained through getIntrinsicWidth () and getIntrinsicHeight,
4.
1. Drawable is a printable object. It may be a BitmapDrawable, A ShapeDrawable, or LayerDrawable ), based on the drawing requirements, we can create corresponding printable objects.
2. Canvas, the target area of the drawing, used for drawing
3. Bitmap for graph Processing
4. Matrix
1. Obtain Bitmap from the Resource
1. 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, 100, 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 into bitmap
Public static Bitmap drawableToBitmap (Drawable drawable ){
// Obtain the length and width of drawable
Int w = drawable. getIntrinsicWidth ();
Int h = drawable. getIntrinsicHeight ();
// Obtain the drawable color format
Bitmap. Config config = drawable. getOpacity ()! = PixelFormat. OPAQUE? Bitmap. Config. ARGB_8888
: Bitmap. Config. RGB_565;
// Create a corresponding bitmap
Bitmap bitmap = Bitmap. createBitmap (w, h, config );
// Create a canvas for the corresponding bitmap
Canvas canvas = new Canvas (bitmap );
Drawable. setBounds (0, 0, w, h );
// Draw the drawable content into the canvas
Drawable. draw (canvas );
Return bitmap;
}
Obtain rounded corner Image
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 = 0xff0000242;
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 porterduxfermode (Mode. SRC_IN ));
Canvas. drawBitmap (bitmap, rect, rect, paint );
Return output;
}
1. Bitmap to Drawable
. Bitmap bm = xxx; // xxx is obtained based on your situation
BitmapDrawable bd = new BitmapDrawable (getResource (), bm );
Because BtimapDrawable is a subclass of Drawable, you can directly use the bd object.
2. Drawable Scaling
Public static Drawable zoomDrawable (Drawable drawable, int w, int h ){
Int width = drawable. getIntrinsicWidth ();
Int height = drawable. getIntrinsicHeight ();
// Converts drawable to bitmap.
Bitmap oldbmp = drawableToBitmap (drawable );
// Create a Matrix object for image operations
Matrix matrix = new Matrix ();
// Calculate the scaling ratio
Float sx = (float) w/width );
Float sy = (float) h/height );
// Set the scaling ratio
Matrix. postScale (sx, sy );
// Create a new bitmap with the scaled image of the original bitmap
Bitmap newbmp = Bitmap. createBitmap (oldbmp, 0, 0, width, height,
Matrix, true );
Return new BitmapDrawable (newbmp );
}