Android draw Step 4: Draw and rotate text on the in-place Graph

Source: Internet
Author: User

1. click the button, specify the action and uri, and set the result code (ResultCode) to reach the Gallery of the default album on the mobile phone.

 
 
 

 

The Code is as follows:
 
1. public void onClick (View v ){
2. // TODO Auto-generated method stub
3. Intent intent = new Intent (Intent. ACTION_PICK,
4. android. provider. MediaStore. Images. Media. EXTERNAL_CONTENT_URI); // start the Photo Gallery
5. startActivityForResult (intent, 0 );
6 .}
2. Select an image and return the uri of the selected image.
The Code is as follows:
 
1. @ Override
2. protected void onActivityResult (int requestCode, int resultCode,
3. Intent intent ){
4. // TODO Auto-generated method stub
5. super. onActivityResult (requestCode, resultCode, intent );
6.
7. if (resultCode = RESULT_ OK) {// operation successful www.2cto.com
8. Uri imgFileUri = intent. getData (); // obtain the information of the selected photo
3. process the image and set the appropriate size (suitable for the screen)
Set BitmapFactory. Options parameter inJustDecodeBounds = true, that is, do not create bitmap, but you can obtain the attributes of bitmap. It is based on the ratio of wide to high.
The Code is as follows:
 
1. // The returned image may be too large to be fully loaded into the memory. The system has restrictions and needs to be processed.
2. Display currentDisplay = getWindowManager (). getdefadisplay Display ();
3. int defaultHeight = currentDisplay. getHeight ();
4. int defaultWidth = currentDisplay. getWidth ();
5.
6. BitmapFactory. Options bitmapFactoryOptions = new BitmapFactory. Options ();
7. bitmapFactoryOptions. inJustDecodeBounds = true; // only obtains the size of the original image without returning the Bitmap object.
8. // Note: If set to true, the decoder will return null (no bitmap),
9. // the out... fields will still be set,
10. // allowing the caller to query the bitmap without having
11. // allocate the memory for its pixels
12. try {
13. Bitmap bitmap = BitmapFactory. decodeStream (getContentResolver ()
14. openInputStream (imgFileUri), null,
15. bitmapFactoryOptions );
16. int outHeight = bitmapFactoryOptions. outHeight;
17. int outWidth = bitmapFactoryOptions. outWidth;
18. int heightRatio = (int) Math. ceil (float) outHeight
19./defaultHeight );
20. int widthRatio = (int) Math. ceil (float) outWidth
21./defaultWidth );
22.
23. if (heightRatio> 1 | widthRatio> 1 ){
24. if (heightRatio> widthRatio ){
25. bitmapFactoryOptions. inSampleSize = heightRatio;
26.} else {
27. bitmapFactoryOptions. inSampleSize = widthRatio;
28 .}
29 .}
4. inJustDecodeBounds = false. The bitmap is assigned to the first ImageView.
The Code is as follows:
 
1. bitmapFactoryOptions. inJustDecodeBounds = false;
2. bitmap = BitmapFactory. decodeStream (getContentResolver ()
3. openInputStream (imgFileUri), null,
4. bitmapFactoryOptions );
5.
6. mImageShow. setImageBitmap (bitmap );
5. Draw a bitmap (selected image) and assign it to the second ImageView.
A. Draw a front Bitmap
B. Draw text on this Bitmap
C. Create a Matrix object and specify the rotation angle.
D. Create a New bitmap object, that is, the skewed area to be drawn by the canvas.
E. Draw
The Code is as follows:
 
1 .///*
2. // * draw a bitmap on the in-place Graph
3 .//*/
4 .//
5.
6. Bitmap bitmapAltered = Bitmap. createBitmap (bitmap. getWidth (),
7. bitmap. getHeight (), bitmap. getConfig ());
8.
9. Canvas canvas = new Canvas (bitmapAltered); // bitmap provides the Canvas, only the size and size are provided here, and no background is displayed after the offset.
10.
11. Paint paint = new Paint ();
12.
13. canvas. drawBitmap (bitmap, 0, 0, paint );
14.
15. paint. setColor (Color. RED );
16.
17. canvas. drawText ("hello", 10, 10, paint );
18.
19. // draw the front image and then rotate it.
20. Matrix matrix = new Matrix ();
21.
22. matrix. setRotate (45, 0, 0 );
23. // create a skewed painting area
24. Bitmap bitmapRotated = Bitmap. createBitmap (bitmapAltered, 0, 0, bitmapAltered. getWidth (), bitmapAltered. getHeight (), matrix, false );
25. canvas. drawBitmap (bitmapAltered, matrix, paint );
26. mImageAltered. setImageBitmap (bitmapRotated );
 

 

From xiaoxin's column

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.