我正在尝试制作浮动操纵杆。因为我在圈内使用了圆圈。我制作了两个单独的xml文件,其中包含小圆圈和大圆圈的图像。我把小圆圈放在大圆圈上。但是当我试图拖动大圆圈时,小圆圈不会移动。所以如何将它与图像结合起来,如果我拖动大圆圈,小圆圈也会随之移动。我也想移动小圆圈,但它不应该超出大圆圈。
这是我的代码: 第一个xml(大圆圈):
<ImageView
android:id="@+id/layout_joystick"
android:layout_width="314dp"
android:layout_height="200dp"
android:src="@drawable/image_button_bg" />
</RelativeLayout>
第二个xml(小圆圈):
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/image_button" />
</RelativeLayout>
这是我的java代码: WindowManager wm; RelativeLayout mInview; RelativeLayout sball;
RelativeLayout layout_joystick;
ImageView innercircle;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
wm = (WindowManager) getSystemService(WINDOW_SERVICE);
LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
mInview = (RelativeLayout)inflater.inflate(R.layout.floatingwindow, null, false);
sball=(RelativeLayout)inflater.inflate(R.layout.inner, null, false);
RelativeLayout.LayoutParams layoutParameteres = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, 700);
RelativeLayout.LayoutParams layoutPara = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, 300);
mInview.setLayoutParams(layoutParameteres);
sball.setLayoutParams(layoutPara);
final WindowManager.LayoutParams parameters = new WindowManager.LayoutParams(
600, 400, WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
final WindowManager.LayoutParams para = new WindowManager.LayoutParams(
200, 200, WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
parameters.gravity = Gravity.CENTER | Gravity.CENTER;
parameters.x = 0;
parameters.y = 0;
Button stop = new Button(this);
stop.setText("Stop");
ViewGroup.LayoutParams btnParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
stop.setLayoutParams(btnParameters);
mInview.addView(stop);
wm.addView(mInview, parameters);
wm.addView(sball,para);
sball.setOnTouchListener(new View.OnTouchListener() {
WindowManager.LayoutParams updatedParameters = para;
double x;
double y;
double pressedX;
double pressedY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
x = updatedParameters.x;
y = updatedParameters.y;
pressedX = event.getRawX();
pressedY = event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
updatedParameters.x = (int) (x + (event.getRawX() - pressedX));
updatedParameters.y = (int) (y + (event.getRawY() - pressedY));
wm.updateViewLayout(sball, updatedParameters);
default:
break;
}
return false;
}
});
mInview.setOnTouchListener(new View.OnTouchListener() {
WindowManager.LayoutParams updatedParameters = parameters;
double x;
double y;
double pressedX;
double pressedY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
x = updatedParameters.x;
y = updatedParameters.y;
pressedX = event.getRawX();
pressedY = event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
updatedParameters.x = (int) (x + (event.getRawX() - pressedX));
updatedParameters.y = (int) (y + (event.getRawY() - pressedY));
wm.updateViewLayout(mInview, updatedParameters);
default:
break;
}
return false;
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
wm.removeView(mInview);
stopSelf();
System.exit(0);
}
});
}
@Override
public void onDestroy() {
super.onDestroy();
stopSelf();
}
}