简体   繁体   中英

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... . The Y of IamgeView Set's With out any problems but the X Doesn't Fit Currently.

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 Insted Of Gravity.START | Gravity.LEFT Gravity.START | Gravity.LEFT

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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