图片上涂鸦

在android设备上对照片进行涂鸦,要解决的不同手机屏幕大小不一致的问题,怎样做才能去适应不同的手机屏幕,这里,可以用到一个相似的数学知识。
就是,获取手机屏幕的像素值,再得到图片大小的像素值,计算出两个值的比值,然后监听触摸事件,监听触摸事件得到的数据,处理一下,就可对应到图片具体的像素点。看一下效果。好了,看一下代码
public class ScrawlActivity extends Activity {ImageView imageView;double pictureRelativeLeft, pictureRelativeTop, pictureRelativeRight, pictureRelativeButtom;double imageViewLeft, imageViewTop, imageViewRight, imageViewButtom; double pictureRealLeft, pictureRealTop, pictureRealRight, pictureRealButtom;Bitmap bitmap;double proportionWidth, proportionHeight; double bitmapWidth, bitmapHeight; Canvas canvas;Path path;double preX, preY;Paint paint;boolean hasOut=false;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);InitToolBar.initToolBar(this);setContentView(R.layout.activity_scrawl);setActionBar();imageView = (ImageView) findViewById(R.id.scrawlImageView);BitmapFactory.Options bfoOptions = new BitmapFactory.Options();bfoOptions.inScaled = false;Intent intent=getIntent();bitmap=((APP)getApplication()).bitmap.copy(Bitmap.Config.ARGB_8888, true);imageView.setImageBitmap(bitmap);bitmapWidth = bitmap.getWidth();bitmapHeight = bitmap.getHeight(); canvas = new Canvas();System.out.println(bitmap);canvas.setBitmap(bitmap);setPiont();path = new Path();System.out.println("bitmap:   " + bitmapWidth + "     " + bitmapHeight);}void setActionBar(){ActionBar actionBar=getActionBar();actionBar.setTitle(" ");actionBar.setDisplayShowHomeEnabled(false);actionBar.setDisplayHomeAsUpEnabled(false);actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.actionbar));}@Overridepublic void onWindowFocusChanged(boolean hasFocus) {if (hasFocus == true) {Matrix matrix = imageView.getImageMatrix();Rect rect = imageView.getDrawable().getBounds();float[] values = new float[9];matrix.getValues(values);pictureRelativeLeft = values[2];pictureRelativeTop = values[5];pictureRelativeRight = pictureRelativeLeft + rect.width()* values[0];pictureRelativeButtom = pictureRelativeTop + rect.height()* values[0];int[] location = new int[2]; // 获取组件在手机屏幕中的绝对像素位置imageView.getLocationOnScreen(location);imageViewLeft = location[0];imageViewTop = location[1];System.out.println("imageView:" + imageViewLeft + "     "+ imageViewTop);imageViewRight = imageView.getRight();imageViewButtom = imageView.getBottom();setPictureRealPosition();proportionWidth = bitmapWidth/ (pictureRealRight - pictureRealLeft);proportionHeight = bitmapHeight/ (pictureRealButtom - pictureRealTop);}}void setPiont() {// 设置画笔的颜色paint = new Paint(Paint.DITHER_FLAG);paint.setColor(Color.RED);// 设置画笔风格paint.setStyle(Paint.Style.STROKE);paint.setStrokeWidth(2);// 反锯齿paint.setAntiAlias(true);paint.setDither(true);}@Overridepublic boolean onTouchEvent(MotionEvent event) {double x = event.getX();double y = event.getY();if (x >= pictureRealLeft && x <= pictureRealRight&& y >= pictureRealTop && y <= pictureRealButtom) {switch (event.getAction()) {case MotionEvent.ACTION_DOWN:x = (x - pictureRealLeft) * proportionWidth;y = (y - pictureRealTop) * proportionHeight;path.moveTo((float) x, (float) y);preX = x;preY = y;break;case MotionEvent.ACTION_MOVE:System.out.println(x + "     " + y);x = (x - pictureRealLeft) * proportionWidth;y = (y - pictureRealTop) * proportionHeight;if(hasOut==true){path.reset();path.moveTo((float) x, (float) y);preX=x;preY=y;System.out.println("reset");hasOut=false;}path.quadTo((float) preX, (float) preY, (float) x, (float) y);preX = x;preY = y;break;case MotionEvent.ACTION_UP:System.out.println(x + "     " + y);x = (x - pictureRealLeft) * proportionWidth;y = (y - pictureRealTop) * proportionHeight;canvas.drawPath(path, paint); // ①path.reset();break;}} else {path.reset();hasOut=true;//System.out.println("reset");}// 将cacheBitmap绘制到该View组件上//canvas.drawBitmap(bitmap, 0, 0, paint); // ②// 沿着path绘制canvas.drawPath(path, paint);imageView.setImageBitmap(bitmap);// invalidate();return false;}void setPictureRealPosition() {pictureRealLeft = imageViewLeft + pictureRelativeLeft;pictureRealTop = imageViewTop + pictureRelativeTop;pictureRealRight = imageViewLeft + pictureRelativeRight;pictureRealButtom = imageViewTop + pictureRelativeButtom;}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.scrawl, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {int id = item.getItemId();if (id == R.id.scrawlSure) {Intent intent =new Intent();intent.putExtra("result", true);setResult(1, intent);((APP)getApplication()).bitmap=bitmap;finish();return true;}if(id==R.id.scrawlCancel){setResult(2);finish();return true;}if(id==android.R.id.home){setResult(2);finish();return true;}return false;}@Overrideprotected void onDestroy() {super.onDestroy();setResult(2);}
}    

android 实现在照片上涂鸦相关推荐

  1. android绘制view的撤销,DrawingView android 上的一个涂鸦控件。可以设置画笔的粗细,颜色,撤销上一笔涂鸦,提供保存图片的接口。 @codeKK Android开源站...

    DrawingView 的原型来自DrawingView-Android,是 android 的一个可涂鸦控件. 之所以做这个控件是因为前段时间写了一个截图应用需要用到涂鸦功能,现在把涂鸦的控件单独拿 ...

  2. android涂鸦板保存功能,Android 使用Path实现涂鸦功能

    今天实现一个涂鸦效果,会分几步实现,这里有一个重要的知识点就是图层,要理解这个,不然你看这篇博客,很迷茫,迷茫的苍茫的天涯是我的爱,先从简单的需求做起,绘制一条线,代码如下: package com. ...

  3. android照相及照片上传

    android照相及照片上传 Java代码   package com.android.cist.camera.view; import java.io.ByteArrayOutputStream; ...

  4. iPhone 12怎么在照片上涂鸦

    大多iPhone用户都会使用照片编辑功能,对拍摄的照片进行剪裁.调节亮度对比度或添加滤镜.其实,在照片编辑工具中,还有一项叫做"标记"的隐藏功能,利用这个工具,可以发挥创造力,在照 ...

  5. 如何在照片上涂鸦写字?分享几种照片涂鸦方法

    在照片上添加涂鸦可以带来许多有趣的效果,可以使照片更有趣味性和个性化,特别是对于社交媒体上的照片而言.例如,在自拍照上添加一些搞笑的涂鸦,可以让照片更加生动有趣,吸引更多关注和点赞.还可以用来修复照片 ...

  6. android:在照片上绘制涂鸦

    这个应该是简易版的美图秀秀(小伙伴们吐槽:你这也叫简易版的??我们看着怎么不像啊--).好吧,只是在图片上绘制涂鸦,然后保存. 一.选择图片 这个道长有必要说一下,在绘制涂鸦时,笔画会根据设置Imag ...

  7. android实现在照片上绘制涂鸦的方法,android涂鸦程序(在图像上绘制)

    界面首先由一个choosePicture的按钮,用来从MediaStore取得图片,取得之后,可以在图片上进行绘画,再点击savebutton就可以保存图片到MediaStore. 界面xml文件: ...

  8. android 使用Path实现涂鸦效果

    今天周五了,感觉时间过的好快,这周真是过的太闲了,只好继续我的博客之旅了,今天实现一个涂鸦效果,会分几步实现,这里有一个重要的知识点就是图层,要理解这个,不然你看这篇博客,很迷茫,迷茫的苍茫的天涯是我 ...

  9. Android实现手写板和涂鸦功能

    下面仿一个Android手写板和涂鸦的功能,直接上代码: write_pad.xml <LinearLayout xmlns:android="http://schemas.andro ...

最新文章

  1. Machine Learning week 5 quiz: Neural Networks: Learning
  2. 人人都是 DBA(XII)查询信息收集脚本汇编
  3. mapreduce,map后面跟map是什么操作???
  4. 【渝粤题库】广东开放大学 统计基础 形成性考核
  5. java学习(56):接口之间的继承续
  6. Qt的Xml操作QDomDocument
  7. 织梦dede模板|HTML5建材陶瓷装修设计网站织梦dede模板源码[自适应手机版]
  8. mysql复制学习二 安装及首次复制配置
  9. 23. 二叉搜索树的后序遍历序列
  10. Linux查看进程下的线程
  11. RocketMQ源码学习(六)-Name Server
  12. centos7 mysql 数据库备份与还原
  13. HDU-1013-Digital root
  14. 带鉴权信息的SIP呼叫
  15. Android微信emoji表情,Android 软键盘和emoji表情切换方案,和微信几乎一样的体验...
  16. Matlab中图像平移的实现
  17. ANSYS Electronics Desktop 19.2电机仿真思路
  18. Win11玩游戏延迟高的解决办法
  19. C++手敲Roberts_Prewitt_Sobel实现阈值分割
  20. opencv亚像素边缘精度_opencv 亚像素 算法

热门文章

  1. 【绝对干货】2021年哔哩哔哩Java高级面试题及答案,完整版开放下载
  2. 片状卷积的verilog实现
  3. 数据结构课程设计:室内布线
  4. 陈永杰:中国到底有多少穷人?
  5. 7-4 竞赛排名 (10 分)失败
  6. oracle用命令查表结构,通过sql命令查看表结构
  7. 音乐可视化第一次演讲PPT
  8. 视觉心理学-如何排版
  9. ceph-ansible部署Ceph Pacific版本集群
  10. Unity—UGUI游戏摇杆的制作