Because many of the Code has been designed for the first five advanced steps, we will not repeat them here. List the core code.
Part 1: xml file
Select an image with one button and save the image with one button
The Code is as follows:
1. <? Xml version = "1.0" encoding = "UTF-8"?>
2. <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
3. android: layout_width = "fill_parent"
4. android: layout_height = "fill_parent"
5. android: orientation = "vertical">
6.
7. <Button
8. android: id = "@ + id/pickImageBtn"
9. android: layout_width = "wrap_content"
10. android: layout_height = "wrap_content"
11. android: text = "pickImage"/>
12.
13. <ImageView
14. android: id = "@ + id/pickedImage"
15. android: layout_width = "wrap_content"
16. android: layout_height = "wrap_content"
17. android: src = "@ drawable/ic_launcher"/>
18.
19. <Button
20. android: id = "@ + id/saveBtn"
21. android: layout_width = "wrap_content"
22. android: layout_height = "wrap_content"
23. android: text = "Save"/>
24.
25. </LinearLayout>
Part 2: initialization
The Code is as follows:
1. public void onCreate (Bundle savedInstanceState ){
2. super. onCreate (savedInstanceState );
3. setContentView (R. layout. main );
4.
5. Button pickImageBtn = (Button) findViewById (R. id. pickImageBtn );
6. Button saveBtn = (Button) findViewById (R. id. saveBtn );
7. mImageView = (ImageView) findViewById (R. id. pickedImage );
8.
9.
10. pickImageBtn. setOnClickListener (this );
11. saveBtn. setOnClickListener (this );
12.
13.
14 .}
Part 3: Select images and listen to Touch
The Code is as follows:
1. public void onClick (View v ){
2. // TODO Auto-generated method stub
3. Log. d ("bitmap", "has onClick ");
4. switch (v. getId ()){
5. case R. id. pickImageBtn:
6. Intent intent = new Intent (Intent. ACTION_PICK, android. provider. MediaStore. Images. Media. EXTERNAL_CONTENT_URI );
7. startActivityForResult (intent, REQUEST_CODE );
8. break;
1. @ Override
2. protected void onActivityResult (int requestCode, int resultCode, Intent intent ){
3. // TODO Auto-generated method stub
4. super. onActivityResult (requestCode, resultCode, intent );
5. Log. d ("bitmap", "requestCode is:" + requestCode );
6. if (resultCode = RESULT_ OK ){
7. Log. d ("bitmap", "has result OK ");
8. Uri uri = intent. getData ();
9.
10. int dw = getWindowManager (). getdefadisplay display (). getWidth ();
11. int dh = getWindowManager (). getdefadisplay display (). getHeight ();
12.
13. try {
14. BitmapFactory. Options opts = new BitmapFactory. Options ();
15. opts. inJustDecodeBounds = true; // if it is set to true, no response is returned.
16. Bitmap chooseBitmap = BitmapFactory. decodeStream (getContentResolver (). openInputStream (uri), null, opts );
17. int bw = opts. outWidth; // at this time, the chooseBitmap value is null, but opts still obtains its config
18. int bh = opts. outHeight;
19.
20. int widthRatio = (int) Math. ceil (bw/(float) dw );
21. int heightRatio = (int) Math. ceil (bh/(float) dh );
22.
23. if (widthRatio> 1 | heightRatio> 1 ){
24. if (widthRatio> heightRatio ){
25. opts. inSampleSize = widthRatio; // set the proportion
26.} else {
27. opts. inSampleSize = heightRatio;
28 .}
29 .}
30. opts. inJustDecodeBounds = false;
31. chooseBitmap = BitmapFactory. decodeStream (getContentResolver (). openInputStream (uri), null, opts );
32. Log. d ("bitmap", "chooseBitmap is:" + chooseBitmap );
33.
34. alteredBitmap = Bitmap. createBitmap (chooseBitmap. getWidth (), chooseBitmap. getHeight (), chooseBitmap. getConfig ());
35. canvas = new Canvas (alteredBitmap); // canvas
36. paint = new Paint ();
37. paint. setColor (Color. WHITE );
38. paint. setStyle (Style. STROKE );
39. Matrix matrix = new Matrix ();
40. canvas. drawBitmap (chooseBitmap, matrix, paint );
41.
42. mImageView. setImageBitmap (alteredBitmap );
43. mImageView. setOnTouchListener (this); // set the listener
44.
45.} catch (FileNotFoundException e ){
46. // TODO Auto-generated catch block
47. e. printStackTrace ();
48 .}
49.
50.
51.
52 .}
53.
54 .}
Part 4: Touch and move images and draw images in real time.
The Code is as follows:
1. public boolean onTouch (View v, MotionEvent event ){
2. // TODO Auto-generated method stub
3. Log. d ("touch_draw", "ontouch ()");
4.
5. switch (event. getAction ()){
6. case MotionEvent. ACTION_UP:
7. // upX = event. getX ();
8. // upY = event. getY (); // Code related to draw a straight line. You can test it by yourself. Likewise, you can set the circle and ellipse in the relevant method.
9. // canvas. drawLine (downX, downY, upX, upY, paint );
10. // mImageView. invalidate ();
11. // break;
12. case MotionEvent. ACTION_DOWN:
13. downX = event. getX ();
14. downY = event. getY ();
15. break;
16. case MotionEvent. ACTION_MOVE:
17. upX = event. getX ();
18. upY = event. getY ();
19. canvas. drawLine (downX, downY, upX, upY, paint );
20. mImageView. invalidate ();
21. downX = upX;
22. downY = upY;
23. break;
24. case MotionEvent. ACTION_CANCEL:
25. break;
26.
27 .}
28.
29. return true;
30 .}
Part 5: save images
The Code is as follows:
1. public void onClick (View v ){
2. // TODO Auto-generated method stub
3. Log. d ("bitmap", "has onClick ");
4. switch (v. getId ()){
5. case R. id. pickImageBtn:
6. Intent intent = new Intent (Intent. ACTION_PICK, android. provider. MediaStore. Images. Media. EXTERNAL_CONTENT_URI );
7. startActivityForResult (intent, REQUEST_CODE );
8. break;
9. case R. id. saveBtn: // Save the graffiti Image
10.
11.
12 ./*
13. * PNG: Perfect for artistic lines and graphics: Always keep all data
14. * JPEG: Perfect for gradient full-color images, such as photos. It is a "lossy" codecs and quality can be set
15 .*/
16.
17.
18. if (null! = AlteredBitmap ){
19. Uri imageFileUri = getContentResolver (). insert (Media. EXTERNAL_CONTENT_URI, new ContentValues (); // create a new uri
20.
21. try {
22. OutputStream imageFileOS = getContentResolver (). openOutputStream (imageFileUri); // output stream
23.
24. alteredBitmap. compress (CompressFormat. JPEG, 90, imageFileOS); // generate an image
25.
26. Toast. makeText (this, "has saved", Toast. LENGTH_SHORT). show ();
27.
28.} catch (FileNotFoundException e ){
29. // TODO Auto-generated catch block
30. e. printStackTrace ();
31 .}
32 .}
33. break;
34 .}
35 .}
From xiaoxin's column