在RelativeLayout布局中拖拉图片

@Override
 public boolean onTouchEvent(MotionEvent event) {
  float x = event.getX();
  float y = event.getY();
  try {
   switch (event.getAction()) {
   case MotionEvent.ACTION_DOWN:
    picMove(x, y);
    break;
   case MotionEvent.ACTION_MOVE:
    picMove(x, y);
    break;
   case MotionEvent.ACTION_UP:
    picMove(x, y);
    break;
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  return true;

 }

 private void picMove(float x, float y) {
  float mx, my;
  int intWidth = weatherImage.getWidth();
  int intHeight = weatherImage.getHeight();
  mx = -x + (intWidth / 2);
  my = -y + (intHeight / 2);
  RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) weatherImage
    .getLayoutParams();
  params.setMargins((int) -mx, (int) -my, 0, 0);//关键点
  weatherImage.setLayoutParams(params);
 }

 

 

xml:

<ImageView
        android:id="@+id/weather"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="450dip" />

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/oMartin1/article/details/46787237
个人分类: Android
上一篇直接跳出软键盘
下一篇获得view在当前窗口的绝对位置
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭