Bitmap, as a method of carrying images, can provide information about image files and make corresponding effects and operations on images.
I. Bitmap
Bitmap, as a method of carrying images, can provide information about image files and make corresponding effects and operations on images. There are two resource reference methods and three graphic drawing methods.
References
1) Create Bitmap in the form of BitmapFactory
◆ Declare a Bitmap object
Private Bitmap bitmap; // declare a Bitmap object |
◆ Resource reference to obtain Bitmap objects
The first method for loading resources is bitmap = BitmapFactory. decodeResource (getResources (), R. drawable. a1 ); |
2) use BitmapDrawable to create Bitmap
◆ Declare Bitmap and BitmapDrawable objects
Private Bitmap bitmap; // declare the Bitmap object private BitmapDrawable bitmapDrawable; // declare the bitmapDrawable object |
◆ Obtain BitmapDrawable object through resource reference
// Method 2 for loading resources bitmapDrawable = (BitmapDrawable) getResources (). getDrawable (R. drawable. a1 ); |
◆ Obtain the Bitmap object
Bitmap = bitmapDrawable. getBitmap (); // obtain the bitmap object. |
2: 3 ways to draw images
1) Draw using the drawBitmap method of the Canvas object
// The first method of drawing resources: canvas. drawBitmap (bitmap, 0, 0, null); // draw Bitmap of image size from |
2) crop the image using Rect) and RectF fill the area) Draw
// Rect rect = new Rect (0, 0, 10, 10); // crop the image starting from 00, the size of the cropped image is 10*10 RectF rectf = new RectF (20, 20, 100,100); // draw the cropped image to the starting point of 20, 20, canvas in the 80*80 area. drawBitmap (bitmap, rect, rectf, null ); |
3) use the setbounds method of bitmapDrawable to set the filling area and call the draw method of the object to draw
// The third method of resource plotting is bitmapDrawable. setBounds (0, 0,300,300); // starting from 00, the image is filled to bitmapDrawable in the area of 300*300. draw (canvas); // draw |
The above methods can be combined in any way to achieve the effect.
II. Specific applications
Function: inherits from SurfaceView and custom view to draw images.
1: Inherit from, SurfaceView, and rewrite the three methods. Pay attention to the locking and unlocking of the canvas for custom painting methods)
Public class MyView extends SurfaceView implements Callback {private Bitmap bitmap; // declare the Bitmap object private BitmapDrawable bitmapDrawable; // declare the bitmapDrawable object public MyView (Context context) {super (context ); getHolder (). addCallback (this); // The first method for loading resources // bitmap = BitmapFactory. decodeResource (getResources (), R. drawable. a1); // The second method for loading resources bitmapDrawable = (BitmapDrawable) getResources (). getDrawable (R. drawable. a1); bitmap = bitmapDrawable. getBitmap (); // get bitmap object} public void draw () {Canvas canvas = getHolder (). lockCanvas (); // lock the canvas. drawColor (Color. WHITE); // The first method of resource plotting // canvas. drawBitmap (bitmap, 0, 0, null); // draw the Bitmap of the image size from. // The second method of resource painting // Rect rect = new Rect, 10, 10); // crop the image, starting from 00. The cropped image size is 10*10 // RectF rectf = new // RectF (20, 20, 100,100 ); // draw the cropped image to the area where the starting point is 20, 20, and the size is 80*80. // canvas. drawBitmap (bitmap, rect, rectf, null); // The third method is bitmapDrawable. setBounds (0, 0,300,300); // starting from 00, the image is filled to bitmapDrawable in the area of 300*300. draw (canvas); // draw getHolder (). unlockCanvasAndPost (canvas); // unlock canvas} @ Override public void surfaceChanged (SurfaceHolder holder, int format, int width, int height) {}@ Override public void surfaceCreated (SurfaceHolder holder) {draw (); // call} @ Override public void surfaceDestroyed (SurfaceHolder holder ){}} |
2: Introduce custom views to the main interface
Public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (new MyView (this); // load custom views} |
3: Result: The image is tiled from 0 to 0 to 300,300.
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/093S02120-0.jpg "title =" Unnamed .bmp "alt =" 093442443.jpg"/>
You once said that after I leave
Hope you have contact
Able to listen to each other's joys and sorrows
To be honest
I can't treat it rationally anymore.
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/093S05c7-1.gif "alt =" C_0004.gif "/>
This article is from the "Schindler" blog, please be sure to keep this source http://cinderella7.blog.51cto.com/7607653/1298851