第一次写还是有点不知所措,就将就着看吧您内

话说APP,绝大多数会有新手引导层,需要遮罩的那种,类似这样的:

f3f44da13de7?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

很久很久之前的一个项目,在github上找了一个,用着不是很舒服,比较繁琐,而且基本不能定制,很蛋疼,于是乎(- - || 我去,怎么排版啊)。。

f3f44da13de7?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

好吧,那就这样吧,第一步,一般引导层在某个界面,也就是传说中的Activity,对吧。那么这个引导浮层就可以放在activity的DecorView里。刚好DecorView是个FrameLayout,又刚好我们的浮层就是要在最上层。

FrameLayout decorView = (FrameLayout)activity.getWindow().getDecorView();

对啊,就是这样啊,只要decorView.addView()添加引导层和decorView.removeView()移除引导层就好啦,对吧。

辣么重点来了,引导层咋整?不要方,我们一步步来。又第一步,通过onDraw()引导层挖洞。怎么挖?戳这里(假装有手指)Android中Xfermode简单用法。呐,知道挖洞就好办多了。那么问题又来了,怎么准确的在控件上方挖适当大小的洞呢?

f3f44da13de7?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

好,又又第一步,先拿到这个控件的位置信息

int[] locations =new int[2];

targetView.getLocationOnScreen(locations);

int width = targetView.getMeasuredWidth();

int height = viewParams.targetView.getMeasuredHeight();

// 拿到控件在屏幕上的位置,上面(又假装有手指)一二行,得到控件位置的矩阵Rect(left,top,right,bottom)

Rect rect=new Rect(locations[0],locations[1],locations[0] + width,locations[1] + height);

别怪我没告诉你啊,这个rect就是我们挖洞的位置啦,对吧。

int canvasWidth = canvas.getWidth();

int canvasHeight = canvas.getHeight();

//绘制背景

int layerId = canvas.saveLayer(0,0,canvasWidth,canvasHeight, null,Canvas.ALL_SAVE_FLAG);

paint.setColor(params.backgroundColor);

canvas.drawRect(0,0,canvasWidth,canvasHeight,paint);

//挖洞

paint.setXfermode(newPorterDuffXfermode(PorterDuff.Mode.CLEAR));

canvas.drawRect(rect,paint);

paint.setXfermode(null);

当然啦,你要挖圆形或者椭圆的洞这个Rect可不行哦,因为你挖出来的是Rect的内切圆或椭圆,控件还有一部分没包进去呢,什么?要示意图,呐(自己脑补)。

挖圆形洞,无非就是勾股定理,rect拿到圆心,算出半径,不记得的话赶紧跑,我帮你压住数学老师的棺材板

//挖洞

paint.setXfermode(newPorterDuffXfermode(PorterDuff.Mode.CLEAR));

//绘制圆形,算出外接圆的半径

canvas.drawCircle((rect.left+ rect.right) /2

,(rect.top+ rect.bottom) /2

,(int) Math.sqrt((rect.right- rect.left) * (rect.right- rect.left) + (rect.bottom- rect.top) * (rect.bottom- rect.top)) /2

,paint);

paint.setXfermode(null);

挖椭圆,懒得算了,叫一个同事算的,长短轴据说就是根号2倍的宽高,反正我不信,我还是用了,反正错了赖他(又又假装有手指)

f3f44da13de7?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

//挖洞

paint.setXfermode(newPorterDuffXfermode(PorterDuff.Mode.CLEAR));

//算出targetView外接椭圆的短轴和长轴

int len1 = (int) (((rect.right- rect.left) * Math.sqrt(2) - (rect.right- rect.left)) /2);

int len2 = (int) (((rect.bottom- rect.top) * Math.sqrt(2) - (rect.bottom- rect.top)) /2);

canvas.drawOval(newRectF(rect.left- len1

,rect.top- len2

,rect.right+ len1

,rect.bottom+ len2),paint);

paint.setXfermode(null);

你们要的效果

f3f44da13de7?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

screenshot1.jpg

f3f44da13de7?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

screenshot2.png

好啦,洞挖好啦,还有最最最最重要的一步就是加引导图啦,对吧。要不然只有个洞,有个屏蔽用。

//绘制引导图片,虽然会根据targetView位置自动调整

//如果targetView左边空间较大引导图绘制在targetView左边,否则相反

//如果targetView上边空间较大引导图绘制在targetView上边,否则反之

//提供了offX和offY供用户调整引导图位置

Bitmap bitmap = BitmapFactory.decodeResource(getResources(),guideRes);

if(bitmap ==null)

return;

int len1 =0;

int len2 =0;

if(state== Guide.State.OVAL){

len1 = (int) (((rect.right- rect.left) * Math.sqrt(2) - (rect.right- rect.left)) /2);

len2 = (int) (((rect.bottom- rect.top) * Math.sqrt(2) - (rect.bottom- rect.top)) /2);

}else if(state== Guide.State.CIRCLE){

int radiu = (int) Math.sqrt((rect.right- rect.left) * (rect.right- rect.left) + (rect.bottom- rect.top) * (rect.bottom- rect.top));

len1 = (radiu - (rect.right- rect.left)) /2;

len2 = (radiu - (rect.bottom- rect.top)) /2;

}

int x = rect.left> canvas.getWidth() - rect.right?

rect.left- len1 - bitmap.getWidth() + (rect.right- rect.left) /2+ offX:

rect.right+ len2 - (rect.right- rect.left) /2+ offX;

int y = rect.top> canvas.getHeight() - rect.bottom?

rect.top- len2 - bitmap.getHeight() + offY:

rect.bottom+ len2 + offY;

canvas.drawBitmap(bitmap,x,y,paint);

//记得释放,不然。。(你来配图)

bitmap.recycle();

bitmap =null;

诶,别走听我说。咳咳,有几个点需要解释一下,canvas.drawBitmap(bitmap,x,y,paint);这之前都是大概计算一下图片的位置,规则看注释。还有,记得bitmap.recycle();bitmap =null;还有还有,offX,offY说一下,无非就是写的不准满足不了你们(老脸一红),给你们提供自己调整引导图位置的机会,不用谢。

f3f44da13de7?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

那就这样了,好吧,再见。

大佬别走,戳一下这里guide Android 最简单最好用的遮罩引导库,有代码,有(pian)福(ni)利(de),点个星星。

f3f44da13de7?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

算了,你们还是批评我把。

f3f44da13de7?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐