本文实例为大家分享了Android自定义橡皮擦效果,使用贝塞尔曲线处理曲线转折处 public class picFingerToTest extends View { private Paint paint; private Bitmap decodeResourceSRC; private Bitmap createBitmapDST; //
本文实例为大家分享了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 不消耗 } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。