Summary of trivial knowledge points in Android programming (3)

Source: Internet
Author: User
Tags drawtext

1. image loading method to help users load images

Java code
  1. /***
  2. * Load local Images
  3. *
  4. * @ Param Context
  5. *: Main running function instance
  6. * @ Param bitadress
  7. *: Image address, which generally points to the drawable directory under R.
  8. * @ Return
  9. */
  10. Public final bitmap creatimage (context, int bitadress ){
  11. Bitmap bitmaptemp = NULL;
  12. Bitmaptemp = bitmapfactory. decoderesource (context. getresources (),
  13. Bitadress );
  14. Return bitmaptemp;
  15. }

2. The image mean segmentation method divides a larger image into N rows and n columns on average for ease of use

Java code
  1. /***
  2. * Image Segmentation
  3. *
  4. * @ Param g
  5. *: Canvas
  6. * @ Param paint
  7. *: Paint brush
  8. * @ Param imgbit
  9. *: Image
  10. * @ Param x
  11. *: X-axis start Coordinate
  12. * @ Param y
  13. *: Y-axis start Coordinate
  14. * @ Param W
  15. *: Width of a single image
  16. * @ Param H
  17. *: Height of a single image
  18. * @ Param line
  19. *: Column number
  20. * @ Param row
  21. *: Row number
  22. */
  23. Public final void cuteimage (canvas g, paint, bitmap imgbit, int X,
  24. Int y, int W, int H, int line, int row ){
  25. G. cliprect (X, Y, x + W, H + y );
  26. G. drawbitmap (imgbit, X-line * w, Y-row * H, paint );
  27. G. Restore ();
  28. }

3. resize the current image

Java code
  1. /***
  2. * Image Scaling Method
  3. *
  4. * @ Param bgimage
  5. *: Source image resources
  6. * @ Param newwidth
  7. *: Width after scaling
  8. * @ Param newheight
  9. *: Zoom height
  10. * @ Return
  11. */
  12. Public bitmap zoomimage (Bitmap bgimage, int newwidth, int newheight ){
  13. // Obtain the width and height of the image.
  14. Int width = bgimage. getwidth ();
  15. Int Height = bgimage. getheight ();
  16. // Create a matrix object for image operations
  17. Matrix matrix = new matrix ();
  18. // Calculate the zooming rate. The new size excludes the original size.
  19. Float scalewidth = (float) newwidth)/width;
  20. Float scaleheight = (float) newheight)/height;
  21. // Scaling the image
  22. Matrix. postscale (scalewidth, scaleheight );
  23. Bitmap bitmap = bitmap. createbitmap (bgimage, 0, 0, width, height,
  24. Matrix, true );
  25. Return bitmap;
  26. }

4. Draw text with borders, which is generally used to beautify the text in the game

Java code
  1. /***
  2. * Draw text with borders
  3. *
  4. * @ Param strmsg
  5. *: Draw content
  6. * @ Param g
  7. *: Canvas
  8. * @ Param paint
  9. *: Paint brush
  10. * @ Param setx
  11. *: Start coordinate of the X axis
  12. * @ Param sety
  13. *: Start coordinate of the Y axis
  14. * @ Param FG
  15. *: Foreground color
  16. * @ Param BG
  17. *: Background Color
  18. */
  19. Public void drawtext (string strmsg, canvas g, paint, int setx,
  20. Int sety, int FG, int BG ){
  21. Paint. setcolor (BG );
  22. G. drawtext (strmsg, setx + 1, sety, paint );
  23. G. drawtext (strmsg, setx, sety-1, paint );
  24. G. drawtext (strmsg, setx, sety + 1, paint );
  25. G. drawtext (strmsg, setx-1, sety, paint );
  26. Paint. setcolor (FG );
  27. G. drawtext (strmsg, setx, sety, paint );
  28. G. Restore ();
  29. }

5. The easiest way to separate images

Java code
  1. Public final bitmap cuteimage (Bitmap _ imgbit, int _ startx, int width,
  2. Int _ starty, int height ){
  3. Bitmap tempmap = NULL;
  4. Tempmap = bitmap. createbitmap (_ imgbit, _ startx, _ starty, width, height );
  5. Return tempmap;
  6. }

6. String branch display

Java code
  1. Public String [] stringformat (string text, int maxwidth, int fontsize ){
  2. String [] result = NULL;
  3. Vector <string> tempr = new vector <string> ();
  4. Int lines = 0;
  5. Int Len = text. Length ();
  6. Int index0 = 0;
  7. Int index1 = 0;
  8. Boolean wrap;
  9. While (true ){
  10. Int widthes = 0;
  11. Wrap = false;
  12. For (index0 = index1; index1 <Len; index1 ++ ){
  13. If (text. charat (index1) = '\ n '){
  14. Index1 ++;
  15. Wrap = true;
  16. Break;
  17. }
  18. Widthes = fontsize + widthes;
  19. If (widthes> maxwidth ){
  20. Break;
  21. }
  22. }
  23. Lines ++;
  24. If (WRAP ){
  25. Tempr. addelement (text. substring (index0, index1-1 ));
  26. } Else {
  27. Tempr. addelement (text. substring (index0, index1 ));
  28. }
  29. If (index1> = Len ){
  30. Break;
  31. }
  32. }
  33. Result = new string [Lines];
  34. Tempr. copyinto (result );
  35. Return result;
  36. }

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.