【问题标题】:android - Overlay Service Move Viewandroid - 覆盖服务移动视图
【发布时间】:2015-10-31 09:17:20
【问题描述】:

我正在尝试创建一个始终位于顶部的视图并将图像放入其中...... 代码运行没有任何错误。

问题出在 btn1 TouchListener ,当用户触摸按钮时 ImageView 随之移动,但 ImageView 先跳到屏幕的末尾然后开始在屏幕上移动并且图像视图没有当前移动到用户触摸的地方...... IamgeView Set 的 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;
        }
    });

}

我第一次触摸视图:

当我移动视图时:

【问题讨论】:

    标签: java android view move


    【解决方案1】:

    终于,正如詹姆斯所说,我找到了答案here

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-25
      • 1970-01-01
      • 2016-09-25
      • 2018-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多