27
2017
09

桌面悬浮小火箭的实现

思路

  1. 使用serivce在后台启动小火箭
  2. 小火箭使用windowmanager实现。
  3. 用ontoch监听实现小火箭的拖拽。

代码实现

public class RocketService extends Service {

      private WindowManager mWM;
      private View view;
      private int startX ;
      private int startY ;
      private LayoutParams params;

      @Override
      public IBinder onBind(Intent intent) {
           return null ;
     }

      @Override
      public void onCreate() {
           super .onCreate();
          System. out .println("服务创建。。。。" );
           mWM = (WindowManager) getSystemService(WINDOW_SERVICE );
           winWidth = mWM.getDefaultDisplay(). getWidth();
           winHeight = mWM .getDefaultDisplay().getHeight ();

           params = new WindowManager.LayoutParams();
           params. height = WindowManager.LayoutParams.WRAP_CONTENT ;
           params. width = WindowManager.LayoutParams.WRAP_CONTENT ;
           params. flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                   | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE ;
           params. format = PixelFormat. TRANSLUCENT ;
           params. type = WindowManager.LayoutParams.TYPE_PHONE ;
           params. gravity = Gravity. LEFT + Gravity. TOP;

           view = LayoutInflater.from( this).inflate(R.layout. rocket , null );
           //拿到 imageview,设置帧动画
          ImageView ivRocket = (ImageView) view .findViewById(R.id. rocket);
          ivRocket.setImageResource(R.drawable. rocket );
          AnimationDrawable drawable = (AnimationDrawable) ivRocket.getDrawable();
          drawable.start();
           mWM.addView( view, params);

           // 设置view的触摸事件,让它可以被拖拽
           view.setOnTouchListener( new OnTouchListener() {

               @Override
               public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()) {
                    case MotionEvent. ACTION_DOWN:
                         startX = ( int ) event.getRawX();
                         startY = ( int ) event.getRawY();
                         break ;
                    case MotionEvent. ACTION_MOVE:
                         int dx = (int ) (event.getRawX() - startX );
                         int dy = (int ) (event.getRawY() - startY );
                         // 更新浮窗位置
                         params. x += dx;
                         params. y += dy;

                         // 限制窗口坐标不超过屏幕
                         if (params .x < 0) {
                              params. x = 0;
                        }

                         if (params .x > winWidth - view .getWidth()) {
                              params. x = winWidth - view .getWidth();
                        }

                         if (params .y < 0) {
                              params. y = 0;
                        }

                         if (params .y > winHeight - view .getHeight()) {
                              params. y = winHeight - view .getHeight();
                        }

                         mWM.updateViewLayout( view, params );
                         startX = ( int ) event.getRawX();
                         startY = ( int ) event.getRawY();
                         break ;
                    case MotionEvent. ACTION_UP:
                         // 手指抬起起,需要发射火箭,限定发射火箭的范围
                         if (params .x > 0 && params. x < winWidth
                                  && params. y > winHeight - 500) {
                             sendRocket();
                        }
                         break ;
                   }

                    return true ;// 不再把事件传递给onClick处理
              }

          });
     }

      private Handler mHandler = new Handler() {
           @Override
           public void handleMessage(Message msg) {
                    int y = msg.arg1 ;
                    params. y = y;
                    mWM.updateViewLayout( view, params);
          }
     };
      private int winWidth ;
      private int winHeight ;

      // 发射火箭
      private void sendRocket() {
           // 用子线程更新y轴
           new Thread(new Runnable() {

               @Override
               public void run() {
                    int pos = 1000;
                    for (int i=0; i <= 10; i++) {
                              int y = pos-100*i;
                              //休眠100ms发消息
                              try {
                                  Thread. sleep(100);
                             } catch (InterruptedException e) {
                                   // TODO Auto-generated catch block
                                  e.printStackTrace();
                             }

                             Message msg  = Message.obtain();
                             msg. arg1 = y;
                              mHandler.sendMessage(msg);
                   }
              }
          }).start();
     }

      @Override
      public void onDestroy() {
           // TODO Auto-generated method stub
           super .onDestroy();
           if (mWM != null && view != null) {
               mWM.removeView( view);
               view = null ;
          }

     }
}
上一篇:工具类-软键盘 下一篇:Android Studio里的jni实现方法(二)