這篇文章將為大家詳細(xì)講解有關(guān)Android怎么實(shí)現(xiàn)桌面懸浮小火箭效果,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
成都創(chuàng)新互聯(lián)公司是一家專業(yè)的成都網(wǎng)站建設(shè)公司,我們專注成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、網(wǎng)絡(luò)營銷、企業(yè)網(wǎng)站建設(shè),買鏈接,1元廣告為企業(yè)客戶提供一站式建站解決方案,能帶給客戶新的互聯(lián)網(wǎng)理念。從網(wǎng)站結(jié)構(gòu)的規(guī)劃UI設(shè)計(jì)到用戶體驗(yàn)提高,創(chuàng)新互聯(lián)力求做到盡善盡美。
具體內(nèi)容如下
思路
使用serivce在后臺啟動小火箭
小火箭使用windowmanager實(shí)現(xiàn)。
用ontoch監(jiān)聽實(shí)現(xiàn)小火箭的拖拽。
代碼實(shí)現(xiàn)
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("服務(wù)創(chuàng)建。。。。" ); 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,設(shè)置幀動畫 ImageView ivRocket = (ImageView) view .findViewById(R.id. rocket); ivRocket.setImageResource(R.drawable. rocket ); AnimationDrawable drawable = (AnimationDrawable) ivRocket.getDrawable(); drawable.start(); mWM.addView( view, params); // 設(shè)置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; // 限制窗口坐標(biāo)不超過屏幕 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: // 手指抬起起,需要發(fā)射火箭,限定發(fā)射火箭的范圍 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 ; // 發(fā)射火箭 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發(fā)消息 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 ; } } }
關(guān)于“Android怎么實(shí)現(xiàn)桌面懸浮小火箭效果”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯(cuò),請把它分享出去讓更多的人看到。