C语言学习网

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

发表于:2022-12-09 作者:安全数据网编辑
编辑最后更新 2022年12月09日,这篇文章将为大家详细讲解有关Android怎么实现桌面悬浮小火箭效果,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。具体内容如下思路使用serivce在后台启动小火箭

这篇文章将为大家详细讲解有关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 ;     }   }}

关于"Android怎么实现桌面悬浮小火箭效果"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

0