Android cut image display part

Source: Internet
Author: User

Method 1:

 

The canvas class is used for drawing. you can use the canvas class member functions to draw any part of the image as you wish. canvas. cliprect: Set the display area canvas. drawbitmap: Drawing example: bitmap B = bitmapfactory. decodestream ("Image number", null); // read the image... canvas c = NULL; // instance canvasc. save (); // record the original canvas status C. cliprect (100,100,200,300); // display the area from (100,100) to (200,300) (unit: pixel) C. drawbitmap (B, 10, 0, null); // draws a castrated image to (10, 0) Position C. restore (); // restore the canvas status
 
Not verified
 
Method 2:
 

In a self-defined component, the image must be displayed with numbers, while the current image only has one, with 0-9 digits on it. Therefore, you have to consider cutting down the numbers one by one, you only need to combine the numbers to display them.
The following is the key code of the program:
In the override ondraw (canvas) method in the myview (inherited from view) class, the following code snippet is provided:

Java code
  1. Bitmap resource = bitmapfactory. decoderesource (This. Getresources (), R. drawable. Num );
  2. Bitmap zero = bitmap. createbitmap (resource, 0, 0, 12, 12 );
  3. Bitmap one = bitmap. createbitmap (resource, 12, 0, 12, 12 );
  4. Bitmap two = bitmap. createbitmap (resource, 24, 0, 12, 12 );
  5. Bitmap three = bitmap. createbitmap (resource, 36, 0, 12, 12 );
  6. Bitmap four = bitmap. createbitmap (resource, 48, 0, 12, 12 );
  7. Bitmap five = bitmap. createbitmap (resource, 60, 0, 12, 12 );
  8. Bitmap six = bitmap. createbitmap (resource, 72, 0, 12, 12 );
  9. Bitmap seven = bitmap. createbitmap (resource, 84, 0, 12, 12 );
  10. Bitmap eight = bitmap. createbitmap (resource, 96, 0, 12, 12 );
  11. Bitmap nine = bitmap. createbitmap (resource, 108, 0, 12, 12 );


R. drawable. num is a digital image, and each number occupies 12*12 pixels, bitmap. the five parameters in the createbitmap method have the following meanings: The image resource to be cut, the X coordinate of the cut start point, the Y coordinate of the cut start point, the multi-width cut, and the height of the cut.
After splitting, you can easily display various numbers. For example, if you use a string number to indicate the numbers to be displayed

Java code
  1. CharNums [] = number. tochararray ();
  2. For(IntI = 0; I <nums. length; I ++ ){
  3. If(Nums [I] = '0 '){
  4. Canvas. drawbitmap (zero, I * 12, 0, mpaint );
  5. }Else If(Nums [I] = '1 '){
  6. Canvas. drawbitmap (one, I * 12, 0, mpaint );
  7. }Else If(Nums [I] = '2 '){
  8. Canvas. drawbitmap (two, I * 12, 0, mpaint );
  9. }Else If(Nums [I] = '3 '){
  10. Canvas. drawbitmap (three, I * 12, 0, mpaint );
  11. }Else If(Nums [I] = '4 '){
  12. Canvas. drawbitmap (four, I * 12, 0, mpaint );
  13. }Else If(Nums [I] = '5 '){
  14. Canvas. drawbitmap (five, I * 12, 0, mpaint );
  15. }Else If(Nums [I] = '6 '){
  16. Canvas. drawbitmap (six, I * 12, 0, mpaint );
  17. }Else If(Nums [I] = '7 '){
  18. Canvas. drawbitmap (seven, I * 12, 0, mpaint );
  19. }Else If(Nums [I] = '8 '){
  20. Canvas. drawbitmap (eight, I * 12, 0, mpaint );
  21. }Else If(Nums [I] = '9 '){
  22. Canvas. drawbitmap (nine, I * 12, 0, mpaint );
  23. }
  24. }


Canvas is the canvas, and the four parameters in the drawbitmap method have the following meanings: The image resource to be drawn, the X coordinate of the start point to be drawn on the canvas, Y coordinate, and brush. The paint brush can not be set here. You only need to add a new one, and the paint mpaint = new paint ();

 

Success. I don't know why ????????????????????????????

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.