移动位于WindowManager中的ImageView无法正常工作

时间:2013-05-03 08:52:02

标签: android-layout xamarin.android android-imageview ontouchlistener android-windowmanager

我正在尝试在屏幕上显示所有内容的图标(TOP MOST),类似于新Facebook Messenger的聊天对象

我已经创建了一个在后台工作的服务,并根据特定情况我的图标应该出现在屏幕上(就像有人在Facebook上发送消息时,信使服务将挂钩消息并显示聊天屏幕通知您有关新消息的信息)

我做了什么:

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

[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;
    }
}    

该服务有wiindow经理在其上显示图标...在服务“OnStart”事件中我初始化头部:

        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)添加到它中

我的问题:

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

我正在使用Android 4.0.4在真正的HTC手机上测试它

我正在使用monodroid

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

3 个答案:

答案 0 :(得分:5)

在您的代码中使用...

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;
          }
        });
}

答案 1 :(得分: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,这可能是一个性能问题,无论如何,直到现在我还没有找到另一个移动浮动视图的方法。

答案 2 :(得分: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;
            }
        }
    });