Android实现桌面悬浮小火箭效果
程序员文章站
2023-12-15 15:26:40
本文实例为大家分享了android实现悬浮小火箭效果的具体代码,供大家参考,具体内容如下
思路
使用serivce在后台启动小火箭
小火箭使用windowmanage...
本文实例为大家分享了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 ; } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。