上一篇我们介绍了如何调用本机自带摄像头,这篇我们就接上一篇的,如何调用本机图片程序来选择图片,并在选择的图片上对手指的手势进行绘制,先来看图片
首先看一下布局,这里面只有一个按钮和一个图片
<?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);
下一步就是对选择的图片进行处理了
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { //取得返回数据的Uri Uri 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; //触屏事件 @Override public 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); //刷新ImageView choosenImageView.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; }其实代码不多,大家基本能看得懂吧,要源码的请发留言吧,也是抽空写一下自己学习的代码,希望对大家有帮助。