Android Development notes-Bitmap

Source: Internet
Author: User

Today, Bitmap is used many times. Bitmap is simply a Bitmap. You need to obtain image resources for some image processing operations.

1. Related Concepts
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 Bitmap from resources
  1. Resources res = getResources ();
  2. Bitmap bmp = BitmapFactory. decodeResource (res, R. drawable. icon );

 

2. Bitmap → byte []
  1. Public byte [] Bitmap2Bytes (Bitmap bm ){
  2. ByteArrayOutputStream baos = new ByteArrayOutputStream ();
  3. Bm. compress (Bitmap. CompressFormat. PNG, 100, baos );
  4. Return baos. toByteArray ();
  5. }
3. byte [] → Bitmap
  1. Public Bitmap Bytes2Bimap (byte [] B ){
  2. If (B. length! = 0 ){
  3. Return BitmapFactory. decodeByteArray (B, 0, B. length );
  4. } Else {
  5. Return null;
  6. }
  7. }
4. Bitmap Scaling
  1. Public static Bitmap zoomBitmap (Bitmap bitmap, int width, int height ){
  2. Int w = bitmap. getWidth ();
  3. Int h = bitmap. getHeight ();
  4. Matrix matrix = new Matrix ();
  5. Float scaleWidth = (float) width/w );
  6. Float scaleHeight = (float) height/h );
  7. Matrix. postScale (scaleWidth, scaleHeight );
  8. Bitmap newbmp = Bitmap. createBitmap (bitmap, 0, 0, w, h, matrix, true );
  9. Return newbmp;
  10. }
5. Convert Drawable into Bitmap
  1. Public static Bitmap drawableToBitmap (Drawable drawable ){
  2. // Obtain the length and width of drawable
  3. Int w = drawable. getIntrinsicWidth ();
  4. Int h = drawable. getIntrinsicHeight ();
  5. // Obtain the drawable color format
  6. Bitmap. Config config = drawable. getOpacity ()! = PixelFormat. OPAQUE? Bitmap. Config. ARGB_8888
  7. : Bitmap. Config. RGB_565;
  8. // Create a corresponding bitmap
  9. Bitmap bitmap = Bitmap. createBitmap (w, h, config );
  10. // Create a canvas for the corresponding bitmap
  11. Canvas canvas = new Canvas (bitmap );
  12. Drawable. setBounds (0, 0, w, h );
  13. // Draw the drawable content into the canvas
  14. Drawable. draw (canvas );
  15. Return bitmap;
  16. }
6. Obtain the rounded corner Image
  1. Public static Bitmap getRoundedCornerBitmap (Bitmap bitmap, float roundPx ){
  2. Int w = bitmap. getWidth ();
  3. Int h = bitmap. getHeight ();
  4. Bitmap output = Bitmap. createBitmap (w, h, Config. ARGB_8888 );
  5. Canvas canvas = new Canvas (output );
  6. Final int color = 0xff0000242;
  7. Final Paint paint = new Paint ();
  8. Final Rect rect = new Rect (0, 0, w, h );
  9. Final RectF rectF = new RectF (rect );
  10. Paint. setAntiAlias (true );
  11. Canvas. drawARGB (0, 0, 0, 0 );
  12. Paint. setColor (color );
  13. Canvas. drawRoundRect (rectF, roundPx, roundPx, paint );
  14. Paint. setXfermode (new porterduxfermode (Mode. SRC_IN ));
  15. Canvas. drawBitmap (bitmap, rect, rect, paint );
  16. Return output;
  17. }
7. Get images with reflections
  1. Public static Bitmap createReflectionImageWithOrigin (Bitmap bitmap ){
  2. Final int reflectionGap = 4;
  3. Int w = bitmap. getWidth ();
  4. Int h = bitmap. getHeight ();
  5. Matrix matrix = new Matrix ();
  6. Matrix. preScale (1,-1 );
  7. Bitmap reflectionImage = Bitmap. createBitmap (bitmap, 0, h/2, w,
  8. H/2, matrix, false );
  9. Bitmap bitmapWithReflection = Bitmap. createBitmap (w, (h + h/2 ),
  10. Config. ARGB_8888 );
  11. Canvas canvas = new Canvas (bitmapWithReflection );
  12. Canvas. drawBitmap (bitmap, 0, 0, null );
  13. Paint deafalutPaint = new Paint ();
  14. Canvas. drawRect (0, h, w, h + reflectionGap, deafalutPaint );
  15. Canvas. drawBitmap (reflectionImage, 0, h + reflectionGap, null );
  16. Paint paint = new Paint ();
  17. LinearGradient shader = new LinearGradient (0, bitmap. getHeight (), 0,
  18. BitmapWithReflection. getHeight () + reflectionGap, 0x70ffffff,
  19. 0x00ffffff, TileMode. CLAMP );
  20. Paint. setShader (shader );
  21. // Set the Transfer mode to be porter duff and destination in
  22. Paint. setXfermode (new porterduxfermode (Mode. DST_IN ));
  23. // Draw a rectangle using the paint with our linear gradient
  24. Canvas. drawRect (0, h, w, bitmapWithReflection. getHeight ()
  25. + ReflectionGap, paint );
  26. Return bitmapWithReflection;
  27. }
Iii. Drawable1. Converting Bitmap to Drawable
  1. Bitmap bm = xxx; // xxx is obtained based on your situation
  2. BitmapDrawable bd = new BitmapDrawable (getResource (), bm );
  3. Because BtimapDrawable is a subclass of Drawable, you can directly use the bd object.
2. Drawable Scaling
  1. Public static Drawable zoomDrawable (Drawable drawable, int w, int h ){
  2. Int width = drawable. getIntrinsicWidth ();
  3. Int height = drawable. getIntrinsicHeight ();
  4. // Converts drawable to bitmap.
  5. Bitmap oldbmp = drawableToBitmap (drawable );
  6. // Create a Matrix object for image operations
  7. Matrix matrix = new Matrix ();
  8. // Calculate the scaling ratio
  9. Float sx = (float) w/width );
  10. Float sy = (float) h/height );
  11. // Set the scaling ratio
  12. Matrix. postScale (sx, sy );
  13. // Create a new bitmap with the scaled image of the original bitmap
  14. Bitmap newbmp = Bitmap. createBitmap (oldbmp, 0, 0, width, height,
  15. Matrix, true );
  16. Return new BitmapDrawable (newbmp );
  17. }
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.