2012-08-03 58 views
1

嗨,我是新的Android我使用android 2.2 api level-8.so我可以使用兩個圖像一個用於拖拽目的,另一個用於放置目的。 在這裏我顯示一些我可以在這裏做的代碼。 代碼顯示設備高度寬度和球是我的拖動圖像視圖。如何在android 2.2中使用拖拽使用surfaceview?

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監聽器。 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; 
    } 

現在我該怎麼做。

回答

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; 
      } 
     }); 
    } 
}