简体   繁体   English

在布局中拖放图像

[英]Drag and Drop Image in Layout

I have 10X4 table with in that some cell have image, need to drag and drop image in side table in cells. 我有10X4表,有些单元格有图像,需要在单元格的边桌中拖放图像。 how to find already cell has image or not? 如何找到已经有细胞的图像?

My Code: for creating cells, drag and drop. 我的代码:用于创建单元格,拖放。

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 code: 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>

suggest your logics..thanks 建议你的逻辑..谢谢

是的,我使用setTag for elements解决了这个问题,同时在Saved List中拖动检查该标签的状态。

use this code 使用此代码

in Activity 在活动中

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 File:- 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>

i would do it like this. 我会这样做的。

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

Also you can override onTouch listener and get position . 您还可以覆盖onTouch侦听器并获取位置。 but use setX() and setY() 但是使用setX()和setY()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM