Introduction to android Drawable setbounds (), drawablesetbounds
1. concepts related to Drawable
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
Ii. Bitmap1. Obtain BitmapJava code from the Resource
- Resources res = getResources ();
- Bitmap bmp = BitmapFactory. decodeResource (res, R. drawable. icon );
2. Bitmap → byte [] Java code
- Public byte [] Bitmap2Bytes (Bitmap bm ){
- ByteArrayOutputStream baos = new ByteArrayOutputStream ();
- Bm. compress (Bitmap. CompressFormat. PNG, 100, baos );
- Return baos. toByteArray ();
- }
3. byte [] → BitmapJava code
- Public Bitmap Bytes2Bimap (byte [] B ){
- If (B. length! = 0 ){
- Return BitmapFactory. decodeByteArray (B, 0, B. length );
- } Else {
- Return null;
- }
- }
4. Bitmap scaling Java code
- 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;
- }
5. Convert Drawable into BitmapJava code
- 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;
- }
6. Obtain the Java code of the 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;
- }
7. Obtain the image Java code with reflection
- Public static Bitmap createReflectionImageWithOrigin (Bitmap bitmap ){
- Final int reflectionGap = 4;
- Int w = bitmap. getWidth ();
- Int h = bitmap. getHeight ();
- Matrix matrix = new Matrix ();
- Matrix. preScale (1,-1 );
- Bitmap reflectionImage = Bitmap. createBitmap (bitmap, 0, h/2, w,
- H/2, matrix, false );
- Bitmap bitmapWithReflection = Bitmap. createBitmap (w, (h + h/2 ),
- Config. ARGB_8888 );
- Canvas canvas = new Canvas (bitmapWithReflection );
- Canvas. drawBitmap (bitmap, 0, 0, null );
- Paint deafalutPaint = new Paint ();
- Canvas. drawRect (0, h, w, h + reflectionGap, deafalutPaint );
- Canvas. drawBitmap (reflectionImage, 0, h + reflectionGap, null );
- Paint paint = new Paint ();
- LinearGradient shader = new LinearGradient (0, bitmap. getHeight (), 0,
- BitmapWithReflection. getHeight () + reflectionGap, 0x70ffffff,
- 0x00ffffff, TileMode. CLAMP );
- Paint. setShader (shader );
- // Set the Transfer mode to be porter duff and destination in
- Paint. setXfermode (new porterduxfermode (Mode. DST_IN ));
- // Draw a rectangle using the paint with our linear gradient
- Canvas. drawRect (0, h, w, bitmapWithReflection. getHeight ()
- + ReflectionGap, paint );
- Return bitmapWithReflection;
- }
Iii. Drawable1. Convert Bitmap to DrawableJava code
- 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 Java code
- 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 );
- }