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
- Bitmap resource = bitmapfactory. decoderesource (This. Getresources (), R. drawable. Num );
- Bitmap zero = bitmap. createbitmap (resource, 0, 0, 12, 12 );
- Bitmap one = bitmap. createbitmap (resource, 12, 0, 12, 12 );
- Bitmap two = bitmap. createbitmap (resource, 24, 0, 12, 12 );
- Bitmap three = bitmap. createbitmap (resource, 36, 0, 12, 12 );
- Bitmap four = bitmap. createbitmap (resource, 48, 0, 12, 12 );
- Bitmap five = bitmap. createbitmap (resource, 60, 0, 12, 12 );
- Bitmap six = bitmap. createbitmap (resource, 72, 0, 12, 12 );
- Bitmap seven = bitmap. createbitmap (resource, 84, 0, 12, 12 );
- Bitmap eight = bitmap. createbitmap (resource, 96, 0, 12, 12 );
- 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
- CharNums [] = number. tochararray ();
- For(IntI = 0; I <nums. length; I ++ ){
- If(Nums [I] = '0 '){
- Canvas. drawbitmap (zero, I * 12, 0, mpaint );
- }Else If(Nums [I] = '1 '){
- Canvas. drawbitmap (one, I * 12, 0, mpaint );
- }Else If(Nums [I] = '2 '){
- Canvas. drawbitmap (two, I * 12, 0, mpaint );
- }Else If(Nums [I] = '3 '){
- Canvas. drawbitmap (three, I * 12, 0, mpaint );
- }Else If(Nums [I] = '4 '){
- Canvas. drawbitmap (four, I * 12, 0, mpaint );
- }Else If(Nums [I] = '5 '){
- Canvas. drawbitmap (five, I * 12, 0, mpaint );
- }Else If(Nums [I] = '6 '){
- Canvas. drawbitmap (six, I * 12, 0, mpaint );
- }Else If(Nums [I] = '7 '){
- Canvas. drawbitmap (seven, I * 12, 0, mpaint );
- }Else If(Nums [I] = '8 '){
- Canvas. drawbitmap (eight, I * 12, 0, mpaint );
- }Else If(Nums [I] = '9 '){
- Canvas. drawbitmap (nine, I * 12, 0, mpaint );
- }
- }
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 ????????????????????????????