Android实现拖拽GridView到目标View
[java]package cn.com.weather.serviceDemo.view;
import cn.com.weather.serviceDemo.inte易做图ces.OnDropListener;
import cn.com.weather.serviceDemo.util.DeviceUtil;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
public class MyGridView extends GridView{
private ImageView dragImageView;
private WindowManager windowManager;
private WindowManager.LayoutParams windowParams;
private int dragSrcPosition;//开始拖拽项
private int dragPosition;
//移动的位置
private int dragPointX;
private int dragPointY;
//当前位置距离边界的位置
private int dragOffsetX;
private int dragOffsetY;
private View myView;//目标view
private int dropPosition = -1;//选中项
private OnDropListener onDropListener;
public MyGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setView(View view){
myView = view;
}
public void setOnDropListener(OnDropListener onDropListener){
this.onDropListener = onDropListener;
}
private boolean setOnItemLongClickListener(final MotionEvent ev){
this.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
final int x = (int)ev.getX();//触摸点相对于GridView的坐标
final int y = (int)ev.getY();
dragSrcPosition = dragPosition = position;
if(dragPosition == AdapterView.INVALID_POSITION ){
return false;
}
final ViewGroup item = (ViewGroup)getChildAt(dragPosition - getFirstVisiblePosition());
dragPointX = x - item.getLeft();
dragPointY = y - item.getTop();
dragOffsetX = (int)ev.getRawX() - x;
dragOffsetY = (int)ev.getRawY() - y;
item.destroyDrawingCache();//清除cache
item.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(item.getDrawingCache());
startDrag(getScaleBitmap(bitmap), x, y);
DeviceUtil.Vibrate(getContext(), 100);
return false;
}
});
return super.onInterceptTouchEvent(ev);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if(ev.getAction() == MotionEvent.ACTION_DOWN){
return setOnItemLongClickListener(ev);
}
return super.onInterceptTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if(dragImageView != null && dragPosition != AdapterView.INVALID_POSITION){
int x = (int)ev.getX();
int y = (int)ev.getY();
switch (ev.getAction()) {
case MotionEvent.ACTION_UP:
stopDrag();
&n
补充:移动开发 , Android ,