因为很多代码前面五次进阶已经设计,在此不赘述。单列出核心代码。
第一部分:xml文件
一个按钮选择图片,一个按钮保存图片
代码如下:
第二部分:初始化
代码如下:
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button pickImageBtn = (Button) findViewById(R.id.pickImageBtn); Button saveBtn = (Button) findViewById(R.id.saveBtn); mImageView = (ImageView) findViewById(R.id.pickedImage); pickImageBtn.setOnClickListener(this); saveBtn.setOnClickListener(this); }
第三部分:选择图片,监听Touch
代码如下:
public void onClick(View v) { Log.d("bitmap", "has onClick"); switch (v.getId()) { case R.id.pickImageBtn: Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, REQUEST_CODE); break;
@Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) { super.onActivityResult(requestCode, resultCode, intent); Log.d("bitmap", "requestCode is :" + requestCode); if (resultCode == RESULT_OK) { Log.d("bitmap", "has result ok"); Uri uri = intent.getData(); int dw = getWindowManager().getDefaultDisplay().getWidth(); int dh = getWindowManager().getDefaultDisplay().getHeight(); try { BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true; Bitmap chooseBitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, opts); int bw = opts.outWidth; int bh = opts.outHeight; int widthRatio = (int) Math.ceil(bw / (float) dw); int heightRatio = (int) Math.ceil(bh / (float) dh); if (widthRatio > 1 || heightRatio >1) { if (widthRatio > heightRatio) { opts.inSampleSize = widthRatio; } else { opts.inSampleSize = heightRatio; } } opts.inJustDecodeBounds = false; chooseBitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, opts); Log.d("bitmap", "chooseBitmap is :" + chooseBitmap); alteredBitmap = Bitmap.createBitmap(chooseBitmap.getWidth(), chooseBitmap.getHeight(), chooseBitmap.getConfig()); canvas = new Canvas(alteredBitmap); paint = new Paint(); paint.setColor(Color.WHITE); paint.setStyle(Style.STROKE); Matrix matrix = new Matrix(); canvas.drawBitmap(chooseBitmap, matrix , paint); mImageView.setImageBitmap(alteredBitmap); mImageView.setOnTouchListener(this); } catch (FileNotFoundException e) { e.printStackTrace(); } } }
第四部分:在图片上触摸移动,实时绘制。
代码如下:
public boolean onTouch(View v, MotionEvent event) { Log.d("touch_draw", "ontouch()"); switch (event.getAction()) { case MotionEvent.ACTION_UP: 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); mImageView.invalidate(); downX = upX; downY = upY; break; case MotionEvent.ACTION_CANCEL: break; } return true; }
第五部分:保存图片
代码如下:
public void onClick(View v) { Log.d("bitmap", "has onClick"); switch (v.getId()) { case R.id.pickImageBtn: Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, REQUEST_CODE); break; case R.id.saveBtn: if (null != alteredBitmap) { Uri imageFileUri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, new ContentValues()); try { OutputStream imageFileOS = getContentResolver().openOutputStream(imageFileUri); alteredBitmap.compress(CompressFormat.JPEG, 90, imageFileOS); Toast.makeText(this, "has saved", Toast.LENGTH_SHORT).show(); } catch (FileNotFoundException e) { e.printStackTrace(); } } break; } }