The method of implementing the icon dragging effect in Android programming

Source: Internet
Author: User

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.

Related Article

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.