【问题标题】:Android: move images between gridviewsAndroid:在gridviews之间移动图像
【发布时间】:2014-11-21 10:04:26
【问题描述】:

我需要实现这个场景,但我不知道如何处理它:

黑色的矩形是两个gridviews,蓝色的正方形是图像,红色的只是给你一个占位符。

我需要在 gridviews 之间拖动图像以让用户选择 4-5 个图像(它们在红色方块中)......所以从上到下和从下到上拖动(选择和取消选择)。 谁能帮帮我??

【问题讨论】:

    标签: android image gridview drag


    【解决方案1】:

    您可以通过在活动中实现 OnTouchListener 和 OnDragListener 轻松完成。 ex-create shape_droptarget.xml 和 shape.xml 在可绘制文件夹中。我已经为自己的工作实现了它。希望能给你一些帮助。

    shape_droptarget.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle" >
    
        <stroke
            android:width="2dp"
            android:color="#FFFF0000" />
    
        <gradient
            android:angle="225"
            android:endColor="#DD2ECCFA"
            android:startColor="#DD000000" />
    
        <corners
            android:bottomLeftRadius="7dp"
            android:bottomRightRadius="7dp"
            android:topLeftRadius="7dp"
            android:topRightRadius="7dp" />
    
    </shape> 
    

    shape.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle" >
    
        <stroke
            android:width="2dp"
            android:color="#FFFFFFFF" />
    
        <gradient
            android:angle="225"
            android:endColor="#DD2ECCFA"
            android:startColor="#DD000000" />
    
        <corners
            android:bottomLeftRadius="7dp"
            android:bottomRightRadius="7dp"
            android:topLeftRadius="7dp"
            android:topRightRadius="7dp" />
    
    </shape>
    

    在布局文件夹中创建 draglayout.xml 文件

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
      <GridLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:background="@color/darkred"
          android:columnCount="2"
          android:columnWidth="300dp"
          android:orientation="vertical"
          android:rowCount="2"
          android:stretchMode="columnWidth" >
    
        <LinearLayout
            android:id="@+id/topleft"
            android:layout_width="160dp"
            android:layout_height="200dp"
            android:layout_column="0"
            android:layout_row="0"
            android:background="@drawable/shape" >
    
            <ImageView
                android:id="@+id/myimage1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
    
                android:src="@drawable/ic_launcher" />
        </LinearLayout>
    
        <LinearLayout
            android:id="@+id/topright"
            android:layout_width="160dp"
            android:layout_height="200dp"
            android:layout_column="1"
            android:layout_row="0"
            android:background="@drawable/shape" >
    
            <ImageView
                android:id="@+id/myimage2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
    
                android:src="@drawable/ic_launcher" />
        </LinearLayout>
    
        <LinearLayout
            android:id="@+id/bottomleft"
            android:layout_width="160dp"
            android:layout_height="200dp"
            android:layout_column="0"
            android:layout_row="1"
            android:background="@drawable/shape" >
    
            <ImageView
                android:id="@+id/myimage3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_launcher" />
        </LinearLayout>
    
        <LinearLayout
            android:id="@+id/bottomright"
            android:layout_width="160dp"
            android:layout_height="200dp"
            android:layout_column="1"
            android:layout_row="1"
            android:background="@drawable/shape" >
    
            <ImageView
                android:id="@+id/myimage4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
    
                android:src="@drawable/ic_launcher" />
        </LinearLayout>
    
    </GridLayout>
      <View
            android:layout_width="match_parent"
            android:layout_height="100dp"
        >
        </View>
      <GridLayout 
          android:background="@color/darkgreen"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:columnCount="4"
        android:columnWidth="300dp"
        android:orientation="horizontal"
        android:rowCount="1"
        android:stretchMode="columnWidth" >
    
        <LinearLayout
            android:id="@+id/topleft1"
            android:layout_width="160dp"
            android:layout_height="200dp"
            android:layout_column="0"
            android:layout_row="0"
            android:background="@drawable/shape" >
    
            <ImageView
                android:id="@+id/myimage11"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
    
                android:src="@drawable/ic_launcher" />
        </LinearLayout>
    
        <LinearLayout
            android:id="@+id/topright1"
            android:layout_width="160dp"
            android:layout_height="200dp"
            android:layout_column="1"
            android:layout_row="0"
            android:background="@drawable/shape" >
    
            <ImageView
                android:id="@+id/myimage21"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
    
                android:src="@drawable/ic_launcher" />
        </LinearLayout>
    
        <LinearLayout
            android:id="@+id/bottomleft1"
            android:layout_width="160dp"
            android:layout_height="200dp"
            android:layout_column="2"
            android:layout_row="0"
            android:background="@drawable/shape" >
    
            <ImageView
                android:id="@+id/myimage31"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_launcher" />
        </LinearLayout>
    
        <LinearLayout
            android:id="@+id/bottomright1"
            android:layout_width="160dp"
            android:layout_height="200dp"
            android:layout_column="3"
            android:layout_row="0"
            android:background="@drawable/shape" >
    
            <ImageView
                android:id="@+id/myimage41"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
    
                android:src="@drawable/ic_launcher" />
        </LinearLayout>
    
    </GridLayout>
    </LinearLayout>
    

    创建一个活动 DragActivity

    public class DragActivity extends Activity {
    
    
    
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.draglayout);
        findViewById(R.id.myimage1).setOnTouchListener(new MyTouchListener());
        findViewById(R.id.myimage2).setOnTouchListener(new MyTouchListener());
        findViewById(R.id.myimage3).setOnTouchListener(new MyTouchListener());
        findViewById(R.id.myimage4).setOnTouchListener(new MyTouchListener());
        findViewById(R.id.topleft).setOnDragListener(new MyDragListener());
        findViewById(R.id.topright).setOnDragListener(new MyDragListener());
        findViewById(R.id.bottomleft).setOnDragListener(new MyDragListener());
        findViewById(R.id.bottomright).setOnDragListener(new MyDragListener());
    
    
        findViewById(R.id.myimage11).setOnTouchListener(new MyTouchListener());
        findViewById(R.id.myimage21).setOnTouchListener(new MyTouchListener());
        findViewById(R.id.myimage31).setOnTouchListener(new MyTouchListener());
        findViewById(R.id.myimage41).setOnTouchListener(new MyTouchListener());
        findViewById(R.id.topleft1).setOnDragListener(new MyDragListener());
        findViewById(R.id.topright1).setOnDragListener(new MyDragListener());
        findViewById(R.id.bottomleft1).setOnDragListener(new MyDragListener());
        findViewById(R.id.bottomright1).setOnDragListener(new MyDragListener());
      }
    
      private final class MyTouchListener implements OnTouchListener {
        public boolean onTouch(View view, MotionEvent motionEvent) {
          if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            ClipData data = ClipData.newPlainText("", "");
            DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
            view.startDrag(data, shadowBuilder, view, 0);
            view.setVisibility(View.INVISIBLE);
            return true;
          } else {
            return false;
          }
        }
      }
    
      class MyDragListener implements OnDragListener {
        Drawable enterShape = getResources().getDrawable(R.drawable.shape_droptarget);
        Drawable normalShape = getResources().getDrawable(R.drawable.shape);
    
        @Override
        public boolean onDrag(View v, DragEvent event) {
          int action = event.getAction();
          switch (event.getAction()) {
          case DragEvent.ACTION_DRAG_STARTED:
            // do nothing
            break;
          case DragEvent.ACTION_DRAG_ENTERED:
            v.setBackgroundDrawable(enterShape);
            break;
          case DragEvent.ACTION_DRAG_EXITED:
            v.setBackgroundDrawable(normalShape);
            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;
            container.addView(view);
            view.setVisibility(View.VISIBLE);
            break;
          case DragEvent.ACTION_DRAG_ENDED:
            v.setBackgroundDrawable(normalShape);
          default:
            break;
          }
          return true;
        }
      }
    } 
    

    现在您可以将图像从一个网格移动到另一个网格,也可以在网格内移动。

    【讨论】:

    • 是的,但是您会看到,如果我拖动图像,则拖动图像时,图像也会保留在旧位置(如副本)
    • 图像随着你的指针移动,它也随着指针移动。你能解释更多你到底想要做什么。你也可以将图像视图的高度和宽度设置为等于父级,因为你的要求它不应该在自己的容器中移动。最好从 xml 文件中第二个网格的图像视图中删除 android:src。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    相关资源
    最近更新 更多