2013-10-08 110 views
0

我想使用drad n drop或其他方式将Image1移动到屏幕上。我尝试了很多样本​​,但无法解决。拖放n Drop或其他东西?

需要帮助,我坚持了很久。 enter image description here

代码

public class DnDActivity extends Activity { 
int windowwidth; 
int windowheight; 
ImageView ima1,ima2; 
int i=0; 

private android.widget.RelativeLayout.LayoutParams layoutParams ;   

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_dn_d); 

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

     ima1 = (ImageView)findViewById(R.id.imageview1); 
     ima1.setOnTouchListener(new View.OnTouchListener() { 

      public boolean onTouch(View v, MotionEvent event) { 
       layoutParams = (RelativeLayout.LayoutParams) ima1.getLayoutParams(); 

       switch(event.getAction())     
       { 
       case MotionEvent.ACTION_DOWN:       
       break; 

       case MotionEvent.ACTION_MOVE: 
       int x_cord = (int) event.getRawX(); 
       int y_cord = (int) event.getRawY(); 

        System.out.println("value of x" +x_cord); 
        System.out.println("value of y" +y_cord);   

       if (x_cord > windowwidth) { 
        x_cord = windowwidth; 
       } 
       if (y_cord > windowheight) { 
        y_cord = windowheight; 
        } 
       // layoutParams.setMargins((windowwidth+x_cord)- windowwidth, windowwidth - (windowwidth-y_cord), (windowwidth+x_cord)- windowwidth, windowwidth - (windowwidth-y_cord)); 
        layoutParams.bottomMargin=y_cord-25; 
        layoutParams.rightMargin=x_cord; 
        ima1.setLayoutParams(layoutParams); 

        break; 
        default: break; 
        } 
        return true; 
      } 
     }); 
} 

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 


<ImageView 
    android:id="@+id/imageview1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentTop="true" 
    android:src="@drawable/ic_launcher" /> 

+0

@RafiKamal:http://polamreddyn.blogspot.in/2013/02/android-drag-and-drop-images-in-one.html –

+0

我试着这个工作很好,但是当我改变ImageView1的位置时。它不会给我预期的答案。 @RafiKamal –

+0

请发布您的完整代码(activity和xml)。 –

回答

1

这样写代码很久以前可能有一定的帮助

package com.dev.draganddropdemo; 

import java.util.ArrayList; 

import android.app.Activity; 
import android.os.Bundle; 
import android.util.DisplayMetrics; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnTouchListener; 
import android.widget.FrameLayout; 
import android.widget.FrameLayout.LayoutParams; 
import android.widget.ImageView; 
import android.widget.ProgressBar; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 

public class DragAndDropActivity extends Activity { 
    public FrameLayout board; 

    int dropZone1_X, dropZone2_X, dropZone3_X, dropZone1_Y, dropZone2_Y, 
      dropZone3_Y, movingCoordinateLeft = 0, movingCoordinateTop = 0; 

    int windowHeight, windowWidth, defaultMargin = 150; 
    ImageView answerOption1, answerOption2, answerOption3, dropZone1, 
      dropZone2, dropZone3; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     board = new FrameLayout(this); 
     setContentView(R.layout.drag_layout); 

     /* 
     * set id's of view objects 
     */ 
     setIds(); 

     /* 
     * set on touch listener 
     */ 
     setOnTouchListener(); 
     /* 
     * get window dimensions 
     */ 
     getWindowDimensions(); 

    } 

    private void setOnTouchListener() { 
     // TODO Auto-generated method stub 
     answerOption1.setOnTouchListener(dragt); 
     answerOption2.setOnTouchListener(dragt); 
     answerOption3.setOnTouchListener(dragt); 

    } 

    private void setIds() { 
     // TODO Auto-generated method stub 

     board = (FrameLayout) findViewById(R.id.Board); 
     // ids for answer options 
     answerOption1 = (ImageView) findViewById(R.id.answer_option_1); 
     answerOption2 = (ImageView) findViewById(R.id.answer_option_2); 
     answerOption3 = (ImageView) findViewById(R.id.answer_option_3); 

     // ids for drop zones 
     dropZone1 = (ImageView) findViewById(R.id.frame1); 
     dropZone2 = (ImageView) findViewById(R.id.frame2); 
     dropZone3 = (ImageView) findViewById(R.id.frame3); 

    } 

    /* 
    * 
    * Get default view dimensions at run time 
    */ 

    @Override 
    public void onWindowFocusChanged(boolean hasFocus) { 
     super.onWindowFocusChanged(hasFocus); 
     if (hasFocus) { 

      System.out.println("Method--onWindowFocusChanged"); 

      System.out.println("\n\nFirst drop zone dimensions"); 
      System.out.println("left margin-->" + dropZone1.getLeft()); 
      System.out.println("top margin-->" + dropZone1.getTop()); 

      System.out.println("\n\nSecond drop zone dimensions"); 
      System.out.println("left margin-->" + dropZone2.getLeft()); 
      System.out.println("top margin-->" + dropZone2.getTop()); 

      System.out.println("\n\nThird drop zone dimensions"); 
      System.out.println("left margin-->" + dropZone3.getLeft()); 
      System.out.println("top margin-->" + dropZone3.getTop()); 

     } 
    } 

    private void getWindowDimensions() { 
     // TODO Auto-generated method stub 
     DisplayMetrics metrics = new DisplayMetrics(); 
     getWindowManager().getDefaultDisplay().getMetrics(metrics); 
     windowHeight = metrics.heightPixels; 
     System.out.println("window height" + windowHeight); 
     windowWidth = metrics.widthPixels; 
     System.out.println("window width" + windowWidth); 

    } 

    // onCreate 
    OnTouchListener dragt = new OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      FrameLayout.LayoutParams par = (LayoutParams) v.getLayoutParams(); 
      switch (v.getId()) {// What is being touched 
      /*** 
      * 
      * Answer option 1 
      * 
      * ***/ 
      case R.id.answer_option_1: { 
       // Which action is being taken 
       switch (event.getAction()) { 
       case MotionEvent.ACTION_MOVE: { 
        par.topMargin = (int) event.getRawY() 
          - (v.getHeight() + 22); 
        par.leftMargin = (int) event.getRawX() 
          - (v.getWidth()/2 + 150); 

        movingCoordinateLeft = (int) event.getRawX() 
          - (v.getWidth()/2 + 0); 
        movingCoordinateTop = par.topMargin; 

        System.out.println("Answer 1 --- left" 
          + movingCoordinateLeft + "---top" 
          + movingCoordinateTop); 

        v.setLayoutParams(par); 

        break; 
       }// inner case MOVE 
//    case MotionEvent.ACTION_UP: { 
//     par.height = 40; 
//     par.width = 40; 
//     /* 
//     * par.topMargin = (int) event.getRawY() - (v.getHeight() + 
//     * 15); par.leftMargin = (int) event.getRawX() - 
//     * (v.getWidth()/2 + 90); 
//     */ 
// 
//     if (windowHeight < 460) { 
//      par.topMargin = 109; 
//      par.leftMargin = 0; 
//      par.height = 22; 
//      par.width = 105; 
// 
//     } else { 
//      par.topMargin = defaultMargin; 
//      par.leftMargin = 0; 
//     } 
// 
//     // check if co-ordinates matched and drop answer in drop 
//     // zone 
//     if ((movingCoordinateLeft > 10 && movingCoordinateLeft < 80) 
//       && (movingCoordinateTop > 10 && movingCoordinateTop < 100)) { 
// 
//      System.out.println("left " + movingCoordinateLeft 
//        + "top " + movingCoordinateTop); 
// 
//      dropZone1.setImageDrawable(getResources().getDrawable(
//        R.drawable.duck)); 
//      answerOption1.setVisibility(View.INVISIBLE); 
// 
//     } 
// 
//     v.setLayoutParams(par); 
//     break; 
//    }// inner case UP 
       case MotionEvent.ACTION_DOWN: { 

        System.out.println("left" + event.getRawX()); 
        System.out.println("top" + event.getRawY()); 

        if (windowHeight < 460) { 

         par.height = 40; 
         par.width = 40; 

        } else { 
         par.height = 40; 
         par.width = 40; 
        } 

        v.setLayoutParams(par); 
        break; 
       }// inner case UP 
       }// inner switch 
       break; 
      }// case pawn 

      /*** 
      * 
      * Answer option 2 
      * 
      * ***/ 

      case R.id.answer_option_2: {// Which action is being taken 
       switch (event.getAction()) { 
       case MotionEvent.ACTION_MOVE: { 

        par.topMargin = (int) event.getRawY() 
          - (v.getHeight() + 22); 
        par.leftMargin = (int) event.getRawX() 
          - (v.getWidth()/2 + 150); 

        movingCoordinateLeft = (int) event.getRawX() 
          - (v.getWidth()/2 + 0); 
        movingCoordinateTop = par.topMargin; 

        v.setLayoutParams(par); 

        break; 
       }// inner case MOVE 
       case MotionEvent.ACTION_UP: { 
        par.height = 40; 
        par.width = 40; 
        /* 
        * par.topMargin = (int) event.getRawY() - (v.getHeight() + 
        * 15); par.leftMargin = (int) event.getRawX() - 
        * (v.getWidth()/2 + 90); 
        */ 

        if (windowHeight < 460) { 
         par.topMargin = 150; 
         par.leftMargin = 0; 
         par.height = 40; 
         par.width = 40; 

        } else { 
         par.topMargin = 200; 
         par.leftMargin = 0; 
        } 


        // check if co-ordinates matched and drop answer in drop 
        // zone 
        if ((movingCoordinateLeft > 120 && movingCoordinateLeft < 200) 
          && (movingCoordinateTop > 10 && movingCoordinateTop < 100)) { 

         System.out.println("left " + movingCoordinateLeft 
           + "top " + movingCoordinateTop); 

         dropZone2.setImageDrawable(getResources().getDrawable(
           R.drawable.hen)); 
         answerOption2.setVisibility(View.INVISIBLE); 
        } 

        v.setLayoutParams(par); 

        break; 
       }// inner case UP 
       case MotionEvent.ACTION_DOWN: { 

        if (windowHeight < 460) { 

         par.height = 40; 
         par.width = 40; 

        } else { 
         par.height = 40; 
         par.width = 40; 
        } 

        v.setLayoutParams(par); 
        break; 
       }// inner case UP 
       }// inner switch 
       break; 
      }// case pawn2 

      /*** 
      * 
      * Answer option 3 
      * 
      * ***/ 

      case R.id.answer_option_3: {// Which action is being taken 
       switch (event.getAction()) { 
       case MotionEvent.ACTION_MOVE: { 

        par.topMargin = (int) event.getRawY() 
          - (v.getHeight() + 22); 
        par.leftMargin = (int) event.getRawX() 
          - (v.getWidth()/2 + 150); 

        movingCoordinateLeft = (int) event.getRawX() 
          - (v.getWidth()/2 + 0); 
        movingCoordinateTop = par.topMargin; 


        v.setLayoutParams(par); 

        break; 
       }// inner case MOVE 
       case MotionEvent.ACTION_UP: { 
        par.height = 40; 
        par.width = 40; 
        /* 
        * par.topMargin = (int) event.getRawY() - (v.getHeight() + 
        * 15); par.leftMargin = (int) event.getRawX() - 
        * (v.getWidth()/2 + 90); 
        */ 

        if (windowHeight < 460) { 
         par.topMargin = 191; 
         par.leftMargin = 0; 
         par.height = 40; 
         par.width = 40; 

        } else { 
         par.topMargin = 250; 
         par.leftMargin = 0; 
        } 

        // check if co-ordinates matched and drop answer in drop 
        // zone 
        if ((movingCoordinateLeft > 220 && movingCoordinateLeft < 310) 
          && (movingCoordinateTop > 10 && movingCoordinateTop < 100)) { 

         System.out.println("left " + movingCoordinateLeft 
           + "top " + movingCoordinateTop); 

         dropZone3.setImageDrawable(getResources().getDrawable(
           R.drawable.queen)); 
         answerOption3.setVisibility(View.INVISIBLE); 
        } 
        v.setLayoutParams(par); 

        break; 
       }// inner case UP 
       case MotionEvent.ACTION_DOWN: { 
        System.out.println("down"); 
        if (windowHeight < 460) { 

         par.height = 40; 
         par.width = 40; 

        } else { 
         par.height = 40; 
         par.width = 40; 
        } 
        v.setLayoutParams(par); 
        break; 
       }// inner case UP 
       }// inner switch 
       break; 
      }// case pawn2 

      }// switch 
      return true; 
     }// onTouch 

    };// dragt 

} 

而drag_layout

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/rl" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:gravity="center_horizontal" > 

     <ImageView 
      android:id="@+id/frame3" 
      android:layout_width="100dp" 
      android:layout_height="80dp" 
      android:layout_alignParentRight="true" 
      android:src="@drawable/drag_drop_button" /> 

     <ImageView 
      android:id="@+id/frame2" 
      android:layout_width="100dp" 
      android:layout_height="80dp" 
      android:layout_toLeftOf="@+id/frame3" 
      android:src="@drawable/drag_drop_button" /> 

     <ImageView 
      android:id="@+id/frame1" 
      android:layout_width="100dp" 
      android:layout_height="80dp" 
      android:layout_toLeftOf="@+id/frame2" 
      android:src="@drawable/drag_drop_button" /> 
    </RelativeLayout> 

    <FrameLayout 
     android:id="@+id/Board" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_gravity="center" > 

     <ImageView 
      android:id="@+id/answer_option_1" 
      android:layout_width="40dp" 
      android:layout_height="40dp" 
      android:layout_gravity="center_horizontal" 
      android:layout_marginTop="150dp" 
      android:background="@drawable/duck" > 
     </ImageView> 

     <ImageView 
      android:id="@+id/answer_option_2" 
      android:layout_width="40dp" 
      android:layout_height="40dp" 
      android:layout_below="@+id/answer_option_1" 
      android:layout_gravity="center_horizontal" 
      android:layout_marginTop="200dp" 
      android:background="@drawable/hen" > 
     </ImageView> 

     <ImageView 
      android:id="@+id/answer_option_3" 
      android:layout_width="40dp" 
      android:layout_height="40dp" 
      android:layout_gravity="center_horizontal" 
      android:layout_marginTop="250dp" 
      android:background="@drawable/queen" > 
     </ImageView> 
    </FrameLayout> 

</RelativeLayout>