public class MainActivity extends Activity {
private static final String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startService(new Intent(this, FloatWindowService.class));
finish();
}
}
import android.os.IBinder;
import android.util.Log;
import java.util.Timer;
import java.util.TimerTask;
public class FloatWindowService extends Service {
private static final String TAG = FloatWindowService.class.getSimpleName();
public FloatWindowService() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "on start command");
FloatWindowManager.instance(getApplicationContext()).createFloatWindow();
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
}
public void createFloatWindow() {
if (isWindowShowing()) return;
WindowManager windowManager = getWindowManger(context);
int screenWidth = windowManager.getDefaultDisplay().getWidth();
int screenHeight = windowManager.getDefaultDisplay().getHeight();
if (floatLayout == null) {
floatLayout = new FloatLayout(context);
if (smallLayoutParams == null) {
smallLayoutParams = new WindowManager.LayoutParams();
smallLayoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
smallLayoutParams.format = PixelFormat.RGBA_8888;
smallLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
smallLayoutParams.gravity = Gravity.LEFT | Gravity.TOP;
smallLayoutParams.width = FloatLayout.viewWidth;
smallLayoutParams.height = FloatLayout.viewHeight;
smallLayoutParams.x = screenWidth;
smallLayoutParams.y = screenHeight / 2;
}
}
windowManager.addView(floatLayout,smallLayoutParams);
}
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/small_layout" android:background="@drawable/bg_small" android:orientation="vertical" android:layout_width="60dip" android:layout_height="25dip"> <TextView android:layout_width="match_parent" android:gravity="center" android:text="悬浮窗" android:layout_height="match_parent" /> </LinearLayout>
public class FloatLayout extends LinearLayout {
public static int viewWidth;
public static int viewHeight;
private WindowManager windowManager;
public FloatLayout(final Context context) {
super(context);
windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
LayoutInflater.from(context).inflate(R.layout.small_layout, this);
View view = findViewById(R.id.small_layout);
viewWidth = view.getLayoutParams().width;
viewHeight = view.getLayoutParams().height;
setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
FloatWindowManager.instance(context).createFloatMenu();
return true;
}
});
}
}
public void createFloatMenu() {
if (menuLayout != null) return;
Log.d(TAG, "create float menu");
WindowManager windowManager = getWindowManger(context);
if (menuLayout == null){
menuLayout = new MenuLayout(context);
menuLayoutParams = new WindowManager.LayoutParams();
menuLayoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
menuLayoutParams.format = PixelFormat.RGBA_8888;
}
windowManager.addView(menuLayout,menuLayoutParams);
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:background="#96000000"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:id="@+id/trans_part"
android:orientation="horizontal"
android:layout_weight="1"
android:layout_height="0dp"></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_weight="1"
android:background="@color/colorPrimary"
android:layout_height="0dp">
<TextView
android:layout_width="match_parent"
android:text="存放content"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
public class MenuLayout extends LinearLayout {
public MenuLayout(final Context context) {
super(context);
LayoutInflater.from(context).inflate(R.layout.transparent_layout,this);
View view = findViewById(R.id.trans_part);
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
FloatWindowManager.instance(context).removeMenuLayout();
}
});
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2026 源码网商城 (www.ymwmall.com) 版权所有