【问题标题】:How do I press and drag and leave imageView where i unpressed my finger?[android]如何按下并拖动并将 imageView 留在我未按下手指的位置?[android]
【发布时间】:2015-06-17 11:46:26
【问题描述】:
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) ;
 }

}

但是,代码不起作用,这是我能想到的最接近的。

【问题讨论】:

  • 您找到解决方案了吗?

标签: android view drag-and-drop motionevent


【解决方案1】:

对于 Honeycomb(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()

【讨论】:

  • 我正在使用 api22 - 最新的,我为什么要考虑低于 api 11 的东西?你能提供更相关的建议吗?
猜你喜欢
  • 2021-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-02
  • 1970-01-01
  • 1970-01-01
  • 2019-06-14
  • 1970-01-01
相关资源
最近更新 更多