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

评论

  • M88回复

    hi!,I love your writing so a lot! share we be in contact more approximately your post on AOL? I need a specialist in this area to unravel my problem. May be that is you! Having a look forward to see you.

  • GoloaGergo回复

    cialis en promocion

    <a href=http://canadianfdcialis.com/>cialis canada</a>

    <a href="http://canadianfdcialis.com/">cialis online</a>

    try it original cialis

  • Www.linux.net回复

    Well, Im stunneԁ you alⅼ lert the very additional fineѕt one for
    me.? Daddy mentioned with а teasing smile. ?It?s that һe sent Jesus to diee for
    uѕ and provides uѕ life foreveг and ever and that because of that weⅼl be a household
    in heaven for millions of years. That?s pretty good isnt it?

  • universalsitesupplies.co.uk回复

    I¡¦ll right away grab your rss feed as I can not in finding your email subscription link or e-newsletter service. Do you've any? Kindly let me recognise in order that I may subscribe. Thanks.

  • universalsitesupplies.co.uk回复

    Hi there, I found your site by means of Google even as looking for a related topic, your site got here up, it seems good. I've bookmarked it in my google bookmarks.

  • AlenaGergo回复

    venta de cialis barcelona

    http://buycialiskj.com/ - cialis online

    <a href="http://buycialiskj.com/">cialis coupon</a>

    buying cialis in mexico,

  • universalsitesupplies.co.uk回复

    What i don't realize is actually how you are not really much more well-liked than you may be right now. You're so intelligent. You realize therefore significantly relating to this subject, made me personally consider it from numerous varied angles. Its like men and women aren't fascinated unless it’s one thing to do with Lady gaga! Your own stuffs excellent. Always maintain it up!

  • universalsitesupplies.co.uk回复

    Hello, i feel that i noticed you visited my weblog so i came to “return the desire”.I'm trying to in finding issues to enhance my site!I assume its good enough to make use of some of your ideas!!

  • longchamp回复

    Oh my goodness! an incredible article dude. Thank you However I am experiencing situation with ur rss . Don抰 know why Unable to subscribe to it. Is there anyone getting similar rss drawback? Anybody who knows kindly respond. Thnkx

  • Viagra回复

    Fantastic goods from you, man. I have understand your stuff previous to and you're just too wonderful. I really like what you've acquired here, really like what you are stating and the way in which you say it. You make it enjoyable and you still take care of to keep it smart. I can't wait to read much more from you. This is actually a great web site.|

  • Dog Walker回复

    Good website! I really love how it is simple on my eyes and the data are well written. I'm wondering how I could be notified when a new post has been made. I have subscribed to your feed which must do the trick! Have a great day!

  • Computer Modern回复

    I would like to thank you for the efforts you have put in writing this blog. I'm hoping the same high-grade site post from you in the upcoming also. In fact your creative writing abilities has encouraged me to get my own blog now. Really the blogging is spreading its wings fast. Your write up is a great example of it.

  • World Business回复

    I have learn several excellent stuff here. Definitely worth bookmarking for revisiting. I surprise how much effort you put to create this type of magnificent informative website.

  • Codeschloss回复

    Pretty! This has been an extremely wonderful article. Thank you for supplying this info.|

  • SCAM回复

    Thanks for making me to achieve new strategies about pcs. I also have belief that one of the best ways to help keep your notebook computer in perfect condition is by using a hard plastic-type material case, or maybe shell, that fits over the top of the computer. These kinds of protective gear usually are model unique since they are made to fit perfectly on the natural casing. You can buy all of them directly from the seller, or from third party places if they are for your laptop computer, however not every laptop could have a shell on the market. Just as before, thanks for your points.

  • Porn回复

    Thank you for the sensible critique. Me and my neighbor were just preparing to do a little research about this. We got a grab a book from our local library but I think I learned more from this post. I'm very glad to see such fantastic information being shared freely out there.

  • Business ideas回复

    I like the valuable info you provide in your articles. I’ll bookmark your weblog and check again here regularly. I am quite sure I will learn many new stuff right here! Best of luck for the next!

  • Computer gaming回复

    Good web site! I truly love how it is easy on my eyes and the data are well written. I'm wondering how I could be notified when a new post has been made. I have subscribed to your RSS feed which must do the trick! Have a great day!

  • dog boarding回复

    I’ve read several good stuff here. Certainly worth bookmarking for revisiting. I surprise how much effort you put to create such a wonderful informative site.

  • Technology News回复

    Thank you for the sensible critique. Me & my neighbor were just preparing to do a little research about this. We got a grab a book from our local library but I think I learned more clear from this post. I am very glad to see such excellent information being shared freely out there.

  • Dog Walker回复

    Some truly good content on this internet site , thanks for contribution.

  • Fake回复

    Sweet blog! I found it while searching on Yahoo News. Do you have any tips on how to get listed in Yahoo News? I've been trying for a while but I never seem to get there! Thanks