//该可拖动view主要是有两个imageview,一个用来隐藏该view,一个用来做正常的点击事件。
public DragView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.dragview, this);
iv_close = view.findViewById(R.id.iv_close);
iv_pic = view.findViewById(R.id.iv_pic);
}
主要想法是判断ACTION_DOWN手指按下时,按下的位置是否是关闭的那个imageview
//判断是否触摸view
private boolean isTouchInView(ImageView view, float xAxis, float yAxis) {
if (view== null) {
return false;
}
int[] location = new int[2];
view.getLocationOnScreen(location);
int left = location[0];
int top = location[1];
int right = left + view.getMeasuredWidth();
int bottom = top + view.getMeasuredHeight();
if (yAxis >= top && yAxis <= bottom && xAxis >= left
&& xAxis <= right) {
return true;
}

本文介绍如何在Android中创建一个可拖动的自定义View。这个View包含两个ImageView,一个用于隐藏视图,另一个处理正常点击事件。通过触摸事件拦截,实现视图跟随手指移动。同时提供了在布局中使用、自定义接口以及在Activity中应用的方法,并展示了关闭ImageView被点击时的效果。源码可供参考,是Android开发中的一个实用技巧记录。
1万+

被折叠的 条评论
为什么被折叠?



