Android: drag the GridView to the target View

Source: Internet
Author: User

[Java]
Package cn.com. weather. serviceDemo. view;
 
Import cn.com. weather. serviceDemo. interfaces. 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; // start to drag an item.
Private int dragPosition;

// The position to move
Private int dragPointX;
Private int dragPointY;
// The distance from the current position to the Boundary
Private int dragOffsetX;
Private int dragOffsetY;

Private View myView; // target view
Private int dropPosition =-1; // selected

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 (); // the coordinate of the touch point relative to the 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 (); // clear the 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 ();
OnDrop (int) ev. getRawX (), (int) ev. getRawY ());
Break;
 
Case MotionEvent. ACTION_MOVE:
OnDrag (x, y );
Break;
}


}
Return super. onTouchEvent (ev );
}
 
Private void startDrag (Bitmap bm, int x, int y ){
StopDrag ();

WindowParams = new WindowManager. LayoutParams ();
WindowParams. gravity = Gravity. TOP | Gravity. LEFT;
// Coordinates of the upper left corner relative to the screen
WindowParams. x = x-dragPointX + dragOffsetX;
WindowParams. y = y-dragPointY + dragOffsetY;

WindowParams. height = WindowManager. LayoutParams. WRAP_CONTENT;
WindowParams. width = WindowManager. LayoutParams. WRAP_CONTENT;
WindowParams. flags = WindowManager. LayoutParams. FLAG_NOT_FOCUSABLE
| WindowManager. LayoutParams. FLAG_NOT_TOUCHABLE
| WindowManager. LayoutParams. FLAG_KEEP_SCREEN_ON
| WindowManager. LayoutParams. FLAG_LAYOUT_IN_SCREEN;
WindowParams. format = PixelFormat. TRANSLUCENT;
WindowParams. windowAnimations = 0;

ImageView imageView = new ImageView (this. getContext ());
ImageView. setImageBitmap (bm );

WindowManager = (WindowManager) this. getContext (). getSystemService (Context. WINDOW_SERVICE );
WindowManager. addView (imageView, windowParams );
DragImageView = imageView;
}

Private void stopDrag (){
If (dragImageView! = Null ){
WindowManager. removeView (dragImageView );
DragImageView = null;
}
}

Private void onDrag (int x, int y ){
If (dragImageView! = Null ){
WindowParams. alpha = 0.8f;
WindowParams. x = x-dragPointX + dragOffsetX;
WindowParams. y = y-dragPointY + dragOffsetY;
WindowManager. updateViewLayout (dragImageView, windowParams );
}

}

Private void onDrop (int x, int y ){
Boolean inView = isInView (x, y, myView );
If (inView ){
If (onDropListener! = Null ){
OnDropListener. onDrop (dragSrcPosition );
}
}
}

Private Bitmap getScaleBitmap (Bitmap bitmap ){
Matrix matrix = new Matrix ();
Matrix. postScale (1.1f, 1.1f );
Return Bitmap. createBitmap (bitmap, 0, 0, bitmap. getWidth (), bitmap. getHeight (), matrix, true );
}

Private boolean isInView (int x, int y, View view ){
If (view = null) return false;

Int [] location = new int [2];
View. getLocationInWindow (location );
Int _ x = location [0];
Int _ y = location [1];
Int width = view. getWidth ();
Int height = view. getHeight ();

If (_ x <x & x <_ x + width & _ y <y & y <_ y + height ){
Return true;
}
Return false;
}


 
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.