这篇文章给大家介绍使用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;
}
效果图如下
二、绘制涂鸦
实现代码如下:
@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怎么在照片中绘制涂鸦就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。