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

简介: 简单的手指绘图并保存所绘图片【源码】
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);
    }
  }
}
目录
相关文章
|
3月前
|
前端开发
Canvas绘画之多边形画板,绘制多边形,携带背景图和绘画功能,带有全部清除的功能,用这个
Canvas绘画之多边形画板,绘制多边形,携带背景图和绘画功能,带有全部清除的功能,用这个
|
5月前
|
Android开发 开发者
Android开发之通过渲染纹理展示地球仪
该文阐述了如何使用OpenGL为三维物体添加纹理,以增强其真实感。纹理坐标是二维的,用于标记摊平后的“布料”对应物体的哪个部位,类似裁缝制作衣服的过程。在OpenGL中,启用纹理和深度测试是关键,还包括设置纹理参数、分配纹理编号、绑定位图材质等步骤。计算材质的纹理坐标后,通过`glDrawArrays`结合顶点和纹理坐标逐个贴图。最终示例展示了将世界地图贴到球体上形成逼真的地球仪效果。通过控制旋转、平移和缩放,能实现简单的三维动画效果。
66 2
Android开发之通过渲染纹理展示地球仪
video播放中页面上画线条-坐标点画图demo效果(整理)
video播放中页面上画线条-坐标点画图demo效果(整理)
|
JavaScript 前端开发 容器
手写图片拖拽、鼠标点位缩放
如题,无关技术背景,什么vue、react自己想用就用,这就是js加一点点css实现,意思就是可以任意迁。
187 0
EasyX添加图片+鼠标操作
EasyX添加图片+鼠标操作
191 0
|
Java
JAVA鼠标屏幕绘制拖拽删除矩形
JAVA鼠标屏幕绘制拖拽删除矩形
102 0
|
C++
duilib corner属性的贴图技巧——让图片自动贴到控件的的某一边或者一角并自适应控件的大小
转载请说明原出处,谢谢~~          Duilib给控件贴图功能可以附带多个属性,各个属性的配合可以达到许多效果。以下是duilib支持的所有贴图属性: 贴图描述:          Duilib的表现力丰富很大程度上得益于贴图描述的简单强大。
1789 0
|
算法 安全 API
《Quartz 2D编程指南》电子签名、图片处理(水印、裁剪以及屏幕截图)、常见图形的绘制(饼图、柱状图、雪花、手势密码、画板)
《Quartz 2D编程指南》电子签名、图片处理(水印、裁剪以及屏幕截图)、常见图形的绘制(饼图、柱状图、雪花、手势密码、画板)
209 0
《Quartz 2D编程指南》电子签名、图片处理(水印、裁剪以及屏幕截图)、常见图形的绘制(饼图、柱状图、雪花、手势密码、画板)
推荐一个免费的屏幕取色器,鼠标放到的位置自动显示RGB
推荐一个免费的屏幕取色器,鼠标放到的位置自动显示RGB
356 0
推荐一个免费的屏幕取色器,鼠标放到的位置自动显示RGB
|
前端开发 C++
QML学习笔记(四)-Canva画板画图功能-跟随鼠标位置进行随笔画
参考博主文章进行整理了代码,实现功能参考:https://blog.csdn.net/UbuntuTouch/article/details/46375697 源码:https://github.com/sueRimn/QML-ExampleDemos 更多的鼠标实时画图-画直线画圆画矩形等看下...
2456 0