2

我正在尝试在屏幕上的所有内容(TOP MOST)上绘制一个图标,类似于新 Facebook Messenger 的聊天头

我已经创建了一个在后台工作的服务,并且根据特定条件,我的图标应该出现在屏幕上(就像有人在 facebook 上向您发送消息时,Messenger 服务将挂钩消息并在屏幕上显示聊天头以通知你关于新消息)

我做了什么:

我已经创建了该服务并授予它显示系统警报窗口的权限(因为头部实际上是一个系统警报窗口)

[assembly: UsesPermission(Name = Android.Manifest.Permission.SystemAlertWindow)]

我从 ImageView 继承了一个类(StickyHeadView)并使用以下方式实现了 OnTouchListener 侦听器:

class StickyHeadView : ImageView, Android.Views.View.IOnTouchListener
{
    private StickyHeadService OwnerService;

    public StickyHeadView(StickyHeadService ContextService, Context context)
        : base(context)
    {
        OwnerService = ContextService;
        SetOnTouchListener(this);
    }

    float TouchMoveX;
    float TouchMoveY;

    public bool OnTouch(View v, MotionEvent e)
    {
        var windowService = OwnerService.GetSystemService(Android.Content.Context.WindowService);
        var windowManager = windowService.JavaCast<Android.Views.IWindowManager>();

        switch (e.Action & e.ActionMasked)
        {
            case MotionEventActions.Move:
                TouchMoveX = (int)e.GetX();
                TouchMoveY = (int)e.GetY();
                OwnerService.LOParams.X = (int)(TouchMoveX);
                OwnerService.LOParams.Y = (int)(TouchMoveY);
                windowManager.UpdateViewLayout(this, OwnerService.LOParams);                    
                Log.Debug("Point : ", "X: " + Convert.ToString(OwnerService.LOParams.X) + " Y: " + Convert.ToString(OwnerService.LOParams.Y));
                return true;                    
            case MotionEventActions.Down:
                return true;                    
            case MotionEventActions.Up:
                return true;
        }
        return false;
    }
}    

该服务具有窗口管理器以在其上显示图标...在服务“OnStart”事件中我初始化了 Head :

        private StickyHeadView MyHead;
        public Android.Views.WindowManagerLayoutParams LOParams;
        public override void OnStart(Android.Content.Intent intent, int startId)
        {
            base.OnStart(intent, startId);

            var windowService = this.GetSystemService(Android.Content.Context.WindowService);
            var windowManager = windowService.JavaCast<Android.Views.IWindowManager>();

            MyHead = new StickyHeadView(this, this);
            MyHead.SetImageResource(Resource.Drawable.Icon);
            LOParams = new Android.Views.WindowManagerLayoutParams(Android.Views.WindowManagerLayoutParams.WrapContent,
                                Android.Views.WindowManagerLayoutParams.WrapContent,
                                Android.Views.WindowManagerTypes.Phone,
                                Android.Views.WindowManagerFlags.NotFocusable,
                                Android.Graphics.Format.Translucent);

            LOParams.Gravity = GravityFlags.Top | GravityFlags.Left;
            LOParams.X = 10;
            LOParams.Y = 10;
            windowManager.AddView(MyHead, LOParams);
        } 

如您所见,我已经声明了一个 WindowManager 并使用特殊参数向其中添加了视图(MyHead)

我的问题 :

当我尝试移动视图(我的头)时,它不会以稳定的方式移动并不断发生地震!

我正在真正的 HTC 手机上使用 android 4.0.4 对其进行测试

我正在使用单机器人

请帮助...如果触摸的实施不正确,请提出更好的方法...谢谢。

4

3 回答 3

6

在您的代码中,只需使用...

TYPE_SYSTEM_ALERT

或者

TYPE_PHONE

代替

TYPE_SYSTEM_OVERLAY

希望这会帮助你。

一个工作示例:

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

    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

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

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

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

    windowManager.addView(chatHead, params);

    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;
          }
        });
}
于 2014-02-23T14:23:11.697 回答
3

您使用的 e.GetX()/eGetY() 是相对于视图位置的,因此当您使用 UpdateViewLayout 移动视图时,下一个值将相对于移动。它使用 GetRawX()/GetRawY() 工作,但您还必须跟踪初始 Down rawX 和 rawY。

这是我的有效的JAVA:

@Override
public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_MOVE:
            layoutParams.x = Math.round(event.getRawX() - downX);
            layoutParams.y = Math.round(event.getRawY() - downY);
            windowManager.updateViewLayout(floatingView, layoutParams);
            return true;                    
        case MotionEvent.ACTION_DOWN:
            downX = event.getRawX() - layoutParams.x;
            downY = event.getRawY() - layoutParams.y;
            return true;                    
        case MotionEvent.ACTION_UP:
            return true;
    }
    return false;
}

一条评论,使用 windowManager.updateViewLayout(...) 有一个很大的缺点,这个方法会在每次移动的浮动视图上调用 onLayout,这可能是一个性能问题,无论如何直到现在我还没有找到另一种移动方法浮动视图。

于 2013-05-23T09:12:25.097 回答
1

试试这个可能会有所帮助

首先在您的活动中添加全局变量:

    WindowManager wm;
    LinearLayout lay;
    float downX,downY;

在您的活动中输入代码以创建后

    Button btnstart=(Button)findViewById(R.id.button1);
    Button btnstop=(Button)findViewById(R.id.button2);

    btnstart.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            if(lay==null)
            {
                wm = (WindowManager) getApplicationContext().getSystemService(
                        Context.WINDOW_SERVICE);
                final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                        LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT,
                        WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
                                | WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
                        WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                        PixelFormat.TRANSLUCENT);
                params.x = (int) wm.getDefaultDisplay().getWidth();
                params.y = 0;
                // params.height = wm.getDefaultDisplay().getHeight()/2;
                params.width = LayoutParams.WRAP_CONTENT;
                params.format = PixelFormat.TRANSLUCENT;

                params.gravity = Gravity.TOP | Gravity.LEFT;
                params.setTitle("Info");

                lay = null;

                lay = new LinearLayout(getApplicationContext());
                lay.setOrientation(LinearLayout.VERTICAL);
                // lay.setAlpha(0.5f);

                TextView txt_no = new TextView(getApplicationContext());
                txt_no.setTextSize(10.0f);
                txt_no.setText("Moving view by stack user!");

                txt_no.setTextColor(Color.BLACK);
                // txt_no.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                // LayoutParams.WRAP_CONTENT));

                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);
                layoutParams.setMargins(0, 0, 0, 0); // margins as you wish

                txt_no.setGravity(Gravity.RIGHT);
                txt_no.setBackgroundColor(Color.WHITE);
                txt_no.setLayoutParams(layoutParams);
                txt_no.setPadding(10, 10, 10, 10);

                lay.addView(txt_no);

                AlphaAnimation alpha = new AlphaAnimation(0.5F, 0.5F);
                alpha.setDuration(0); // Make animation instant
                alpha.setFillAfter(true); // Tell it to persist after the animation ends
                // And then on your layout
                wm.addView(lay, params);
                txt_no.startAnimation(alpha);

                downX=params.x;
                downY=params.y;

                Log.v("MSES>", "x="+ downX +",y="+ downY);

                lay.setOnTouchListener(new OnTouchListener() {

                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        // TODO Auto-generated method stub

                        switch (event.getAction()) {
                        case MotionEvent.ACTION_MOVE:

                            params.x = Math.round(event.getRawX() - downX);
                            params.y = Math.round(event.getRawY() - downY);
                            wm.updateViewLayout(lay, params);
                            Log.v("MSES EVENT>", "x="+ event.getRawX() +",y="+ event.getRawY());
                            Log.v("MSES MOVE>", "x="+ params.x +",y="+ params.y);
                            return true;                    
                        case MotionEvent.ACTION_DOWN:
                            downX = event.getRawX() - params.x;
                            downY = event.getRawY() - params.y;
                            Log.v("MSES DOWN>", "x="+ params.x +",y="+ params.y);
                           return true;                    
                        case MotionEvent.ACTION_UP:
                            //params.x = Math.round(event.getRawX() - downX);
                            //params.y = Math.round(event.getRawY() - downY);
                            //wm.updateViewLayout(lay, params);
                            return true;                   

                        }
                        return false;
                    }
                });
            }

        }
    });

    btnstop.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (lay != null) {
                lay.removeAllViews();
                wm.removeViewImmediate(lay);
                lay = null;
            }
        }
    });
于 2013-07-29T08:12:09.883 回答