This article illustrates the method of implementing the icon dragging effect in Android programming. Share to everyone for your reference, specific as follows:
The most recent optimization icon drag speed, a little bit of effect, directly to the code posted, interested in a discussion of friends can give me a message.
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 {This (context, NULL);
Public Dragview (context, AttributeSet attrs) {This (context, attrs, 0);
Public Dragview (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 +, (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 +, (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;
The public class Dragtestactivity extends activity {/** called the ' when the ' is the ' The activity ' is a
./
@Override
p ublic void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
}
Main.xml
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:layout_width=" fill_parent "
android:layout_height=" fill_parent "
android:o" rientation= "vertical" >
<com.android.dragtest.dragview
android:layout_width= "Match_parent"
android:layout_height= "Match_parent" >
</com.android.dragtest.DragView>
</ Linearlayout>
I hope this article will help you with the Android program.