使用android怎么在照片中绘制涂鸦

发布时间:2021-03-10 16:40:59 作者:Leah
来源:亿速云 阅读:280

这篇文章给大家介绍使用android怎么在照片中绘制涂鸦,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

把ImageView设置为全屏,布局文件代码如下

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_margin="5dp"
 android:orientation="vertical">

 <ImageView
  android:id="@+id/iv_draw_pic"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_gravity="center_horizontal"
  android:layout_margin="10dp" />

 <RelativeLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content">

  <Button
   android:id="@+id/btn_choose"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_margin="5dp"
   android:layout_weight="1"
   android:text="选择照片" />

  <Button
   android:id="@+id/btn_save"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignParentRight="true"
   android:layout_margin="5dp"
   android:layout_weight="1"
   android:text="保存照片" />

  <Button
   android:id="@+id/btn_clear"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignParentBottom="true"
   android:layout_centerHorizontal="true"
   android:layout_margin="5dp"
   android:layout_weight="1"
   android:text="擦除笔迹" />
 </RelativeLayout>

</FrameLayout>

根据机型设置缩放比例

  switch (model){
   case "MI 4LTE":
    scale = 1.1f;
    break;
   case "HUAWEI NXT-AL10":
    scale = 1.5f;
    break;
  }

效果图如下

使用android怎么在照片中绘制涂鸦

二、绘制涂鸦

实现代码如下:

 @Override
 public boolean onTouch(View view, MotionEvent motionEvent) {
  int action = motionEvent.getAction();
  switch (action) {
   case MotionEvent.ACTION_CANCEL:
    break;
   case MotionEvent.ACTION_DOWN:
    downX = motionEvent.getX() * scale;
    downY = motionEvent.getY() * scale;
    break;
   case MotionEvent.ACTION_UP:
    upX = motionEvent.getX() * scale;
    upY = motionEvent.getY() * scale;
    canvas.drawLine(downX, downY, upX, upY, paint);
    iv_drawpicture.invalidate();
    break;
   case MotionEvent.ACTION_MOVE:
    upX = motionEvent.getX() * scale;
    upY = motionEvent.getY() * scale;
    canvas.drawLine(downX, downY, upX, upY, paint);
    iv_drawpicture.invalidate();
    downX = upX;
    downY = upY;
    break;
   default:
    break;
  }
  return true;
 }

三、保存绘制涂鸦后的图片

实现代码如下:

try {

  Uri imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new ContentValues());
  OutputStream os = getContentResolver().openOutputStream(imageUri);

  //compress方法将图片转换成JPG或者PNG格式
  drawBitmap.compress(Bitmap.CompressFormat.JPEG, 90, os);
  Toast.makeText(this, "Saved:" + imageUri.toString(), Toast.LENGTH_LONG).show();
 } catch (FileNotFoundException e) {
  e.printStackTrace();
 }

四、擦除涂鸦笔迹

实现代码如下:

 drawBitmap = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());
 canvas = createCanvas(drawBitmap);
 paint = createPaint();
 canvas.drawBitmap(bmp, 0, 0, paint);
 iv_drawpicture.setImageBitmap(drawBitmap);
 iv_drawpicture.setOnTouchListener(this);

关于使用android怎么在照片中绘制涂鸦就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. PyQt5 在label显示的图片中绘制矩形的方法
  2. 使用Python怎么在图片中插入大量文字

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android

上一篇:什么是Qt QGraphics体系及刷新机制

下一篇:django 如何实现插入视频的简单方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》