因为很多代码前面五次进阶已经设计,在此不赘述。单列出核心代码。
第一部分:xml文件
一个按钮选择图片,一个按钮保存图片
代码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <Button
- android:id="@+id/pickImageBtn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="pickImage" />
- <ImageView
- android:id="@+id/pickedImage"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/ic_launcher" />
- <Button
- android:id="@+id/saveBtn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Save" />
- </LinearLayout>
第二部分:初始化
代码如下:
- 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) {
- // TODO Auto-generated method stub
- 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) {
- // TODO Auto-generated method stub
- 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;//如果设置为true,本身不会返回
- Bitmap chooseBitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, opts);
- int bw = opts.outWidth;//此时,chooseBitmap的值为null,但opts仍然获得其config
- 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) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
第四部分:在图片上触摸移动,实时绘制。
代码如下:
- public boolean onTouch(View v, MotionEvent event) {
- // TODO Auto-generated method stub
- Log.d("touch_draw", "ontouch()");
- switch (event.getAction()) {
- case MotionEvent.ACTION_UP:
- // upX = event.getX();
- // upY = event.getY();//画直线的相关代码,可自己测试。同理,画圆椭圆都可在相关方法里设置
- // canvas.drawLine(downX, downY, upX, upY, paint);
- // mImageView.invalidate();
- // break;
- 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) {
- // TODO Auto-generated method stub
- 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://保存已经涂鸦过的图片
- /*
- * PNG :非常适合艺术线条和图形:始终保持所有数据
- * JPEG:非常适合带渐变的全彩图像,例如照片。是“有损的”编×××,可设置质量
- */
- if (null != alteredBitmap) {
- Uri p_w_picpathFileUri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, new ContentValues());//创建一个新的uri
- try {
- OutputStream p_w_picpathFileOS = getContentResolver().openOutputStream(p_w_picpathFileUri);//输出流
- alteredBitmap.compress(CompressFormat.JPEG, 90, p_w_picpathFileOS);//生成图片
- Toast.makeText(this, "has saved", Toast.LENGTH_SHORT).show();
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- break;
- }
- }