如何创建一个可以像 Facebook 通知一样在整个屏幕上移动的浮动广告?

How do I create a floater that I can move all over the screen like the Facebook notification?

如何创建一个可以像 Facebook 通知一样在整个屏幕上移动的浮动广告?

尝试以下代码

创建ActivityMainActivit.java

public class MainActivity extends Activity {
Button startService,stopService;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    startService=(Button)findViewById(R.id.startService);
    stopService=(Button)findViewById(R.id.stopService);
    startService.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            startService(new Intent(getApplication(), ChatHeadService.class));

        }
    });
    stopService.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            stopService(new Intent(getApplication(), ChatHeadService.class));

        }
    });
}

}

为ChatHeadService.java

写一个服务
public class ChatHeadService extends Service {

private WindowManager windowManager;
private ImageView chatHead;
WindowManager.LayoutParams params;

@Override
public void onCreate() {
    super.onCreate();

    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

    chatHead = new ImageView(this);
    chatHead.setImageResource(R.drawable.face1);

    params= new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);

    params.gravity = Gravity.TOP | Gravity.LEFT;
    params.x = 0;
    params.y = 100;

    //this code is for dragging the chat head
    chatHead.setOnTouchListener(new View.OnTouchListener() {
        private int initialX;
        private int initialY;
        private float initialTouchX;
        private float initialTouchY;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                initialX = params.x;
                initialY = params.y;
                initialTouchX = event.getRawX();
                initialTouchY = event.getRawY();
                return true;
            case MotionEvent.ACTION_UP:
                return true;
            case MotionEvent.ACTION_MOVE:
                params.x = initialX
                        + (int) (event.getRawX() - initialTouchX);
                params.y = initialY
                        + (int) (event.getRawY() - initialTouchY);
                windowManager.updateViewLayout(chatHead, params);
                return true;
            }
            return false;
        }
    });
    windowManager.addView(chatHead, params);
}

@Override
public void onDestroy() {
    super.onDestroy();
    if (chatHead != null)
        windowManager.removeView(chatHead);
}

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}
}

在您的 androidMenifest 文件中添加以下代码

<service android:name="your package name".ChatHeadService" >
    </service>

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />