android 懸浮球 (所有介面可用) 開發
阿新 • • 發佈:2019-01-02
建立 service 後臺啟動
public class FBService extends Service {
private static final String TAG = "FBService";
public static final int TYPE_ADD = 0;
public static final int TYPE_DEL = 1;
private FloatBallManager mFBManager;
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG,"onCreate" );
mFBManager = FloatBallManager.getInstance(this);
}
private void showFloatBall() {
Log.d(TAG,"showFloatBall");
mFBManager.addBallView(); //通過mFBManager 在windowmanager新增view
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG,"onStartCommand" );showFloatBall(intent);
showFloatBall();
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG,"onDestroy" );
}
}
通過FloatBallManager 在windowmanager 載入 懸浮球佈局
public class FloatBallManager {
private static final String TAG = "FloatBallManager";
private FloatBallManager(Context ctx){
mCtx = ctx;
mWindowManager = (WindowManager) mCtx.getSystemService(Context.WINDOW_SERVICE);
//獲取WindowManager
}
public static FloatBallManager getInstance(Context ctx){
if(instance == null){
synchronized(FloatBallManager.class){
if(instance == null){
instance = new FloatBallManager(ctx);
}
}
}
return instance;
}
public void addBallView() {
if (mFloatBallView == null) {
//獲取 整個手機的寬度和高度
DisplayMetrics dm = new DisplayMetrics();
mWindowManager.getDefaultDisplay().getMetrics(dm);
mScreenWidth = dm.widthPixels;
mScreenHeight = dm.heightPixels;
//填充懸浮球 佈局
mFBWindow = LayoutInflater.from(mCtx).inflate(R.layout.float_ball, null);
mFloatBallView = mFBWindow.findViewById(R.id.float_ball);
mFBParams = new LayoutParams(); //設定懸浮球佈局的引數
mFBParams.x = mScreenWidth-mFloatBallView.getWidth();
mFBParams.y = mScreenHeight / 2;
mFBParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
mFBParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
mFBParams.gravity = Gravity.LEFT | Gravity.TOP;
mFBParams.type = LayoutParams.TYPE_SYSTEM_ALERT | LayoutParams.TYPE_SYSTEM_OVERLAY;//頂層的View ,就要用這兩個type
mFBParams.format = PixelFormat.TRANSLUCENT;//半透明
mFBParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_NOT_FOCUSABLE;//設定沒有焦點不能touch,這樣其他的介面才可以滑動和操作
mWindowManager.addView(mFBWindow, mFBParams);
//將View 按照mFBParams引數設定,新增到mWindowManager ,mFBParams是告訴mFBWindow 的父view
}
}
//這個方法用來拖動時使用,更新位置
public void updateBallView(int deltaX,int deltaY) {
mFBParams.x += deltaX;
mFBParams.y += deltaY;
if (mWindowManager != null) {
// Log.d(TAG,"updateBallView mParams.x = "+mFBParams.x+" mParams.y = "+mFBParams.y);
mWindowManager.updateViewLayout(mFBWindow, mFBParams);
}
}
}
懸浮球佈局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fl_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:orientation="vertical">
<!--懸浮球背景-->
<ImageView
android:id="@+id/img_bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleX="0.7"
android:scaleY="0.7"
android:background="@drawable/icon_bg"
android:layout_gravity="center"/>
<!--懸浮球動畫小球-->
<ImageView
android:id="@+id/img_ball"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:alpha="0.5"
android:scaleX="0.7"
android:scaleY="0.7"
android:background="@drawable/icon_ball"/>
<!--懸浮球動畫大球-->
<ImageView
android:id="@+id/img_big_ball"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:scaleX="0.7"
android:scaleY="0.7"
android:background="@drawable/icon_big_ball"
android:visibility="invisible"
/>
<!--懸浮球開關 預設是off-->
<TextView
android:id="@+id/tv_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/switch_off"/>
</FrameLayout>
以上一個懸浮球 就可以顯示了,但是不能操作