简体   繁体   English

android-叠加服务移动视图

[英]android - Overlay Service Move View

I'm trying to create an always-op-top View And Put Image Inside It.... The Code Run Without any errors. 我正在尝试创建一个始终运行于顶部的视图并将图像放入其中。...代码运行时没有任何错误。

The Problem is On btn1 TouchListener ,When User Touch The Button ImageView Move with it, But ImageView Firstly jump To the end of The Screen then start to move on the screen and the image view dose not move currently where user touches... . 问题出在btn1 TouchListener上,当用户触摸按钮ImageView随其移动时,但是ImageView首先跳到屏幕的末端,然后开始在屏幕上移动,并且图像视图当前在用户触摸的地方不动...。 The Y of IamgeView Set's With out any problems but the X Doesn't Fit Currently. IamgeView Set的Y表示没有问题,但X当前不适合。

Here Is My Code : 这是我的代码:

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 I Touch The View: 我第一次触摸视图: 第一次

When I Move The The View: 当我移动视图时: 第二次

Finaly, I Found The Answer as james Said here . 最终,我在这里找到了詹姆斯的话。

Just Add Gravity.TOP | Gravity.LEFT 只需添加Gravity.TOP | Gravity.LEFT Gravity.TOP | Gravity.LEFT Insted Of Gravity.START | Gravity.LEFT 重力。 Gravity.START | Gravity.LEFT Gravity.TOP | Gravity.LEFT重力Gravity.START | Gravity.LEFT Gravity.START | Gravity.LEFT

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM