您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關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怎么實現桌面懸浮小火箭效果”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。