上一篇我們介紹了如何調用本機內建網路攝影機,這篇我們就接上一篇的,如何調用本機圖片程式來選擇圖片,並在選擇的圖片上對手指的手勢進行繪製,先來看圖片
首先看一下布局,這裡面只有一個按鈕和一個圖片
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="選取圖片" /> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:src="@drawable/ic_launcher" /></LinearLayout>
接下來就是單擊按鈕調用原生圖片程式
//調用本機圖片程式Intent choosePictureIntent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);startActivityForResult(choosePictureIntent, 0);
下一步就是對選擇的圖片進行處理了
@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stubsuper.onActivityResult(requestCode, resultCode, data);if (resultCode == RESULT_OK) {//取得返回資料的UriUri imageFileUri = data.getData();//取得當前顯示地區Display currentDisplay = getWindowManager().getDefaultDisplay();float dw = currentDisplay.getWidth();float dh = currentDisplay.getHeight();try {//對圖片進行縮放BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();bmpFactoryOptions.inJustDecodeBounds = true;bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri), null, bmpFactoryOptions);int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight/ (float) dh);int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth/ (float) dw);if (heightRatio > 1 && widthRatio > 1) {if (heightRatio > widthRatio) {bmpFactoryOptions.inSampleSize = heightRatio;} else {bmpFactoryOptions.inSampleSize = widthRatio;}}bmpFactoryOptions.inJustDecodeBounds = false;//縮放後的圖片bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri), null, bmpFactoryOptions);//建立一張新圖片alteredBitmap = Bitmap.createBitmap(bmp.getWidth(),bmp.getHeight(), bmp.getConfig());canvas = new Canvas(alteredBitmap);paint = new Paint();paint.setColor(Color.GREEN);//調定畫筆寬度paint.setStrokeWidth(5);matrix = new Matrix();canvas.drawBitmap(bmp, matrix, paint);choosenImageView.setImageBitmap(alteredBitmap);choosenImageView.setOnTouchListener(this);} catch (Exception e) {}}}
最後就是觸屏事件了
float downx = 0;float downy = 0;float upx = 0;float upy = 0;//觸屏事件@Overridepublic boolean onTouch(View v, MotionEvent event) {int action = event.getAction();switch (action) {case MotionEvent.ACTION_DOWN:downx = event.getX();downy = event.getY();break;case MotionEvent.ACTION_MOVE:upx = event.getX();upy = event.getY();//畫線條canvas.drawLine(downx, downy, upx, upy, paint);//重新整理ImageViewchoosenImageView.invalidate();downx = upx;downy = upy;break;case MotionEvent.ACTION_UP:upx = event.getX();upy = event.getY();canvas.drawLine(downx, downy, upx, upy, paint);choosenImageView.invalidate();break;case MotionEvent.ACTION_CANCEL:break;default:break;}return true;}
其實代碼不多,大家基本能看得懂吧,要源碼的請發留言吧,也是抽空寫一下自己學習的代碼,希望對大家有協助。