本篇文章給大家分享的是有關(guān)Android中怎么自定義一個(gè)全局懸浮按鈕,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。
成都創(chuàng)新互聯(lián)專(zhuān)注于紅崗網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供紅崗營(yíng)銷(xiāo)型網(wǎng)站建設(shè),紅崗網(wǎng)站制作、紅崗網(wǎng)頁(yè)設(shè)計(jì)、紅崗網(wǎng)站官網(wǎng)定制、小程序定制開(kāi)發(fā)服務(wù),打造紅崗網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供紅崗網(wǎng)站排名全網(wǎng)營(yíng)銷(xiāo)落地服務(wù)。
一、首先因?yàn)閼腋〈笆降乃砸砑訖?quán)限,對(duì)于SDK>=23的需要?jiǎng)討B(tài)獲取權(quán)限,我這邊用的是22的
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> <uses-permission android:name="android.permission.WRITE_SETTINGS"/>
二、通過(guò)application獲取到全局性的WindowManager的params數(shù)據(jù)
private WindowManager.LayoutParams wmParams=new WindowManager.LayoutParams(); public WindowManager.LayoutParams getMywmParams(){ return wmParams; }
三、自定義ImageView,并實(shí)現(xiàn)點(diǎn)擊具有狀態(tài)選擇.其中寫(xiě)了一個(gè)回調(diào)接口用于對(duì)點(diǎn)擊事件的處理
public class CustomeMovebutton extends ImageView { private final int statusHeight; int sW; int sH; private float mTouchStartX; private float mTouchStartY; private float x; private float y; private boolean isMove=false; private Context context; private WindowManager wm = (WindowManager) getContext().getApplicationContext().getSystemService(Context.WINDOW_SERVICE); private WindowManager.LayoutParams wmParams = ((MyApplication) getContext().getApplicationContext()).getMywmParams(); private float mLastX; private float mLastY; private float mStartX; private float mStartY; private long mDownTime; private long mUpTime; private OnSpeakListener listener; public CustomeMovebutton(Context context) { this(context,null); this.context = context; } public CustomeMovebutton(Context context, AttributeSet attrs) { this(context, attrs,-1); } public CustomeMovebutton(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs,defStyleAttr); sW = wm.getDefaultDisplay().getWidth(); sH = wm.getDefaultDisplay().getHeight(); statusHeight = getStatusHeight(context); } /** * 狀態(tài)欄的高度 * */ public static int getStatusHeight(Context context) { int statusHeight = -1; try { Class clazz = Class.forName("com.android.internal.R$dimen"); //使用反射獲取實(shí)例 Object object = clazz.newInstance(); int height = Integer.parseInt(clazz.getField("status_bar_height") .get(object).toString()); statusHeight = context.getResources().getDimensionPixelSize(height); } catch (Exception e) { e.printStackTrace(); } return statusHeight; } @Override public boolean onTouchEvent(MotionEvent event) { //獲取相對(duì)屏幕的坐標(biāo),即以屏幕左上角為原點(diǎn) x = event.getRawX(); y = event.getRawY() - statusHeight; //statusHeight是系統(tǒng)狀態(tài)欄的高度 switch (event.getAction()) { case MotionEvent.ACTION_DOWN: //按下 setImageResource(R.drawable.btn_voice_pressed); mTouchStartX = event.getX(); mTouchStartY = event.getY(); mStartX = event.getRawX(); mStartY = event.getRawY(); mDownTime = System.currentTimeMillis(); isMove = false; break; case MotionEvent.ACTION_MOVE: //手指移動(dòng) updateViewPosition(); isMove = true; break; case MotionEvent.ACTION_UP: //手抬起 setImageResource(R.drawable.btn_voice_rest); mLastX = event.getRawX(); mLastY = event.getRawY(); mUpTime = System.currentTimeMillis(); //按下到抬起的時(shí)間大于500毫秒,并且抬手到抬手絕對(duì)值大于20像素處理點(diǎn)擊事件 if(mUpTime - mDownTime < 500){ if(Math.abs(mStartX- mLastX )< 20.0 && Math.abs(mStartY - mLastY) < 20.0){ if (listener!=null){ listener.onSpeakListener(); } } } break; } return true; } private void updateViewPosition() { wmParams.x = (int) (x - mTouchStartX); wmParams.y = (int) (y- mTouchStartY); wm.updateViewLayout(this, wmParams); //刷新顯示 } /** * 設(shè)置點(diǎn)擊回調(diào)接口 */ public interface OnSpeakListener{ void onSpeakListener(); } public void setOnSpeakListener(OnSpeakListener listener){ this.listener=listener; } }
四、Activity中使用,其中有設(shè)置圖片的參數(shù)和位置參數(shù)
public class MainActivity extends AppCompatActivity{ private WindowManager wm; private WindowManager.LayoutParams wmParams; private CustomeMovebutton CustomeMovebutton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE); DisplayMetrics dm = getResources().getDisplayMetrics(); int widthPixels = dm.widthPixels; int heightPixels = dm.heightPixels; wmParams = ((MyApplication) getApplication()).getMywmParams(); wmParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT; wmParams.format= PixelFormat.RGBA_8888;//設(shè)置背景圖片 wmParams.flags= WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE ;// wmParams.gravity = Gravity.LEFT|Gravity.TOP;// wmParams.x = widthPixels-150; //設(shè)置位置像素 wmParams.y = heightPixels-110; wmParams.width=200; //設(shè)置圖片大小 wmParams.height=200; CustomeMovebutton = new CustomeMovebutton(getApplicationContext()); CustomeMovebutton.setImageResource(R.drawable.btn_voice_rest); wm.addView(CustomeMovebutton, wmParams); CustomeMovebutton.setOnSpeakListener(new CustomeMovebutton.OnSpeakListener() { @Override public void onSpeakListener() { Toast.makeText(MainActivity.this, "點(diǎn)擊事件", Toast.LENGTH_SHORT).show(); } }); } @Override protected void onDestroy() { super.onDestroy(); if(CustomeMovebutton != null){ wm.removeView(CustomeMovebutton); } } }
以上就是Android中怎么自定義一個(gè)全局懸浮按鈕,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)站標(biāo)題:Android中怎么自定義一個(gè)全局懸浮按鈕
URL標(biāo)題:http://vcdvsql.cn/article2/podsic.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁(yè)設(shè)計(jì)公司、品牌網(wǎng)站建設(shè)、外貿(mào)建站、網(wǎng)站收錄、定制網(wǎng)站、建站公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
網(wǎng)頁(yè)設(shè)計(jì)公司知識(shí)