android - 覆盖服务移动视图

时间:2015-10-31 09:17:21

标签: java android view move

我试图在其中创建一个始终操作顶视图并放置图像.... 代码运行没有任何错误。

问题在于 btn1 TouchListener,当用户触摸按钮时ImageView随之移动,但ImageView首先跳转到屏幕的末尾然后开始在屏幕上移动而图像视​​图不在目前在用户接触的地方移动.... IamgeView套装的Y没有任何问题,但X当前不适合。

这是我的代码:

public class OverlayService extends Service {

LinearLayout lView;
TouchImageView oView;    //My Custom Image View
WindowManager wm;
Button btn1;
WindowManager.LayoutParams params;
Bitmap a;

@Override
public IBinder onBind(Intent i) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    sendNotification();

    isRun=true;

    //Get A Bitmap For Imageview
    clashbaz.irana.ir.clashbaz.Bitmap mbit = new clashbaz.irana.ir.clashbaz.Bitmap();

    a = mbit.imageFromAsset(intent.getStringExtra("data"),getApplicationContext());

    oView.viewHeight = a.getHeight();
    oView.viewWidth = a.getWidth();

    lView.addView(btn1);
    lView.addView(oView);

    params = new WindowManager.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
            | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
            PixelFormat.TRANSLUCENT);

    wm = (WindowManager) getSystemService(WINDOW_SERVICE);

    params.gravity = Gravity.START | Gravity.LEFT;
    wm.addView(lView, params);
    oView.setImageDrawable(new BitmapDrawable(a)); // Image Resource
    wm.updateViewLayout(lView, params);
    return START_STICKY;
}

@Override
public void onCreate() {
    super.onCreate();
    lView = new LinearLayout(this);
    lView.setOrientation(LinearLayout.VERTICAL);

    oView = new TouchImageView(this);
    oView.isFocusable();
    btn1 = new Button(this);
    btn1.setText("Move");
    btn1.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent e) {
            if (e.getAction() == MotionEvent.ACTION_MOVE) {
                params.x = (int) e.getRawX();
                params.y = (int) e.getRawY();
                wm.updateViewLayout(lView, params);
            }
            return false;
        }
    });

}

我第一次触摸视图: First Time

当我移动视图时: Second Time

1 个答案:

答案 0 :(得分:1)

最后,我找到答案为詹姆斯说here

只需添加Gravity.TOP | Gravity.LEFT Insted of Gravity.START | Gravity.LEFT