Java android-覆盖服务移动视图
我正在尝试创建一个始终处于运行状态的俯视图,并将图像放入其中。。。。 代码运行时没有任何错误 问题出在btn1TouchListener上,当用户触摸按钮ImageView时,ImageView会随之移动,但ImageView会先跳到屏幕的末端,然后开始在屏幕上移动,并且图像视图当前不会移动到用户触摸的位置。 IamgeView集合的Y没有任何问题,但X当前不适合 这是我的密码:Java android-覆盖服务移动视图,java,android,view,move,Java,Android,View,Move,我正在尝试创建一个始终处于运行状态的俯视图,并将图像放入其中。。。。 代码运行时没有任何错误 问题出在btn1TouchListener上,当用户触摸按钮ImageView时,ImageView会随之移动,但ImageView会先跳到屏幕的末端,然后开始在屏幕上移动,并且图像视图当前不会移动到用户触摸的位置。 IamgeView集合的Y没有任何问题,但X当前不适合 这是我的密码: public class OverlayService extends Service { LinearLayou
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;
}
});
}
第一次触摸视图时:
当我移动视图时:
最后,我找到了詹姆斯所说的答案 只需添加
Gravity.TOP | Gravity.LEFT
而不是Gravity.START | Gravity.LEFT