1

嗨,我是 android 新手,我使用 android 2.2 api level-8.so 我可以使用两个图像,一个用于拖动目的,另一个用于 Drop 目的。在这里我显示一些我可以在这里做的代码。代码是哪个显示设备高度宽度和球是我拖动的图像视图。

windowwidth = getWindowManager().getDefaultDisplay().getWidth();
        windowheight = getWindowManager().getDefaultDisplay().getHeight();
        Log.e("Screen Width-->", "" + windowwidth);
        Log.e("Screen Height-->", "" + windowheight);
        ball = (ImageView) findViewById(R.id.secondImage);
        tempAnimationDrawable = (AnimationDrawable) ball.getDrawable();
        ball.setOnTouchListener(this);

在此应用 seton touch Listener。on touchListener 如下。

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

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

        case MotionEvent.ACTION_MOVE:

            int imgWidth = ball.getWidth();
            Log.e("Image Width -->", "" + imgWidth);
            int imgHeight = ball.getHeight();
            Log.e("Image Height -->", "" + imgHeight);

            int x_cord = (int) event.getRawX();
            System.err.println("Display X code->" + x_cord);
            int y_cord = (int) event.getRawY();
            System.err.println("Display y code->" + y_cord);


            int getX=(int) event.getX();
            System.err.println("Display Here X Value -->"+getX);            
            int getY=(int) event.getY();
            System.err.println("Display Here Y Value -->"+getY);

            if (x_cord < windowwidth && getX < imgWidth ) 
            {               
                x_cord = windowwidth;
                Log.e("If Part X Cord-->", "" + x_cord);
            }
            if (y_cord > windowheight) 
            {
                y_cord = windowheight;
                Log.e("If Part Y Cord-->", "" + y_cord);
            }
            layoutParams.setMargins(imgWidth + incLeft, imgHeight + incTop, imgWidth + incRight, imgHeight + incBottom);
            // layoutParams.setMargins(left, top, right, bottom)
            ball.setLayoutParams(layoutParams);
            break;

        default:
            break;

        }
        return true;
    }

现在我该怎么办。

4

2 回答 2

0
   import android.app.Activity;
   import android.graphics.Point;
   import android.os.Bundle;
   import android.view.Display;
   import android.view.MotionEvent;
   import android.view.View;
   import android.widget.ImageView;
   import android.widget.RelativeLayout;

 public class DragMainActivity extends Activity  {
     int windowwidth;
     int windowheight;      
     ImageView ima1,ima2;

     private android.widget.RelativeLayout.LayoutParams layoutParams ;
    // private android.widget.RelativeLayout.LayoutParams layoutParams ;
     //private android.widget.RelativeLayout.LayoutParams layoutParams ;         

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

        /* Display display = getWindowManager().getDefaultDisplay(); 
         int width = display.getWidth();
         int height = display.getHeight();*/


         windowwidth = getWindowManager().getDefaultDisplay().getWidth()-130; 
         windowheight = getWindowManager().getDefaultDisplay().getHeight()-165;

         System.out.println("width" +windowwidth);
         System.out.println("height" +windowheight);             

         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_CANCEL:
                         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.leftMargin = x_cord-25;
                         layoutParams.topMargin = y_cord-25;
                  //       layoutParams.rightMargin = x_cord-25;
                   //      layoutParams.bottomMargin = y_cord-25;
                         ima1.setLayoutParams(layoutParams);
                         break;
                     default: break;
                 }  
                 return true;
             }
         });

         ima2 = (ImageView)findViewById(R.id.imageview2);
         ima2.setOnTouchListener(new View.OnTouchListener() {         

             public boolean onTouch(View v, MotionEvent event) {
                 layoutParams = (RelativeLayout.LayoutParams) ima2.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();

                         System.out.println("value of x1" +x_cord);
                         System.out.println("value of y1" +y_cord);                          

                         if (x_cord > windowwidth) {
                             x_cord = windowwidth;
                         }
                         if (y_cord > windowheight) {
                             y_cord = windowheight;
                         }
                         layoutParams.leftMargin = x_cord - 25;
                         layoutParams.topMargin = y_cord - 75;
                         ima2.setLayoutParams(layoutParams);
                         break;
                     default: break;
                 }
                 return true;
             }
         });
     }
 }
于 2012-08-03T04:57:28.980 回答
0

嗨,我在不使用表面视图的情况下为您提供了 android 2.2 中的整个拖放代码。我在这里给出链接。去那里查看代码并实现它。我相信它会对你有所帮助。

链接是 如何在 android 2.2 中实现拖放?

另一个是 http://code.google.com/p/android-drag-and-drop-basic/source/browse/src/edu/sbcc/cs123/draganddropbasic/DragAndDropBasicActivity.java

于 2012-08-03T04:58:36.323 回答