Android繪制進階之六:觸摸軌跡的繪制及圖片的保存

因為很多代碼前面五次進階已經設計,在此不贅述。單列出核心代碼。
第一部分:xml文件
一個按鈕選擇圖片,一個按鈕保存圖片
代碼如下:
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>
第二部分:初始化
代碼如下:
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.     }
第三部分:選擇圖片,監聽Touch
代碼如下:
 
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().getDefaultDisplay().getWidth();
11.             int dh = getWindowManager().getDefaultDisplay().getHeight();
12.             
13.             try {
14.                 BitmapFactory.Options opts = new BitmapFactory.Options();
15.                 opts.inJustDecodeBounds = true;//如果設置為true,本身不會返回
16.                 Bitmap chooseBitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, opts);
17.                 int bw = opts.outWidth;//此時,chooseBitmap的值為null,但opts仍然獲得其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;//設置比例
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);//畫佈
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);//設置監聽
44.                 
45.             } catch (FileNotFoundException e) {
46.                 // TODO Auto-generated catch block
47.                 e.printStackTrace();
48.             }
49.             
50.             
51.             
52.         }
53.         
54.     }
 
第四部分:在圖片上觸摸移動,實時繪制。
代碼如下:
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();//畫直線的相關代碼,可自己測試。同理,畫圓橢圓都可在相關方法裡設置
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.     }
第五部分:保存圖片
代碼如下:
 
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://保存已經塗鴉過的圖片
10.             
11.             
12.             /*
13.              * PNG :非常適合藝術線條和圖形:始終保持所有數據
14.              * JPEG:非常適合帶漸變的全彩圖像,例如照片。是“有損的”編解碼器,可設置質量
15.              */
16. 
17.             
18.             if (null != alteredBitmap) {
19.                 Uri imageFileUri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, new ContentValues());//創建一個新的uri
20.                 
21.                 try {
22.                      OutputStream imageFileOS = getContentResolver().openOutputStream(imageFileUri);//輸出流
23.                      
24.                      alteredBitmap.compress(CompressFormat.JPEG, 90, imageFileOS);//生成圖片
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.     }

 
摘自 小新專欄

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。