您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要介绍怎么使用Android实现图片浮动随意拖拽效果,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
本文实例为大家分享了Android实现图片浮动拖拽效果的具体代码,供大家参考,具体内容如下
实现步骤
1.先自定义一个浮动工具类
public class MoveImage extends ImageView { /** * * 浮动工具类 * */ private int lastX = 0; private int lastY = 0; private int dx; private int dy; private float movex = 0; private float movey = 0; private int screenWidth; private int screenHeight; public MoveImage(Context context, AttributeSet attrs) { super(context, attrs); screenWidth = ScreenUtils.getWidth(context); screenHeight = ScreenUtils.getHeight(context); } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: lastX = (int) event.getRawX(); lastY = (int) event.getRawY(); movex = lastX; movey = lastY; break; case MotionEvent.ACTION_MOVE: dx = (int) event.getRawX() - lastX; dy = (int) event.getRawY() - lastY; int left = getLeft() + dx; int top = getTop() + dy; int right = getRight() + dx; int bottom = getBottom() + dy; if (left < 0) { left = 0; right = left + getWidth(); } if (right > screenWidth) { right = screenWidth; left = right - getWidth(); } if (top < 0) { top = 0; bottom = top + getHeight(); } if (bottom > screenHeight) { bottom = screenHeight; top = bottom - getHeight(); } layout(left, top, right, bottom); lastX = (int) event.getRawX(); lastY = (int) event.getRawY(); break; case MotionEvent.ACTION_UP: //避免滑出触发点击事件 if ((int) (event.getRawX() - movex) != 0 || (int) (event.getRawY() - movey) != 0) { return true; } break; default: break; } return super.onTouchEvent(event); } }
2.xml布局的引用
<com.zjtd.bzcommunity.text.MoveImage android:id="@+id/iv_phone_bar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_marginBottom="70dp" android:src="@drawable/my_qiandao_icon" />
3.activity的实现
private MoveImage iv_phone_bar;//签到图片 iv_phone_bar = (MoveImage) view.findViewById(R.id.iv_phone_bar); iv_phone_bar.setOnClickListener(this); // 签到 case R.id.iv_phone_bar: startActivity(new Intent(getActivity(), SignAcitvity.class));//点击跳转页面 break;
以上是“怎么使用Android实现图片浮动随意拖拽效果”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。