Android高仿QQ小红点功能
先给大家展示下效果图:
代码已上传至github:高仿qq小红点,如对您有帮助,欢迎star~感谢
绘制贝塞尔曲线:
主要是当在一定范围内拖拽时算出固定圆和拖拽圆的外切直线以及对应的切点,就可以通过path.quadto()来绘制二阶贝塞尔曲线了~
整体思路:
1、当小红点静止时,什么都不做,只需要给自定义小红点qqbezierview(extends textview)添加一个.9文件当背景即可
2、当滑动时,通过getrootview()获得*根view,然后new一个dragview ( extends view ) 来绘制各种状态时的小红点,并且通过getrootview().addview()的方式把dragview 加进去,这样dragview 就可以实现全屏滑动了
实现过程:
自定义qqbezierview ( extends textview ) 并复写ontouchevent来处理各种情况,代码如下:
@override public boolean ontouchevent(motionevent event) { //获得根view view rootview = getrootview(); //获得触摸位置在全屏所在位置 float mrawx = event.getrawx(); float mrawy = event.getrawy(); switch (event.getaction()) { case motionevent.action_down: //请求父view不拦截 getparent().requestdisallowintercepttouchevent(true); //获得当前view在屏幕上的位置 int[] clocation = new int[2]; getlocationonscreen(clocation); if (rootview instanceof viewgroup) { //初始化拖拽时显示的view dragview = new dragview(getcontext()); //设置固定圆的圆心坐标 dragview.setstickypoint(clocation[0] + mwidth / 2, clocation[1] + mheight / 2, mrawx, mrawy); //获得缓存的bitmap,滑动时直接通过drawbitmap绘制出来 setdrawingcacheenabled(true); bitmap bitmap = getdrawingcache(); if (bitmap != null) { dragview.setcachebitmap(bitmap); //将dragview添加到rootview中,这样就可以全屏滑动了 ((viewgroup) rootview).addview(dragview); setvisibility(invisible); } } break; case motionevent.action_move: //请求父view不拦截 getparent().requestdisallowintercepttouchevent(true); if (dragview != null) { //更新dragview的位置 dragview.setdragviewlocation(mrawx, mrawy); } break; case motionevent.action_up: getparent().requestdisallowintercepttouchevent(false); if (dragview != null) { //手抬起时来判断各种情况 dragview.setdragup(); } break; } return true; }
上面代码注释已经很详细了,总结一下就是通过内部拦截法来请求父view是否拦截分发事件,并通过event.getrawx()和event.getrawy()来不断更新dragview的位置,那么dragview都做了哪些事呢,接下来就看一下dragview,dragview是qqbezierview 的一个内部view类:
private int mstate;//当前红点的状态 private static final int state_init = 0;//默认静止状态 private static final int state_drag = 1;//拖拽状态 private static final int state_move = 2;//移动状态 private static final int state_dismiss = 3;//消失状态
首先声明了小红点的四种状态,静止状态,拖拽状态,移动状态和消失状态。
在qqbezierview 的ontouchevent的down事件中调用了setstickypoint()方法:
/** * 设置固定圆的圆心和半径 * @param stickyx 固定圆的x坐标 * @param stickyy 固定圆的y坐标 */ public void setstickypoint(float stickyx, float stickyy, float touchx, float touchy) { //分别设置固定圆和拖拽圆的坐标 stickypointf.set(stickyx, stickyy); dragpointf.set(touchx, touchy); //通过两个圆点算出圆心距,也是拖拽的距离 dragdistance = mathutil.gettwopointdistance(dragpointf, stickypointf); if (dragdistance <= maxdistance) { //如果拖拽距离小于规定最大距离,则固定的圆应该越来越小,这样看着才符合实际 stickradius = (int) (defaultradius - dragdistance / 10) < 10 ? 10 : (int) (defaultradius - dragdistance / 10); mstate = state_drag; } else { mstate = state_init; } }
接着,在qqbezierview 的ontouchevent的move事件中调用了setdragviewlocation()方法:
/** * 设置拖拽的坐标位置 * * @param touchx 拖拽时的x坐标 * @param touchy 拖拽时的y坐标 */ public void setdragviewlocation(float touchx, float touchy) { dragpointf.set(touchx, touchy); //随时更改圆心距 dragdistance = mathutil.gettwopointdistance(dragpointf, stickypointf); if (mstate == state_drag) { if (isinsiderange()) { stickradius = (int) (defaultradius - dragdistance / 10) < 10 ? 10 : (int) (defaultradius - dragdistance / 10); } else { mstate = state_move; if (ondraglistener != null) { ondraglistener.onmove(); } } } invalidate(); }
最后在qqbezierview 的ontouchevent的up事件中调用了setdragup()方法:
public void setdragup() { if (mstate == state_drag && isinsiderange()) { //拖拽状态且在范围之内 startresetanimator(); } else if (mstate == state_move) { if (isinsiderange()) { //在范围之内 需要reset startresetanimator(); } else { //在范围之外 消失动画 mstate = state_dismiss; startexplodeanim(); } } }
最后来看下dragview的ondraw方法,拖拽时的贝塞尔曲线以及拖拽滑动时的状态都是通过ondraw实现的:
@override protected void ondraw(canvas canvas) { if (isinsiderange() && mstate == state_drag) { mpaint.setcolor(color.red); //绘制固定的小圆 canvas.drawcircle(stickypointf.x, stickypointf.y, stickradius, mpaint); //首先获得两圆心之间的斜率 float link = mathutil.getlineslope(dragpointf, stickypointf); //然后通过两个圆心和半径、斜率来获得外切线的切点 pointf[] stickypoints = mathutil.getintersectionpoints(stickypointf, stickradius, link); dragradius = (int) math.min(mwidth, mheight) / 2; pointf[] dragpoints = mathutil.getintersectionpoints(dragpointf, dragradius, link); mpaint.setcolor(color.red); //二阶贝塞尔曲线的控制点取得两圆心的中点 controlpoint = mathutil.getmiddlepoint(dragpointf, stickypointf); //绘制贝塞尔曲线 mpath.reset(); mpath.moveto(stickypoints[0].x, stickypoints[0].y); mpath.quadto(controlpoint.x, controlpoint.y, dragpoints[0].x, dragpoints[0].y); mpath.lineto(dragpoints[1].x, dragpoints[1].y); mpath.quadto(controlpoint.x, controlpoint.y, stickypoints[1].x, stickypoints[1].y); mpath.lineto(stickypoints[0].x, stickypoints[0].y); canvas.drawpath(mpath, mpaint); } if (mcachebitmap != null && mstate != state_dismiss) { //绘制缓存的bitmap canvas.drawbitmap(mcachebitmap, dragpointf.x - mwidth / 2, dragpointf.y - mheight / 2, mpaint); } if (mstate == state_dismiss && explodeindex < explode_res.length) { //绘制小红点消失时的爆炸动画 canvas.drawbitmap(bitmaps[explodeindex], dragpointf.x - mwidth / 2, dragpointf.y - mheight / 2, mpaint); } }
ps:最开始使用的是 android:clipchildren=”false” 这个属性,如果父view只是一个普通的viewgroup(如linearlayout、relativelayout等),此时在父view中设置android:clipchildren=”false”后,子view就可以超出自己的范围,在viewgroup中也可以滑动了,此时也没问题;但是当是recycleview时,只要itemview设置了background属性,滑动时的dragview就会显示在background的下面了,好蛋疼~如有知其原因的还望不吝赐教~
最后再贴下源码下载地址:android高仿qq小红点
以上所述是小编给大家介绍的android高仿qq小红点功能,希望对大家有所帮助
上一篇: 联想昭阳K43的声卡芯片是什么?