Android仿IOS AssistiveTouch(悬浮框的运用)

前言

前面开源了一篇关于“MVP+RxJava+Retrofit+okhttp实现视频+新闻客户端”的项目阐述了Android项目从零到上线的全过程,反响还不错。最近把以前做的一个仿ios AssistiveTouch(悬浮框的运用)的项目整理了一下。

项目主要功能模块

主要功能
如图所示:
1、系统设置(回到主页、一键锁屏、手电筒)
2、工具助手(自动抢红包、应用管理、小火箭垃圾清理)
3、WindowManager悬浮框

WindowManager悬浮框

本文主要介绍WindowManager悬浮框的运用。浮动窗大家应该都不陌生,360、应用宝之类的小浮动窗口运用的就是WindowManager悬浮框。

原理

1、悬浮框的显示:通过getSystemService(Context.WINDOW_SERVICE)得到WindowManager,调用WindowManager的addview(View view, WindowManager.LayoutParams mLayoutParams)进行显示

//获取WindowManager
wManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
mParams = new WindowManager.LayoutParams(); 
//适配小米、魅族等手机需要悬浮框权限的问题,可绕过授权
if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT)
{    wmParams.type = LayoutParams.TYPE_PHONE;
} else {    
wmParams.type = LayoutParams.TYPE_TOAST;
}
// 支持透明
 //mParams.format = PixelFormat.RGBA_8888; mParams.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
// 焦点 
mParams.width = 490;
//窗口的宽和高
 mParams.height = 160;
 mParams.x = 0;
//窗口位置的偏移量
 mParams.y = 0;
/** 
* 这里的flags也很关键 *代码实际是wmParams.flags |= FLAG_NOT_FOCUSABLE; 
* 40的由来是wmParams的默认属性(32)+ FLAG_NOT_FOCUSABLE(8) 
*/
mParams.flags=40;
//窗口的透明度mParams.alpha = 0.1f; 
myView = new MyView(this);
wManager.addView(myView, mParams);//添加窗口

2、悬浮框的移动:监听view的OnTouchListener通过WindowManager的updateViewLayout(Context mContext, WindowManager.LayoutParams mLayoutParams)修改mLayoutParams的mLayoutParams.x和mLayoutParams.y从而更新浮动窗的位置

private OnTouchListener mTouchListener = new OnTouchListener() {    
  float lastX, lastY;    int paramX, paramY;    
  public boolean onTouch(View v, MotionEvent event) {       
    final int action = event.getAction();        
    float x = event.getRawX();        
    float y = event.getRawY();       
    if (mTag == 0) {            
       mOldOffsetX = mViewEventMParams.x; // 偏移量            
       mOldOffsetY = mViewEventMParams.y; // 偏移量        
    }        
    switch (action) {            
       case MotionEvent.ACTION_DOWN:                
            motionActionDownEvent(x, y);                
       break;            
       case MotionEvent.ACTION_MOVE:                
            motionActionMoveEvent(x, y);                
       break;            
       case MotionEvent.ACTION_UP:                
            motionActionUpEvent(x, y);                
       break;            
       default:                
       break;        
       }        
       return true;    
  }    
  private void motionActionDownEvent(float x, float y) {        
    lastX = x;        
    lastY = y;        
    paramX = mViewEventMParams.x;        
    paramY = mViewEventMParams.y;    
  }    
  private void motionActionMoveEvent(float x, float y) {        
    int dx = (int) (x - lastX);       
    int dy = (int) (y - lastY);        
    mViewEventMParams.x = paramX + dx;       
    mViewEventMParams.y = paramY + dy;       
    mTag = 1;        
   // 更新悬浮窗位置        
  mWManager.updateViewLayout(mTouchView, mViewEventMParams);   
  }    
  private void motionActionUpEvent(float x, float y) {        
    int newOffsetX = mViewEventMParams.x;       
    int newOffsetY = mViewEventMParams.y;        
    if (mOldOffsetX == newOffsetX && mOldOffsetY == newOffsetY) {            
    updateSettingTableView();            
    mPopuWin = new PopupWindow(mSettingTable, LayoutParams.WRAP_CONTENT,        
    LayoutParams.WRAP_CONTENT);            
    mPopuWin.setTouchInterceptor(new OnTouchListener() {                
       public boolean onTouch(View v, MotionEvent event) {                    
           if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {                        
                hideSettingTable();                        
       return true;                   
          }                    
    return false;                
    }            
  });           
  mPopuWin.setBackgroundDrawable(new BitmapDrawable());            
  mPopuWin.setTouchable(true);            
  mPopuWin.setFocusable(true);            
  mPopuWin.setOutsideTouchable(true);            
  mPopuWin.setContentView(mSettingTable);            
  if (Math.abs(mOldOffsetX) > midX) {                
    if (mOldOffsetX > 0) {                    
       mOldOffsetX = midX;                
    } else {                    
       mOldOffsetX = -midX;               
    }            
  }            
  if (Math.abs(mOldOffsetY) > midY) {                
    if (mOldOffsetY > 0) {                    
       mOldOffsetY = midY;                
    } else {                    
       mOldOffsetY = -midY;                
    }            
  }            
  mPopuWin.setAnimationStyle(R.style.AnimationPreview);            
  mPopuWin.setFocusable(true);            
  mPopuWin.update();            
  mPopuWin.showAtLocation(mTouchView, Gravity.CENTER, -mOldOffsetX, -mOldOffsetY);            
  // TODO              
  mIconImageView.setBackgroundDrawable(getResources().getDrawable(R.drawable.transparent));              
  catchSettingTableDismiss();        
  } else {           
    mTag = 0;       
  }    
}};

3、悬浮框的删除:调用WindowManager的removeView(View view)

自动抢红包

原理就是根据关键字找到相应的View, 然后自动点击。主要是用到AccessibilityService这个辅助服务,基本可以满足自动抢红包的功能,但是有些逻辑需要优化,比如,拆完一个红包后,必须手动点击返回键,才能进行下一次自动抢红包。感谢CodeBoy微信抢红包外挂的作者:英文名:Leon 个人博客:http://www.happycodeboy.com Email:codeboy2013@gmail.com。github上面有详细的教程,想了解的可以自己去github。

其它

其它功能模块就不做详细介绍了,需要了解的可以去github:仿ios AssistiveTouch(悬浮框的运用),别忘了star支持下作者

本文作者:Rance935本文出处:Android仿IOS AssistiveTouch(悬浮框的运用)转载请在开头注明作者详细信息和本文出处
欢迎关注我的微信公众号和QQ群,分享Android 开发和互联网内容
Android技术分享:群号534813930
微信号:androidparks
公众号:AndroidParks

评论

  • ShuraGergo回复

    how much viagra is safe

    [url=http://viagralkas.com/]buy generic viagra[/url]

    <a href="http://viagralkas.com/">generic viagra online</a>

    viagra dosage for young men

  • ShuraGergo回复

    i wanna buy viagra tablets

    [url=http://viagralkas.com/]viagra cheap[/url]

    <a href="http://viagralkas.com/">canadian viagra</a>

    viagra dosage by weight

  • BulinokGergo回复

    usa prescription viagra no

    [url=http://viagraolx.com/]cheap viagra online[/url]

    <a href="http://viagraolx.com/">viagra cheap</a>

    viagra pour femme au maroc

  • BulinokGergo回复

    how can i buy viagra online

    [url=http://viagraolx.com/]generic viagra online[/url]

    <a href="http://viagraolx.com/">buy generic viagra</a>

    fedex generic viagra

  • music回复

    Great post. I was checking continuously this blog and I'm impressed! Extremely helpful information specially the last part 😊 I care for such information a lot. I was seeking this certain info for a long time. Thank you and good luck.

  • Kitchen Design回复

    Hello. fantastic job. I did not expect this. This is a impressive story. Thanks!

  • Garden Fence回复

    Attractive section of content. I just stumbled upon your site and in accession capital to assert that I acquire in fact enjoyed account your blog posts. Any way I’ll be subscribing to your augment and even I achievement you access consistently fast.

  • CheplanovaGergo回复

    viagra lieferung osterreich

    [url=http://viagralms.com/]generic viagra online[/url]

    <a href="http://viagralms.com/">buy viagra online</a>

    viagra ablauf patent

  • CheplanovaGergo回复

    cheapest viagra net,

    [url=http://viagralms.com/]buy generic viagra[/url]

    <a href="http://viagralms.com/">generic viagra online</a>

    36 hour viagra dosage

  • MakritskayaGergo回复

    viagra sin receta venta

    [url=http://viagraoail.com/]cheap viagra online[/url]

    <a href="http://viagraoail.com/">buy generic viagra</a>

    suppliers of viagra in uk

  • wedding photography回复

    Great ¡V I should definitely pronounce, impressed with your web site. I had no trouble navigating through all the tabs and related information ended up being truly easy to do to access. I recently found what I hoped for before you know it at all. Reasonably unusual. Is likely to appreciate it for those who add forums or anything, web site theme . a tones way for your customer to communicate. Excellent task..

  • Kitchen Flooring回复

    Wow! Thank you! I continually wanted to write on my website something like that. Can I take a portion of your post to my site?

  • Department Of Education回复

    Hello there, I found your website by way of Google whilst searching for a similar subject, your site came up, it seems to be great. I've bookmarked it in my google bookmarks.

  • VladilenGergo回复

    viagra aus china bestellen

    <a href=http://viagraomz.com/>cheap viagra</a>

    <a href="http://viagraomz.com/">generic viagra online</a>

    viagra kaufen gran canaria

  • CheplanovaGergo回复

    viagra prix redui

    http://viagralms.com/ - canadian viagra

    <a href="http://viagralms.com/">online viagra</a>

    only here getting viagra

  • MakritskayaGergo回复

    cheap sale viagra

    [url=http://viagraoail.com/]viagra cheap[/url]

    <a href="http://viagraoail.com/">cheap viagra</a>

    viagra drogueria barata

  • มาร์กี้มิราเคิล回复

    Hello! I've been reading your weblog for some time now and finally got the courage to go ahead and
    give you a shout out from Lubbock Texas! Just wanted to tell you keep up the excellent work!

  • CheplanovaGergo回复

    only here getting viagra

    http://viagralms.com/ - buy generic viagra

    <a href="http://viagralms.com/">cheap viagra</a>

    kob viagra

  • MakritskayaGergo回复

    generic mg viagra

    <a href=http://viagraoail.com/>viagra online</a>

    <a href="http://viagraoail.com/">cheap viagra</a>

    cheapest generic viagra ci

  • CheplanovaGergo回复

    viagra prix redui

    http://viagralms.com/ - online viagra

    <a href="http://viagralms.com/">buy viagra</a>

    viagra ratiopharm kaufe

  • MakritskayaGergo回复

    buy viagra saudi arabia

    <a href=http://viagraoail.com/>cheap viagra</a>

    <a href="http://viagraoail.com/">buy generic viagra</a>

    i use it viagra india pharmacy

  • CheplanovaGergo回复

    viagra sales!people group

    http://viagralms.com/ - buy viagra

    <a href="http://viagralms.com/">online viagra</a>

    buy 1 viagra.

  • MakritskayaGergo回复

    cheap viagra online.

    <a href=http://viagraoail.com/>generic viagra online</a>

    <a href="http://viagraoail.com/">cheap viagra</a>

    viagra hormone therapy

  • website laten maken回复

    I don't know whether it's just me or if perhaps everyone else encountering problems with your site.
    It appears as though some of the written text on your posts are running off the screen. Can someone
    else please provide feedback and let me know if
    this is happening to them too? This might
    be a problem with my web browser because I've had this happen before.
    Thanks

  • CheplanovaGergo回复

    viagra online kaufen zoll

    http://viagralms.com/ - viagra online

    <a href="http://viagralms.com/">viagra online</a>

    viagra in uk low price

  • MakritskayaGergo回复

    viagra tablets india

    <a href=http://viagraoail.com/>buy generic viagra</a>

    <a href="http://viagraoail.com/">online viagra</a>

    viagra in farmacia propeci

  • makii miracle回复

    What's Going down i am new to this, I stumbled upon this I've found It positively useful and
    it has helped me out loads. I'm hoping to give a contribution & aid different customers like its aided me.
    Great job.

  • MakritskayaGergo回复

    viagra 100mg pfizer billig

    <a href=http://viagraoail.com/>buy generic viagra</a>

    <a href="http://viagraoail.com/">online viagra</a>

    the best site viagra label

  • CheplanovaGergo回复

    prices of viagra in kerala

    http://viagralms.com/ - cheap viagra

    <a href="http://viagralms.com/">generic viagra online</a>

    order online viagra without rx

  • MakritskayaGergo回复

    viagra pills cost india

    <a href=http://viagraoail.com/>viagra cheap</a>

    <a href="http://viagraoail.com/">cheap viagra online</a>

    prix viagra en ligne

  • CheplanovaGergo回复

    viagra man

    http://viagralms.com/ - generic viagra online

    <a href="http://viagralms.com/">generic viagra online</a>

    viagra da prescrizione

  • MakritskayaGergo回复

    i use it viagra original use

    <a href=http://viagraoail.com/>buy viagra online</a>

    <a href="http://viagraoail.com/">cheap viagra</a>

    viagra cost target

  • CheplanovaGergo回复

    viagra soft tab sale

    http://viagralms.com/ - viagra online

    <a href="http://viagralms.com/">viagra online</a>

    viagra and alcohol

  • VladilenGergo回复

    free viagra from canada

    [url=http://viagraomz.com/]viagra cheap[/url]

    <a href="http://viagraomz.com/">viagra cheap</a>

    cheap 100mg viagra

  • Luiz Gastão Bittencourt回复

    I go to see everyday a few web sites and information sites to read content, but this website provides quality based writing.|

  • CheplanovaGergo回复

    buy viagra high street

    http://viagralms.com/ - buy viagra online

    <a href="http://viagralms.com/">buy viagra</a>

    venta viagra gran buenos aires

  • MakritskayaGergo回复

    viagra e i giovan

    <a href=http://viagraoail.com/>viagra cheap</a>

    <a href="http://viagraoail.com/">generic viagra online</a>

    rebate for viagra

  • MakritskayaGergo回复

    viagra original rezeptfre

    <a href=http://viagraoail.com/>cheap viagra</a>

    <a href="http://viagraoail.com/">buy viagra</a>

    viagra urdu

  • CheplanovaGergo回复

    cialis viagra marche pas

    http://viagralms.com/ - viagra cheap

    <a href="http://viagralms.com/">canadian viagra</a>

    one day delivery viagra

  • MakritskayaGergo回复

    beat way to order viagra

    <a href=http://viagraoail.com/>cheap viagra</a>

    <a href="http://viagraoail.com/">buy generic viagra</a>

    in mumbai where to get viagra

  • CheplanovaGergo回复

    who sells viagra in shops

    http://viagralms.com/ - buy viagra

    <a href="http://viagralms.com/">cheap viagra</a>

    safest viagra for men

  • MakritskayaGergo回复

    buy viagra stores uk

    <a href=http://viagraoail.com/>generic viagra online</a>

    <a href="http://viagraoail.com/">online viagra</a>

    rx generic viagra

  • CheplanovaGergo回复

    link for you geniune viagra

    http://viagralms.com/ - generic viagra online

    <a href="http://viagralms.com/">online viagra</a>

    very good site real viagra

  • VladilenGergo回复

    authorized viagra dealers

    <a href=http://viagraomz.com/>cheap viagra online</a>

    <a href="http://viagraomz.com/">online viagra</a>

    comprar viagra con mastercar

  • VladilenGergo回复

    viagra apotheke online

    <a href=http://viagraomz.com/>buy generic viagra</a>

    <a href="http://viagraomz.com/">buy viagra</a>

    buy generic viagra online now

  • CheplanovaGergo回复

    viagra man

    http://viagralms.com/ - cheap viagra

    <a href="http://viagralms.com/">buy viagra online</a>

    viagra kaufen ohne vorkasse,

  • MakritskayaGergo回复

    free samples viagra cialis

    <a href=http://viagraoail.com/>buy viagra online</a>

    <a href="http://viagraoail.com/">buy generic viagra</a>

    we like it order viagra pill