Android[進階教程] Android程式調用本地圖片並進行繪製

來源:互聯網
上載者:User

上一篇我們介紹了如何調用本機內建網路攝影機,這篇我們就接上一篇的,如何調用本機圖片程式來選擇圖片,並在選擇的圖片上對手指的手勢進行繪製,先來看圖片

首先看一下布局,這裡面只有一個按鈕和一個圖片

<?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;}

其實代碼不多,大家基本能看得懂吧,要源碼的請發留言吧,也是抽空寫一下自己學習的代碼,希望對大家有協助。



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.