Android自定義APP全局懸浮按鈕
原本想通過framelayout實現(xiàn)一個懸浮在其他控件上的按鈕,但是覺得很麻煩,需要各個界面都要動態(tài)填充.于是想到了懸浮窗,就自定一個ImageView用于顯示全局按鈕.
一、首先因為懸浮窗式的所以要添加權(quán)限,對于SDK>=23的需要動態(tài)獲取權(quán)限,我這邊用的是22的
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> <uses-permission android:name="android.permission.WRITE_SETTINGS"/>
二、通過application獲取到全局性的WindowManager的params數(shù)據(jù)
private WindowManager.LayoutParams wmParams=new WindowManager.LayoutParams();
public WindowManager.LayoutParams getMywmParams(){
return wmParams;
}
三、自定義ImageView,并實現(xiàn)點擊具有狀態(tài)選擇.其中寫了一個回調(diào)接口用于對點擊事件的處理
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"); //使用反射獲取實例
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) {
//獲取相對屏幕的坐標,即以屏幕左上角為原點
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: //手指移動
updateViewPosition();
isMove = true;
break;
case MotionEvent.ACTION_UP: //手抬起
setImageResource(R.drawable.btn_voice_rest);
mLastX = event.getRawX();
mLastY = event.getRawY();
mUpTime = System.currentTimeMillis();
//按下到抬起的時間大于500毫秒,并且抬手到抬手絕對值大于20像素處理點擊事件
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à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, "點擊事件", Toast.LENGTH_SHORT).show();
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
if(CustomeMovebutton != null){
wm.removeView(CustomeMovebutton);
}
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android基于OpenCV實現(xiàn)QR二維碼檢測
QR碼比普通一維條碼具有快速讀取和更大的存儲資料容量,也無需要像一維條碼般在掃描時需要直線對準掃描儀。因此其應(yīng)用范圍已經(jīng)擴展到包括產(chǎn)品跟蹤,物品識別,文檔管理,庫存營銷等方面。本文講解Android基于OpenCV實現(xiàn)QR二維碼檢測的步驟2021-06-06
Android開發(fā)listview選中高亮簡單實現(xiàn)代碼分享
這篇文章主要介紹了Android開發(fā)listview選中高亮簡單實現(xiàn)代碼分享,具有一定借鑒價值,需要的朋友可以參考下2018-01-01
Android實現(xiàn)音頻條形圖效果(仿音頻動畫無監(jiān)聽音頻輸入)
這篇文章主要介紹了Android實現(xiàn)音頻條形圖效果(仿音頻動畫無監(jiān)聽音頻輸入)的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09

