This article shares the specific code for Android to realize the effect of a suspended rocket for your reference. The specific content is as follows
Ideas
Use severce to start the rocket in the background
The small rocket is implemented using windowmanager.
Use ontoch monitoring to realize the drag of the small rocket.
Code implementation
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("Service creation...." ); mWM = (WindowManager) getSystemService(WINDOW_SERVICE ); winWidth = (). getWidth(); winHeight = mWM .getDefaultDisplay().getHeight (); params = new (); params. height = .WRAP_CONTENT ; params. width = .WRAP_CONTENT ; params. flags = .FLAG_KEEP_SCREEN_ON | .FLAG_NOT_FOCUSABLE ; params. format = PixelFormat. TRANSLUCENT ; params. type = .TYPE_PHONE ; params. gravity = Gravity. LEFT + Gravity. TOP; view = ( this).inflate(. rocket , null ); //Get imageview and set frame animation ImageView ivRocket = (ImageView) view .findViewById(. rocket); (. rocket ); AnimationDrawable drawable = (AnimationDrawable) (); (); ( view, params); // Set the touch event of the view so that it can be dragged ( new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (()) { case MotionEvent. ACTION_DOWN: startX = ( int ) (); startY = ( int ) (); break ; case MotionEvent. ACTION_MOVE: int dx = (int ) (() - startX ); int dy = (int ) (() - startY ); // Update the floating window position params. x += dx; params. y += dy; // Limit window coordinates not to exceed the screen 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(); } ( view, params ); startX = ( int ) (); startY = ( int ) (); break ; case MotionEvent. ACTION_UP: // Lift your fingers and need to launch a rocket, limiting the range of launching a rocket if (params .x > 0 && params. x < winWidth && params. y > winHeight - 500) { sendRocket(); } break ; } return true ;// No longer pass events to onClick for processing } }); } private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { int y = msg.arg1 ; params. y = y; ( view, params); } }; private int winWidth ; private int winHeight ; // Launch rocket private void sendRocket() { //Update the y-axis with child threads new Thread(new Runnable() { @Override public void run() { int pos = 1000; for (int i=0; i <= 10; i++) { int y = pos-100*i; //Sleep for 100ms Send a message try { Thread. sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block (); } Message msg = (); msg. arg1 = y; (msg); } } }).start(); } @Override public void onDestroy() { // TODO Auto-generated method stub super .onDestroy(); if (mWM != null && view != null) { ( view); view = null ; } } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.