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

简介: 简单的手指绘图并保存所绘图片【源码】
public class SimpleFingerDraw extends Activity implements OnTouchListener,
    OnClickListener
{
  ImageView imageView;
  Button choosePicture, savePicture;
  Bitmap bitmap;
  Bitmap alteredBitmap;
  Canvas canvas;
  Paint paint;
  Matrix matrix;
  float downx = 0;
  float downy = 0;
  float upx = 0;
  float upy = 0;
  float top = 0, bottom = 0;
  String TAG = "FingerDraw";
  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    imageView = (ImageView) findViewById(R.id.ChosenImageView);
    choosePicture = (Button) findViewById(R.id.ChoosePictureButton);
    savePicture = (Button) findViewById(R.id.SavePictureButton);
    choosePicture.setOnClickListener(this);
    savePicture.setOnClickListener(this);
    savePicture.setVisibility(View.GONE);
  }
  @Override
  public boolean onTouch(View v, MotionEvent event)
  {
    // TODO Auto-generated method stub
    int action = event.getAction();
    switch (action)
    {
      case MotionEvent.ACTION_DOWN:
        downx = event.getX();// 获取的是以ImageView左上角为原点的坐标
        downy = event.getY();
        Log.e(TAG, "downx:" + downx + " downy:" + downy);
        break;
      case MotionEvent.ACTION_MOVE:
        upx = event.getX();
        upy = event.getY();
        canvas.drawLine(downx, downy, upx, upy, paint);
        imageView.invalidate();
        downx = upx;
        downy = upy;
        break;
      case MotionEvent.ACTION_UP:
        upx = event.getX();
        upy = event.getY();
        canvas.drawLine(downx, downy, upx, upy, paint);// 注意:这里是以图像的左上角为原点进行绘制直线
        imageView.invalidate();
        break;
      default:
        break;
    }
    return true;
  }
  @Override
  public void onClick(View v)
  {
    // TODO Auto-generated method stub
    if (v == choosePicture)
    {
      Intent choosePictureIntent = new Intent(Intent.ACTION_PICK,
          Media.EXTERNAL_CONTENT_URI);
      startActivityForResult(choosePictureIntent, 0);
    } else
    {
      if (alteredBitmap != null)
      {
        try
        {
          Uri ImageFileUri = getContentResolver().insert(
              Media.EXTERNAL_CONTENT_URI, new ContentValues());
          OutputStream imageFileOS = getContentResolver()
              .openOutputStream(ImageFileUri);
          alteredBitmap.compress(CompressFormat.JPEG, 100,
              imageFileOS);
          Toast.makeText(this, "Saved! ", Toast.LENGTH_LONG).show();
        } catch (FileNotFoundException e)
        {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
    }
  }
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data)
  {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK)
    {
      savePicture.setVisibility(View.VISIBLE);
      Uri imageFileUri = data.getData();
      Display currentDisplay = getWindowManager().getDefaultDisplay();
      @SuppressWarnings("deprecation")
      float dw = currentDisplay.getWidth();
      @SuppressWarnings("deprecation")
      float dh = currentDisplay.getHeight();
      BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
      bmpFactoryOptions.inJustDecodeBounds = true;
      try
      {
        bitmap = BitmapFactory
            .decodeStream(
                getContentResolver().openInputStream(
                    imageFileUri), null, bmpFactoryOptions);
      } catch (FileNotFoundException e)
      {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight / dh);
      int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth / dw);
      if (heightRatio >= 1 && widthRatio >= 1)
      {
        if (heightRatio > widthRatio)
        {
          bmpFactoryOptions.inSampleSize = heightRatio;
          // Log.e(TAG,"heightRatio");
        } else
        {
          bmpFactoryOptions.inSampleSize = widthRatio;
          // Log.e(TAG,"widthRatio");
        }
      }
      // Log.e(TAG,"heightRatio:"+heightRatio+" widthRatio:"+widthRatio);
      bmpFactoryOptions.inJustDecodeBounds = false;
      try
      {
        bitmap = BitmapFactory
            .decodeStream(
                getContentResolver().openInputStream(
                    imageFileUri), null, bmpFactoryOptions);
      } catch (FileNotFoundException e)
      {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      alteredBitmap = Bitmap.createBitmap(bitmap.getWidth(),
          bitmap.getHeight(), bitmap.getConfig());
      Log.e(TAG, "bitmap.Height:" + bitmap.getHeight());
      canvas = new Canvas(alteredBitmap);
      paint = new Paint();
      paint.setColor(Color.GREEN);
      paint.setStrokeWidth(5);
      matrix = new Matrix();
      canvas.drawBitmap(bitmap, matrix, paint);
      imageView.setImageBitmap(alteredBitmap);
      // top=imageView.getTop(); //144.0
      // bottom=imageView.getBottom();//144.0
      // height=imageView.getHeight(); //0.0
      // Log.e(TAG, "bottom:"+bottom);
      imageView.setOnTouchListener(this);
    }
  }
}
目录
相关文章
|
2天前
|
人工智能 运维 安全
|
5天前
|
SpringCloudAlibaba 负载均衡 Dubbo
微服务架构下Feign和Dubbo的性能大比拼,到底鹿死谁手?
本文对比分析了SpringCloudAlibaba框架下Feign与Dubbo的服务调用性能及差异。Feign基于HTTP协议,使用简单,适合轻量级微服务架构;Dubbo采用RPC通信,性能更优,支持丰富的服务治理功能。通过实际测试,Dubbo在调用性能、负载均衡和服务发现方面表现更出色。两者各有适用场景,可根据项目需求灵活选择。
386 124
微服务架构下Feign和Dubbo的性能大比拼,到底鹿死谁手?
|
7天前
|
人工智能 JavaScript 测试技术
Qwen3-Coder入门教程|10分钟搞定安装配置
Qwen3-Coder 挑战赛简介:无论你是编程小白还是办公达人,都能通过本教程快速上手 Qwen-Code CLI,利用 AI 轻松实现代码编写、文档处理等任务。内容涵盖 API 配置、CLI 安装及多种实用案例,助你提升效率,体验智能编码的乐趣。
713 108
|
2天前
|
算法 Python
【轴承故障诊断】一种用于轴承故障诊断的稀疏贝叶斯学习(SBL),两种群稀疏学习算法来提取故障脉冲,第一种仅利用故障脉冲的群稀疏性,第二种则利用故障脉冲的额外周期性行为(Matlab代码实现)
【轴承故障诊断】一种用于轴承故障诊断的稀疏贝叶斯学习(SBL),两种群稀疏学习算法来提取故障脉冲,第一种仅利用故障脉冲的群稀疏性,第二种则利用故障脉冲的额外周期性行为(Matlab代码实现)
228 152
|
4天前
|
Java 数据库 数据安全/隐私保护
Spring 微服务和多租户:处理多个客户端
本文介绍了如何在 Spring Boot 微服务架构中实现多租户。多租户允许单个应用实例为多个客户提供独立服务,尤其适用于 SaaS 应用。文章探讨了多租户的类型、优势与挑战,并详细说明了如何通过 Spring Boot 的灵活配置实现租户隔离、动态租户管理及数据源路由,同时确保数据安全与系统可扩展性。结合微服务的优势,开发者可以构建高效、可维护的多租户系统。
203 127
|
4天前
|
Web App开发 前端开发 API
在折叠屏应用中,如何处理不同屏幕尺寸和设备类型的样式兼容性?
在折叠屏应用中,如何处理不同屏幕尺寸和设备类型的样式兼容性?
231 124
|
2天前
|
编解码 算法 自动驾驶
【雷达通信】用于集成传感和通信的OFDM雷达传感算法(Matlab代码实现)
【雷达通信】用于集成传感和通信的OFDM雷达传感算法(Matlab代码实现)
174 125
|
2天前
|
JavaScript 关系型数据库 MySQL
基于python的网上外卖订餐系统
本系统基于Python与Flask框架,结合MySQL数据库及Vue前端技术,实现了一个功能完善的网上订餐平台。系统涵盖餐品、订单、用户及评价管理模块,并深入研究订餐系统的商业模式、用户行为与服务质量。技术上采用HTML、PyCharm开发工具,支持移动端访问,助力餐饮业数字化转型。