涂鸦
来源:互联网 发布:手机网络管理软件 编辑:程序博客网 时间:2024/05/16 23:56
实现功能:导入背景图片,感知用户的touch操作,实现在背景图片上的涂鸦功能。将canvas分为两层,一层时背景图片,一层是用户涂鸦的图片,将两层叠加返回新的view,并存储。
public class HandWriteView extends View{ Paint paint = null; private Bitmap drawingBitmap = null; Bitmap afterDrawingBitmap = null; private Bitmap originalBitmap = null; private float clickX = 0,clickY = 0; private float startX = 0,startY = 0; private boolean isMove = true; private boolean isClear = false; public int color = Color.RED; public float strokeWidth = 6.0f; public HandWriteView(Context context, Bitmap bitmap) { super(context); drawingBitmap =bitmap; afterDrawingBitmap = Bitmap.createBitmap(bitmap); } public void clear(){ isClear = true; originalBitmap = Bitmap.createBitmap(drawingBitmap); invalidate(); } public void setstyle(float strokeWidth){ this.strokeWidth = strokeWidth; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawBitmap(HandWriting(afterDrawingBitmap), 0, 0,null); } public Bitmap HandWriting(Bitmap dwawingBitmap) { Canvas canvas = null; if(isClear){ canvas = new Canvas(originalBitmap); } else{ canvas = new Canvas(dwawingBitmap); } paint = new Paint(); paint.setStyle(Style.STROKE); paint.setAntiAlias(true); paint.setColor(color); paint.setStrokeWidth(strokeWidth); if(isMove){ canvas.drawLine(startX, startY, clickX, clickY, paint); } startX = clickX; startY = clickY; if(isClear){ return originalBitmap; } return dwawingBitmap; } @Override public boolean onTouchEvent(MotionEvent event) { clickX = event.getX(); clickY = event.getY(); if(event.getAction() == MotionEvent.ACTION_DOWN){ startX=event.getX(); startY=event.getY(); isMove = false; invalidate(); return true; } else if(event.getAction() == MotionEvent.ACTION_MOVE){ isMove = true; invalidate(); return true; } return super.onTouchEvent(event); }}
注意:在onTouchEvent中必须试试更改clickX和clickY,才能让涂鸦有更好的体验
clickX = event.getX(); clickY = event.getY();
0 0
- 涂鸦
- 涂鸦
- 涂鸦
- 涂鸦
- 涂鸦
- "涂鸦"
- 涂鸦
- 涂鸦
- 涂鸦
- 涂鸦~~~~~
- 涂鸦
- 涂鸦
- 涂鸦
- 涂鸦
- 涂鸦
- 涂鸦
- 涂鸦,又见涂鸦...
- 涂鸦心情
- Android得到本地SHA1、MD5(百度地图及Google地图)
- htmlRange的使用
- HTML学习笔记之网页元素编辑
- ubuntu下安装QQ
- 第7周项目4-队列数组
- 涂鸦
- linux wake_up
- ZMQ的初步学习
- 条理清晰的搭建SSH环境之整合Struts和Spring
- html5中sql 使用
- 共享一些毕业设计论文
- 众筹出版《介绍用Java编程》一书
- LeetCode解题报告--Swap Nodes in Pairs
- 条理清晰的搭建SSH环境之整合Hibernate和Spring