Android自定义橡皮擦效果
程序员文章站
2022-08-20 17:10:48
本文实例为大家分享了android自定义橡皮擦效果,使用贝塞尔曲线处理曲线转折处
public class picfingertotest extends vie...
本文实例为大家分享了android自定义橡皮擦效果,使用贝塞尔曲线处理曲线转折处
public class picfingertotest extends view { private paint paint; private bitmap decoderesourcesrc; private bitmap createbitmapdst; // 手指路径,使用贝塞尔路线 private path path; private float perx; private float pery; public picfingertotest(context context, attributeset attrs) { super(context, attrs); // 1、设置禁用硬件设置 setlayertype(view.layer_type_software, null); // 2、设置手指画笔 paint = new paint(); paint.setantialias(true); paint.setcolor(color.red); paint.setstyle(paint.style.stroke); paint.setstrokewidth(45); // 3、生成图像手指源目标 // 源 decoderesourcesrc = bitmapfactory.decoderesource(getresources(), r.drawable.welcome, null); // 目标 createbitmapdst = bitmap.createbitmap(decoderesourcesrc.getwidth(), decoderesourcesrc.getheight(), config.argb_8888); path = new path(); } @override protected void ondraw(canvas canvas) { super.ondraw(canvas); // 分层绘制 int savelayer = canvas.savelayer(0, 0, getwidth(), getheight(), null,canvas.all_save_flag); // 把手指轨迹划到目标路径上 canvas canvas2 = new canvas(createbitmapdst); canvas2.drawpath(path, paint); // 把目标图像画到画布上 canvas.drawbitmap(createbitmapdst, 0, 0, paint); // 计算源图像区域 paint.setxfermode(new porterduffxfermode(mode.src_out)); canvas.drawbitmap(decoderesourcesrc, 0, 0, paint); paint.setxfermode(null); canvas.restoretocount(savelayer); } //使用贝塞尔曲线,使折线过度圆滑 @override public boolean ontouchevent(motionevent event) { switch (event.getaction()) { // 记录手指触摸的初始化位置 case motionevent.action_down: path.moveto(event.getx(), event.gety()); perx = event.getx(); pery = event.gety(); return true; case motionevent.action_move: float endx = (perx + event.getx()) / 2; float endy = (pery + event.gety()) / 2; path.quadto(perx, pery, endx, endy); perx = event.getx(); pery = event.gety(); postinvalidate(); break; case motionevent.action_up: break; default: break; } return super.ontouchevent(event); } }
小编再为大家补充一段代码:android橡皮擦擦图片功能
public void oncreate() { //底边图片 imageview ivtop = (imageview) findviewbyid(r.id.iv_top); options opts = new options(); //图片加载器,用于配置一些缩放比例,和像素单位 opts.insamplesize = 2; //制定加载器把原图片的宽高缩放到2/1的效果加载 //获得外层图片,decoderesource方法默认获得的像素单位是rgb(red,green,blue),argb(alpha,red,green,blue) bitmap topimage = bitmapfactory.decoderesources( getresource(),r.drawable.top, opts); //创建一张空白图片,并且把图片想读单位指定为:argb bitmap blank = bitmap.createbitmap(topimage.getwidth(), topimage.getheight, config.argb_4444); //把上边的topimage画到空白图片上 canvas canvas = new canvas(blank); //把topimage画到空白图片上但是像素单位变成argb() canvas.drawbitmap(topimage, 0, 0, null); ivtop.setimagebitmap(blank); } class myontouchlistoner implements ontouchlistener { @override pulic boolean ontouch(view v, motionevent event) { //是否是移动的事件 if (event.getaction() == motionevent.action_move) { //获得按下坐标 int x = (int) event.getx(); int y = (int) event.gety(); for (int i = x - 10; i < x + 10; i++) { for (int j = y - 10; j < y + 10; j++) { //防止超出边界 if (j >= 0 && blank.getheight() && i >= 0 && i < blank.getwidth()) { blank.setpixel(i, j, color.transparent); } } } //修改后的图片设置给imageview ivtop.setimagebitmap(blank); } return true; //true 消耗掉这次触摸事件.false 不消耗 } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。