我有一个 ViewGroup,当用户长按到时,我想用手指在屏幕上移动它circleMenu
。我该如何移动ViewGroup
?
这是java:
public class ViewService extends Service implements OnItemSelectedListener,
OnItemClickListener, OnRotationFinishedListener, OnCenterClickListener, OnLongClickListener {
private WindowManager windowManager;
CircleLayout circleMenu;
CircleImageView ciw1, ciw2, ciw3, ciw4;
@Override public IBinder onBind(Intent intent) {
// Not used
return null;
}
@Override public void onCreate() {
super.onCreate();
//windowsmanager
windowManager = (WindowManager) getSystemService("window");
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT
);
params.gravity = Gravity.CENTER | Gravity.LEFT;
params.x = 0;
params.y = 0;
//layout+circleMenu
LayoutInflater li = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View baseview = li.inflate(R.layout.activity_main, null);
circleMenu = (CircleLayout) baseview.findViewById(R.id.main_circle_layout);
ciw1 = new CircleImageView(this);
ciw1.setBackgroundResource(R.drawable.twitter);
ciw2 = new CircleImageView(this);
ciw2.setBackgroundResource(R.drawable.whatsapp);
ciw3 = new CircleImageView(this);
ciw3.setBackgroundResource(R.drawable.cam);
ciw4 = new CircleImageView(this);
ciw4.setBackgroundResource(R.drawable.sms);
ciw1.setId(1);
ciw2.setId(2);
ciw3.setId(3);
ciw4.setId(4);
circleMenu.addView(ciw1);
circleMenu.addView(ciw2);
circleMenu.addView(ciw3);
circleMenu.addView(ciw4);
windowManager.addView(baseview, params);
}
@Override
public boolean onLongClick(View v) {
return true;
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onItemSelected(View view, String name) {
switch (view.getId()) {
}
}
@Override
public void onItemClick(View view, String name) {
switch (view.getId()) {
case 1:
Toast.makeText(getApplicationContext(), "twit", Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(getApplicationContext(), "what", Toast.LENGTH_SHORT).show();
break;
case 3:
Toast.makeText(getApplicationContext(), "cam", Toast.LENGTH_SHORT).show();
break;
case 4:
Toast.makeText(getApplicationContext(), "sms", Toast.LENGTH_SHORT).show();
break;
}
}
@Override
public void onCenterClick() {
Toast.makeText(getApplicationContext(), R.string.center_click,
Toast.LENGTH_SHORT).show();
}
@Override
public void onRotationFinished(View view, String name) {
Animation animation = new RotateAnimation(0, 360, view.getWidth() / 2,
view.getHeight() / 2);
animation.setDuration(250);
view.startAnimation(animation);
}
}
这是 XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:circle="http://schemas.android.com/apk/res/lazy.launcher"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" ">
<package.ViewGroup.CircleLayout
android:id="@+id/main_circle_layout"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginLeft="-80dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
circle:firstChildPosition="East"
circle:isRotating="true"
circle:rotateToCenter="true"
circle:speed="75" >
</package.ViewGroup.CircleLayout>
<ImageView
android:id="@+id/toggle"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignParentLeft="true"
android:layout_marginTop="80dp"
android:gravity="center"
android:src="@drawable/ic_launcher" />