Android programming implementation of the icon dragging effect method
Android programming implementation of the icon dragging effect method
This article describes how to implement the icon dragging effect in Android programming. We will share this with you for your reference. The details are as follows:
The speed of the recently optimized icon dragging has a little effect. You can directly paste the Code. If you are interested in discussing it together, you can leave a message for me.
The Code is as follows:
DragView. java
Package com. android. dragtest;
Import android. content. Context;
Import android. util. AttributeSet;
Import android. util. Log;
Import android. view. MotionEvent;
Import android. view. View;
Import android. widget. FrameLayout;
Public class DragView extends FrameLayout {
Private static final String TAG = "DragView ";
Private float X;
Private float Y;
Private View mDragView;
Public DragView (Context context ){
This (context, null );
}
Public DragView (Context context, AttributeSet attrs ){
This (context, attrs, 0 );
}
Public DragView (Context context, AttributeSet attrs, int defStyle ){
Super (context, attrs, defStyle );
MDragView = new View (context );
MDragView. setLayoutParams (new LayoutParams (60, 60 ));
MDragView. setBackgroundDrawable (getResources (). getDrawable (R. drawable. gamecenter ));
MDragView. setVisibility (View. INVISIBLE );
AddView (mDragView );
}
Public boolean onInterceptTouchEvent (MotionEvent ev ){
Final int action = ev. getAction ();
Switch (action ){
Case MotionEvent. ACTION_DOWN:
Log. d (TAG, "====================> onInterceptTouchEvent ACTION_DOWN ");
Break;
Case MotionEvent. ACTION_MOVE:
Log. d (TAG, "====================> onInterceptTouchEvent ACTION_MOVE ");
Break;
Case MotionEvent. ACTION_UP:
Log. d (TAG, "====================> onInterceptTouchEvent ACTION_UP ");
Break;
}
Return true;
}
Public boolean onTouchEvent (MotionEvent ev ){
Final int action = ev. getAction ();
X = ev. getX ();
Y = ev. getY ();
Switch (action ){
Case MotionEvent. ACTION_DOWN:
Log. d (TAG, "onTouchEvent ACTION_DOWN ");
MDragView. layout (int) X-30, (int) Y-30, (int) X + 30, (int) Y + 30 );
MDragView. setVisibility (View. VISIBLE );
Break;
Case MotionEvent. ACTION_MOVE:
Log. d (TAG, "onTouchEvent ACTION_MOVE x:" + X + "Y:" + Y );
MDragView. layout (int) X-30, (int) Y-30, (int) X + 30, (int) Y + 30 );
Break;
Case MotionEvent. ACTION_UP:
Log. d (TAG, "onTouchEvent ACTION_UP ");
MDragView. setVisibility (View. INVISIBLE );
Break;
}
Return true;
}
}
DragTestActivity. java
Package com. android. dragtest;
Import android. app. Activity;
Import android. OS. Bundle;
Public class DragTestActivity extends Activity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
}
}
Main. xml
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
Android: layout_width = "match_parent"
Android: layout_height = "match_parent">
I hope this article will help you with Android program design.