在布局中拖放图像

时间:2015-12-18 10:39:20

标签: android android-layout android-fragments

我有10X4表,其中有些单元格有图像,需要在单元格的边桌中拖放图像。如何找到已经有细胞的图像?

我的代码:用于创建单元格,拖放。

public class WhiteFragment extends Fragment {
    View mView;
    private TableLayout Table1;
    ImageView img;


    int[] imageArray = {R.drawable.a, R.drawable.a, R.drawable.a, R.drawable.a};

    LinearLayout layout;
    View parentView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_white, container, false);

        Table1 = (TableLayout) mView.findViewById(R.id.table);
        for (int i = 0; i < 4; i++) {
            TableRow TR = new TableRow(getActivity());

            for (int j = 0; j < 10; j++) {
                LayoutInflater layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View row = layoutInflater.inflate(R.layout.table_image, null, false);
                img = (ImageView) row.findViewById(R.id.ivImage);
                layout=(LinearLayout) row.findViewById(R.id.layout);

                if(j%2==0)
                img.setImageResource(imageArray[i]);
                layout.setOnTouchListener(new MyTouchListener());
                layout.setOnDragListener(new MyDragListener());

                TR.addView(row);

            }
            Table1.addView(TR);
        }
        return mView;
    }

    float x, y = 0.0f;
    boolean ismoving;

    private final class MyTouchListener implements View.OnTouchListener {
        public boolean onTouch(View view, MotionEvent motionEvent) {

           switch (motionEvent.getAction()){
               case MotionEvent.ACTION_DOWN:
                   Log.e("owner status","down");
                   break;
               case MotionEvent.ACTION_MOVE:
                   Log.e("owner status","move");
                   ClipData data = ClipData.newPlainText("", "");
                   View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
                   view.startDrag(data, shadowBuilder, view, 0);
                   view.setVisibility(View.INVISIBLE);

                   break;
               case MotionEvent.ACTION_UP:
                   Log.e("owner status","up");
                   break;
           }
           return true;
        }
    }

    class MyDragListener implements View.OnDragListener {

        @Override
        public boolean onDrag(View v, DragEvent event) {
            int action = event.getAction();
            switch (event.getAction()) {
                case DragEvent.ACTION_DRAG_STARTED:
                    break;
                case DragEvent.ACTION_DRAG_ENTERED:
                    break;
                case DragEvent.ACTION_DRAG_EXITED:
                    break;
                case DragEvent.ACTION_DROP:
                    // Dropped, reassign View to ViewGroup
                    View view = (View) event.getLocalState();
                    ViewGroup owner = (ViewGroup) view.getParent();
                    owner.removeView(view);
                    LinearLayout container = (LinearLayout) v;

                    if(owner.getChildCount()==0)
                        container.addView(view);
                   else owner.addView(view);
                    view.setVisibility(View.VISIBLE);
                    break;
                case DragEvent.ACTION_DRAG_ENDED:
                default:
                    break;
            }
            return true;
        }
    }
}

xml代码:

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/table_border">
       <LinearLayout
           android:id="@+id/layout"
           android:layout_width="120dp"
           android:layout_height="120dp">
        <ImageView
            android:id="@+id/ivImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dp" />
       </LinearLayout>
    </LinearLayout>

建议你的逻辑......谢谢

2 个答案:

答案 0 :(得分:0)

使用此代码

在活动中

windowwidth = getWindowManager().getDefaultDisplay().getWidth();
windowheight = getWindowManager().getDefaultDisplay().getHeight();


tv1 = (ImageView)findViewById(R.id.image);
tv1.setOnTouchListener(new View.OnTouchListener() {         

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        layoutParams1 = (RelativeLayout.LayoutParams) tv1.getLayoutParams();
        switch(event.getActionMasked())
        {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                int x_cord = (int) event.getRawX();
                int y_cord = (int) event.getRawY();
                if (x_cord > windowwidth) {
                    x_cord = windowwidth;
                }
                if (y_cord > windowheight) {
                    y_cord = windowheight;
                }
                layoutParams1.leftMargin = x_cord - 25;
                layoutParams1.topMargin = y_cord - 75;
                tv1.setLayoutParams(layoutParams1);
                break;
            default:
                break;
        }
        return true;
    }
});

tv2 = (ImageView)findViewById(R.id.image1);
tv2.setOnTouchListener(new View.OnTouchListener() {         

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        layoutParams2 = (RelativeLayout.LayoutParams) tv2.getLayoutParams();
        switch(event.getActionMasked())
        {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                int x_cord = (int) event.getRawX();
                int y_cord = (int) event.getRawY();
                if (x_cord > windowwidth) {
                    x_cord = windowwidth;
                }
                if (y_cord > windowheight) {
                    y_cord = windowheight;
                }
                layoutParams2.leftMargin = x_cord - 25;
                layoutParams2.topMargin = y_cord - 75;
                tv2.setLayoutParams(layoutParams2);
                break;
            default:
                break;
        }
        return true;
    }
});

XML文件: -

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ImageView android:layout_width="50sp" android:layout_height="50sp"
        android:id="@+id/image" android:src="@drawable/image">
    </ImageView>
    <ImageView android:layout_y="30dip" android:layout_x="118dip"
        android:layout_width="50sp" android:layout_height="50sp" android:id="@+id/image1"
        android:src="@drawable/image1">
    </ImageView>
</RelativeLayout>

答案 1 :(得分:0)

我会这样做。

   int[] newLocation = new int[2];
    button.getLocationOnScreen(newLocation);
    int x = newLocation[0];
    int y = newLocation[1];
    imageView.setX(x);
    imageView.setY(y);

此外,您可以覆盖onTouch侦听器并获取位置。 但使用setX()和setY()