the method of implementing the icon dragging effect in Android programming
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 is (context, NULL);
}
Public Dragview (context context, AttributeSet Attrs) {
This is (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 +, (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;
public class Dragtestactivity extends activity {
/** called the activity is a. */
@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 the Android program.