博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
图片涂鸦
阅读量:6689 次
发布时间:2019-06-25

本文共 3384 字,大约阅读时间需要 11 分钟。

hot3.png

public class BAGraffitiView extends View {    private int panitColor;    private Bitmap mBitmap;    private float downX, downY;    private float tempX, tempY;    private Bitmap src;    private List
drawPathList; private Paint paint; private Path path; private Bitmap resultBitmap; public BAGraffitiView(Context context) { super(context); } public BAGraffitiView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(); } public BAGraffitiView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } private void init() { drawPathList = new ArrayList<>(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (mBitmap != null) { if (src == null) { int width = canvas.getWidth(); int height = canvas.getHeight(); int bWidth = mBitmap.getWidth(); int bHeight = mBitmap.getHeight(); float wScale = (float) width/bWidth; float hScale = (float) height/bHeight; Matrix matrix = new Matrix(); matrix.postScale(wScale, hScale); src = Bitmap.createBitmap(mBitmap, 0, 0, bWidth, bHeight, matrix, true); } canvas.drawBitmap(src, 0, 0, null); } if (drawPathList != null && drawPathList.size() != 0) { for (DrawPath drawPath : drawPathList) { canvas.drawPath(drawPath.path, drawPath.paint); } } canvas.save(); } private void initValue() { paint = new Paint(); path = new Path(); paint.setAntiAlias(true); paint.setStrokeWidth(10); paint.setStyle(Paint.Style.STROKE); if (panitColor == 0) { paint.setColor(Color.RED); } else { paint.setColor(panitColor); } } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: initValue(); downX = event.getX(); downY = event.getY(); path.moveTo(downX, downY); invalidate(); tempX = downX; tempY = downY; DrawPath drawPath = new DrawPath(); drawPath.paint = paint; drawPath.path = path; drawPathList.add(drawPath); break; case MotionEvent.ACTION_MOVE: float moveX = event.getX(); float moveY = event.getY(); path.quadTo(tempX, tempY, moveX, moveY); invalidate(); tempX = moveX; tempY = moveY; break; } return true; } //设置颜色 public void setPanitColor(int panitColor) { this.panitColor = panitColor; } //设置背景图片 public void setmBitmap(Bitmap mBitmap) { this.mBitmap = mBitmap; } //撤销 public void undo() { if (drawPathList.size() > 0) { drawPathList.remove(drawPathList.size() - 1); invalidate(); } } public Bitmap getSrc() { return this.src; } class DrawPath { Paint paint; Path path; }}

 

转载于:https://my.oschina.net/u/2459282/blog/1613096

你可能感兴趣的文章
压缩[SCOI2007]
查看>>
ssh 免密码登入
查看>>
三叉搜索树
查看>>
vim右键粘贴 等杂
查看>>
学生基本系统管理
查看>>
Java反射总结
查看>>
为什么要使用SLF4J而不是Log4J
查看>>
day4 二维数组旋转90度
查看>>
简说设计模式——组合模式
查看>>
第二次实训作业
查看>>
Java 9.0.4版本 包裹类型和基本类型 ==和equals的比较
查看>>
数组多重筛选条件排序方法
查看>>
Vue中import引入模块路径时的@符号
查看>>
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
查看>>
sublime text 3插件
查看>>
Javascript优化后的加减乘除(解决js浮点数计算bug)
查看>>
js中的super小结
查看>>
ios显示或隐藏导航栏的底线
查看>>
包含 min 函数的栈
查看>>
rm -f /var/lib/rpm/__db*;rpm --rebuilddb
查看>>