Facebook和MusixMatch就像浮动通知泡泡一样

时间:2016-09-24 10:15:10

标签: android android-layout service system-alert-window

我正在添加类似于Facebook Messenger“ChatHeads”或Musixmatch歌词功能的功能。

我有一个service,可以创建一个浮动notification气泡(如Messenger聊天头)。我正在对布局进行充气并将view添加到window manager

final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                900,
                1400,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);

LayoutInflater inflater = (LayoutInflater) this.getSystemService( this.LAYOUT_INFLATER_SERVICE );
mView = inflater.inflate(R.layout.floating, null);

windowManager.addView(mView, params); 

这是布局文件:

<RelativeLayout
android:id="@+id/outerRelativeLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:animateLayoutChanges="true" >

<ImageButton
    android:id="@+id/imagebutton"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:background="@drawable/shape_circular"
    android:src="@drawable/launcher"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="16dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:adjustViewBounds="true"
    android:scaleType="centerCrop"
    android:paddingBottom="8dp"
    android:paddingLeft="8dp"
    android:paddingTop="8dp"
    android:paddingRight="8dp"
    android:foregroundGravity="center_vertical|center_horizontal"/>


<RelativeLayout
    android:id="@+id/innerRelativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/imagebutton"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    android:layout_alignParentBottom="true"
    android:background="@drawable/shape_reound_edges_black">

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="CONTINUE"
        android:textSize="12sp"
        android:textColor="#FFFFFF"
        android:background="@drawable/shape_round_edges"
        android:layout_marginLeft="8dp"
        android:layout_marginBottom="8dp"
        android:layout_marginRight="8dp"
        android:layout_alignParentBottom="true"/>

    <VideoView
        android:id="@+id/videoView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_above="@+id/button"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:background="@drawable/shape_round_edges"/>

</RelativeLayout>

布局中的imagebutton充当floating notification button

我在OnTouchListener添加了imagebutton,帮助我移动layout。此外,当我点击该按钮时,innerRelativeLayout's visibility设置为GONE。但root relative layout仍然存在,因此imagebutton无法移动太多。如何实现浮动imagebutton的完整移动。

OnTouchListener的代码:

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) {
                    if (gestureDetector.onTouchEvent(event)) {
                        if (isLayoutVisible) {
                            mInnerLayout.setVisibility(View.GONE);
                            isLayoutVisible = false;
                        } else {
                            mInnerLayout.setVisibility(View.VISIBLE);
                            isLayoutVisible = true;
                        }
                        return true;
                    } else {
                        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(mView, params);
                                return true;
                        }
                    }
                return false;
            }
        });

提前致谢!

0 个答案:

没有答案