亲宝软件园·资讯

展开

Android悬浮小火箭 Android实现桌面悬浮小火箭效果

lifeirong 人气:0
想了解Android实现桌面悬浮小火箭效果的相关内容吗,lifeirong在本文为您仔细讲解Android悬浮小火箭的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android悬浮小火箭,Android桌面悬浮小火箭,Android小火箭,下面大家一起来学习吧。

思路

使用serivce在后台启动小火箭
小火箭使用windowmanager实现。
用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 ;
   }

  }
}

加载全部内容

相关教程
猜你喜欢
用户评论