import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
import android.view.MotionEvent;
/**
* Created by hq on 2017/10/10.
* 参考:http://blog.csdn.net/zane_xiao/article/details/51188867
*/
public class DragImageView extends AppCompatImageView {
String TAG = "DragImageView";
public DragImageView(Context context) {
this(context, null);
}
public DragImageView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public DragImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
/**
* 设置在父布局中的边界
* @param l
* @param t
* @param r
* @param b
*/
public void initEdge(int l,int t,int r,int b) {
edgeLeft = l;
edgeTop = t;
edgeRight = r;
edgeBottom = b;
}
int edgeLeft, edgeTop, edgeRight, edgeBottom;
int lastX, lastY, movex, movey, dx, dy;
@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 < edgeLeft) {
left = edgeLeft;
right = left + getWidth();
}
if (right > edgeRight) {
right = edgeRight;
left = right - getWidth();
}
if (top < edgeTop) {
top = edgeTop;
bottom = top + getHeight();
}
if (bottom > edgeBottom) {
bottom = edgeBottom;
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);
}
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/df_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.windfindtech.ishanghai.view.SwipeScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/default_white"
android:scrollbars="none">
<RelativeLayout
android:id="@+id/network_tab_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/default_white">
...........
</RelativeLayout>
</com.windfindtech.ishanghai.view.SwipeScrollView>
<com.windfindtech.ishanghai.view.DragImageView
android:id="@+id/iv_drag_adver"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="right|top"
android:src="@drawable/ic_launcher" />
</FrameLayout>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有