0

我有一个 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" />

4

2 回答 2

0

查看作为 Android API 演示一部分的“视图/拖放”演示。看看DraggableDot.java。您可以结合使用现有的和View.setTranslationx()/View.setTranslationY()

于 2014-05-27T19:22:15.487 回答
0

试试这个在RelativeLayout地方 a里面FrameLayout并使用OnTouchListener来移动那个View

xml代码

<FrameLayout
      android:id="@+id/imageHolder"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_alignParentTop="true"
      android:layout_marginLeft="-80dp" >

<package.ViewGroup.CircleLayout
    android:id="@+id/main_circle_layout"
    android:layout_width="200dp"
    android:layout_height="200dp"
    circle:firstChildPosition="East"
    circle:isRotating="true"
    circle:rotateToCenter="true"
    circle:speed="75" >

</package.ViewGroup.CircleLayout>
</FrameLayout>

爪哇代码

circleLayout.setOnTouchListener(new View.OnTouchListener() {
            private int left, top;
            private int parentLeft, parentTop;

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        parentLeft = imageHolder.getLeft();
                        parentTop = ((ViewGroup) imageHolder.getParent()).getTop();
                        left = (int) event.getRawX() - v.getLeft() - parentLeft;
                        top = (int) event.getRawY() - v.getTop() - parentTop;
                        break;
                    case MotionEvent.ACTION_MOVE:
                        int leftMargin = (int) (event.getRawX() - left - parentLeft);
                        int topMargin = (int) (event.getRawY() - top - parentTop);

                        FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) v.getLayoutParams();
                        if (leftMargin >= 0 && leftMargin + v.getWidth() <= imageHolder.getWidth())
                            params.leftMargin = leftMargin;
                        if (topMargin >= 0 && topMargin + v.getHeight() <= imageHolder.getHeight())
                            params.topMargin = topMargin;

                        params.gravity = Gravity.NO_GRAVITY;
                        v.setLayoutParams(params);

                        break;
                }

                return true;
            }
        });
于 2014-05-27T19:23:12.427 回答