如何按下并拖动图像并将我的手指放在哪里?[android]

时间:2015-06-17 11:46:26

标签: android view drag-and-drop motionevent

ImageView currentImage = (ImageView) findViewById(R.id.image) ;  
onTouch(View v, MotionEvent event){
 if(event.getAction() == MotionEvent.ACTION_DOWN){
    float x = event.getX() ;
    float y = event.getY();
    currentImage.setX(x) ;
    currentImage.setY(y) ;
    //the 2 lines above must be dragging the image across the screen
 }


 if( event.getAction() == MotionEvent.ACTION_UP){
     currentImage.setX(x) ;
     currentImage.setY(y) ;
 }

}

然而,代码不起作用,这是我能提出的最接近的代码。

1 个答案:

答案 0 :(得分:1)

对于Honeycomb(API Level 11)以下的任何内容,您必须使用setLayoutParams(): -

如果superLayout是RelativeLayout -

 RelativeLayout.LayoutParams layoutParams=new RelativeLayout.LayoutParams(int width, int height);
    layoutParams.setMargins(int left, int top, int right, int bottom);
    imageView.setLayoutParams(layoutParams);

如果superLayout是Linearlayout -

 Linearlayout.LayoutParams layoutParams=new Linearlayout.LayoutParams(int width, int height);
    layoutParams.setMargins(int left, int top, int right, int bottom);
    imageView.setLayoutParams(layoutParams);

如果你限制对Honeycomb的支持,你可以使用setX(), setY(), setLeft(), setTop()