我有以下代码,我想得到x和y坐标。
public class HeadService extends Service {
private final static int FOREGROUND_ID = 999;
private WindowManager windowManager;
// private HeadLayer mHeadLayer;
ViewGroup mTopView;
LayoutInflater inflater;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
inflater = (LayoutInflater) getSystemService(getApplicationContext().LAYOUT_INFLATER_SERVICE);
mTopView = (ViewGroup) inflater.inflate(
R.layout.activity_invisible, null);
RelativeLayout rl;
rl = (RelativeLayout) mTopView.findViewById(R.id.window);
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT);
windowManager.addView(rl, params);
rl.setOnTouchListener(new View.OnTouchListener() {
private int initialX;
private int initialY;
private float initialTouchX;
private float initialTouchY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
initialX = params.x;
initialY = params.y;
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
return true;
case MotionEvent.ACTION_UP:
return true;
case MotionEvent.ACTION_MOVE:
params.x = initialX + (int) (event.getRawX() - initialTouchX);
params.y = initialY + (int) (event.getRawY() - initialTouchY);
Toast.makeText(getApplicationContext(), String.valueOf(params.x), Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
logServiceStarted();
initHeadLayer();
PendingIntent pendingIntent = createPendingIntent();
Notification notification = createNotification(pendingIntent);
startForeground(FOREGROUND_ID, notification);
return START_STICKY;
}
@Override
public void onDestroy() {
destroyHeadLayer();
stopForeground(true);
logServiceEnded();
if (chatHead != null) windowManager.removeView(chatHead);
}
private void initHeadLayer() {
// mHeadLayer = new HeadLayer(this);
}
private void destroyHeadLayer() {
// mHeadLayer.destroy();
// mHeadLayer = null;
}
private PendingIntent createPendingIntent() {
Intent intent = new Intent(this, MainActivity.class);
return PendingIntent.getActivity(this, 0, intent, 0);
}
private Notification createNotification(PendingIntent intent) {
return new Notification.Builder(this)
.setContentTitle(getText(R.string.notificationTitle))
.setContentText(getText(R.string.notificationText))
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(intent)
.build();
}
private void logServiceStarted() {
Toast.makeText(this, "Start", Toast.LENGTH_SHORT).show();
}
private void logServiceEnded() {
Toast.makeText(this, "End", Toast.LENGTH_SHORT).show();
}
}
xml文件:
<RelativeLayout
android:id="@+id/window"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#80000000"
android:clickable="false"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">
</RelativeLayout>
现在当我触摸屏幕时,我无法获得x和y坐标。
那么我怎样才能获得相对布局的坐标?
答案 0 :(得分:0)
我已经解决了。
我刚刚将WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY
替换为WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
,但它确实有效。
谢谢