1

我知道如何创建一个简单的透明活动,但我如何同时拥有透明活动和可在屏幕上拖动。我能想到的最好的例子是Google Play 上的 Overskreen 应用程序。

4

1 回答 1

0

尝试这个

显现:

<activity
       android:name=".popup.PopupActivity"
       android:excludeFromRecents="true"
       android:label="@string/app_name"
       android:launchMode="singleTop"
       android:screenOrientation="portrait"
       android:theme="@style/Theme.Transparent" />

代码:

View mRootView;
WindowManager.LayoutParams params;
WindowManager windowManager;

@Override
protected void onCreate(Bundle savedInstanceState){
    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

    mRootView = getLayoutInflater().inflate(R.layout.popup_alert_dialog, null);

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

    params.gravity = Gravity.TOP | Gravity.START;
    params.x = 0; // initial position
    params.y = 0; // initial position

    //this code is for dragging the chat head
    mRootView.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:
                    int newX = initialX
                            + (int) (event.getRawX() - initialTouchX); // new position
                    int newY = initialY
                            + (int) (event.getRawY() - initialTouchY); // new position
                    params.x = newX;
                    params.y = newY;
                    windowManager.updateViewLayout(mRootView, params);
                    return true;
            }
            return false;
        }
    });

    windowManager.addView(mRootView, params);

    // update content view here
    TextView tv = (TextView) mRootView.findViewById(R.id.textView);
    tv.setText("Hello World!!");

    // no need to call setContentView() any more
}
于 2016-10-02T12:01:24.613 回答