简单的手指绘图并保存所绘图片【源码】

举报
ShaderJoy 发表于 2021/12/29 23:23:07 2021/12/29
【摘要】 public class SimpleFingerDraw extends Activity implements OnTouchListener, OnClickListener{ ImageView imageView; Button choosePicture, savePicture; Bitmap bitmap; Bitma...

  
  1. public class SimpleFingerDraw extends Activity implements OnTouchListener,
  2. OnClickListener
  3. {
  4. ImageView imageView;
  5. Button choosePicture, savePicture;
  6. Bitmap bitmap;
  7. Bitmap alteredBitmap;
  8. Canvas canvas;
  9. Paint paint;
  10. Matrix matrix;
  11. float downx = 0;
  12. float downy = 0;
  13. float upx = 0;
  14. float upy = 0;
  15. float top = 0, bottom = 0;
  16. String TAG = "FingerDraw";
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState)
  19. {
  20. // TODO Auto-generated method stub
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_main);
  23. imageView = (ImageView) findViewById(R.id.ChosenImageView);
  24. choosePicture = (Button) findViewById(R.id.ChoosePictureButton);
  25. savePicture = (Button) findViewById(R.id.SavePictureButton);
  26. choosePicture.setOnClickListener(this);
  27. savePicture.setOnClickListener(this);
  28. savePicture.setVisibility(View.GONE);
  29. }
  30. @Override
  31. public boolean onTouch(View v, MotionEvent event)
  32. {
  33. // TODO Auto-generated method stub
  34. int action = event.getAction();
  35. switch (action)
  36. {
  37. case MotionEvent.ACTION_DOWN:
  38. downx = event.getX();// 获取的是以ImageView左上角为原点的坐标
  39. downy = event.getY();
  40. Log.e(TAG, "downx:" + downx + " downy:" + downy);
  41. break;
  42. case MotionEvent.ACTION_MOVE:
  43. upx = event.getX();
  44. upy = event.getY();
  45. canvas.drawLine(downx, downy, upx, upy, paint);
  46. imageView.invalidate();
  47. downx = upx;
  48. downy = upy;
  49. break;
  50. case MotionEvent.ACTION_UP:
  51. upx = event.getX();
  52. upy = event.getY();
  53. canvas.drawLine(downx, downy, upx, upy, paint);// 注意:这里是以图像的左上角为原点进行绘制直线
  54. imageView.invalidate();
  55. break;
  56. default:
  57. break;
  58. }
  59. return true;
  60. }
  61. @Override
  62. public void onClick(View v)
  63. {
  64. // TODO Auto-generated method stub
  65. if (v == choosePicture)
  66. {
  67. Intent choosePictureIntent = new Intent(Intent.ACTION_PICK,
  68. Media.EXTERNAL_CONTENT_URI);
  69. startActivityForResult(choosePictureIntent, 0);
  70. } else
  71. {
  72. if (alteredBitmap != null)
  73. {
  74. try
  75. {
  76. Uri ImageFileUri = getContentResolver().insert(
  77. Media.EXTERNAL_CONTENT_URI, new ContentValues());
  78. OutputStream imageFileOS = getContentResolver()
  79. .openOutputStream(ImageFileUri);
  80. alteredBitmap.compress(CompressFormat.JPEG, 100,
  81. imageFileOS);
  82. Toast.makeText(this, "Saved! ", Toast.LENGTH_LONG).show();
  83. } catch (FileNotFoundException e)
  84. {
  85. // TODO Auto-generated catch block
  86. e.printStackTrace();
  87. }
  88. }
  89. }
  90. }
  91. @Override
  92. protected void onActivityResult(int requestCode, int resultCode, Intent data)
  93. {
  94. // TODO Auto-generated method stub
  95. super.onActivityResult(requestCode, resultCode, data);
  96. if (resultCode == RESULT_OK)
  97. {
  98. savePicture.setVisibility(View.VISIBLE);
  99. Uri imageFileUri = data.getData();
  100. Display currentDisplay = getWindowManager().getDefaultDisplay();
  101. @SuppressWarnings("deprecation")
  102. float dw = currentDisplay.getWidth();
  103. @SuppressWarnings("deprecation")
  104. float dh = currentDisplay.getHeight();
  105. BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
  106. bmpFactoryOptions.inJustDecodeBounds = true;
  107. try
  108. {
  109. bitmap = BitmapFactory
  110. .decodeStream(
  111. getContentResolver().openInputStream(
  112. imageFileUri), null, bmpFactoryOptions);
  113. } catch (FileNotFoundException e)
  114. {
  115. // TODO Auto-generated catch block
  116. e.printStackTrace();
  117. }
  118. int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight / dh);
  119. int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth / dw);
  120. if (heightRatio >= 1 && widthRatio >= 1)
  121. {
  122. if (heightRatio > widthRatio)
  123. {
  124. bmpFactoryOptions.inSampleSize = heightRatio;
  125. // Log.e(TAG,"heightRatio");
  126. } else
  127. {
  128. bmpFactoryOptions.inSampleSize = widthRatio;
  129. // Log.e(TAG,"widthRatio");
  130. }
  131. }
  132. // Log.e(TAG,"heightRatio:"+heightRatio+" widthRatio:"+widthRatio);
  133. bmpFactoryOptions.inJustDecodeBounds = false;
  134. try
  135. {
  136. bitmap = BitmapFactory
  137. .decodeStream(
  138. getContentResolver().openInputStream(
  139. imageFileUri), null, bmpFactoryOptions);
  140. } catch (FileNotFoundException e)
  141. {
  142. // TODO Auto-generated catch block
  143. e.printStackTrace();
  144. }
  145. alteredBitmap = Bitmap.createBitmap(bitmap.getWidth(),
  146. bitmap.getHeight(), bitmap.getConfig());
  147. Log.e(TAG, "bitmap.Height:" + bitmap.getHeight());
  148. canvas = new Canvas(alteredBitmap);
  149. paint = new Paint();
  150. paint.setColor(Color.GREEN);
  151. paint.setStrokeWidth(5);
  152. matrix = new Matrix();
  153. canvas.drawBitmap(bitmap, matrix, paint);
  154. imageView.setImageBitmap(alteredBitmap);
  155. // top=imageView.getTop(); //144.0
  156. // bottom=imageView.getBottom();//144.0
  157. // height=imageView.getHeight(); //0.0
  158. // Log.e(TAG, "bottom:"+bottom);
  159. imageView.setOnTouchListener(this);
  160. }
  161. }
  162. }

文章来源: panda1234lee.blog.csdn.net,作者:panda1234lee,版权归原作者所有,如需转载,请联系作者。

原文链接:panda1234lee.blog.csdn.net/article/details/8727275

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。