2012-09-27 18 views
7

mã này để kéo màn hình hiện tại lên.di chuyển chậm hoặc nhanh (cuộn) bằng cách sử dụng robotium

int screenWidth = getActivity().getWindowManager().getDefaultDisplay().getWidth(); 
int screenHeight = getActivity().getWindowManager().getDefaultDisplay().getHeight(); 
int fromX, toX, fromY, toY = 0; 
fromX = screenWidth/2; 
toX = screenWidth/2; 
fromY = (screenHeight/2) + (screenHeight/3); 
toY = (screenHeight/2) - (screenHeight/3); 
int scroll_time = 10000;    
solo.sleep(5000); 
    // Drag UP 
solo.drag(fromX, toX, fromY, toY, 40); 
Log.d(TAG, "Drag 1"); 
    // here default origin (x,y = 0,0) is left upper corner 

cuộn ở đây hoạt động nhưng rất chậm.

Vì vậy, để nhanh chóng cuộn những thay đổi nào trong mã này được yêu cầu?

Trả lời

9

tôi phải đối mặt với cùng một vấn đề, những gì bạn cần làm là điều chỉnh dòng mã sau đây,

solo.drag(fromX, toX, fromY, toY, 40); //Change 40 to 10 

này sẽ làm tăng di chuyển của bạn tốc độ, số bước càng thấp, tốc độ di chuyển càng nhanh!

+0

Cảm ơn rất nhiều Royston Pinto, làm việc trên điện thoại của tôi Galaxy Ace tốt. –

0
public class MainActivity 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); 

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

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

thấy cái này nó là sử dụng đầy đủ Android Drag and drop images on the Screen?

+0

tôi sử dụng rô bốt để tự động hóa, ở đây không cần tương tác với người dùng, không yêu cầu setOnTouchListener. Mã trên của tôi đang hoạt động nhưng tôi muốn cuộn nhanh màn hình hiện tại. anyway nhờ NagarjunaReddy cho trả lời của bạn và thử. –

Các vấn đề liên quan