2015-06-17 17 views
0
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) ; 
} 

}如何按并拖动和离开的ImageView其中i未按我的手指?[机器人]

但是,代码不工作,这是最接近我能想出。

+0

您是否找到了解决方案? – Sree

回答

1

对于任何低于蜂窝(API等级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()

+0

我使用api22 - 最新的,为什么我会考虑下api 11以下的东西?你能提供更多相关的建议吗? – ERJAN