android制作轨迹图,Android绘制进阶之六:触摸轨迹的绘制及图片的保存

因为很多代码前面五次进阶已经设计,在此不赘述。单列出核心代码。

第一部分: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;         }     }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值