Android Development--a small ball followed by a finger

Source: Internet
Author: User

Today to achieve is a follow the fingers of the small ball, White is to let the ball in the direction of the finger sliding trajectory movement, the realization is still relatively easy.

The class used is Drawview, we first customize a Drawview component.

Drawview.java:

Package Cn.csdn.android.view;

Import Android.content.Context;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import Android.graphics.Paint;
Import Android.view.View;

public class Drawview extends view{

public float CurrentX = 300;
public float currenty = 500;

Public Drawview (Context context) {
Super (context);
TODO auto-generated Constructor stub
}

@Override
protected void OnDraw (canvas canvas) {
TODO auto-generated Method Stub
Super.ondraw (canvas);
Paint p = new paint ();
P.setcolor (color.red);
Canvas.drawcircle (CurrentX, CurrentY, p);
}


}

==================================

In the Main.xml file, give a android:id= "@+id/root";

Then write the Java code to implement the tracking effect:

Customview.java

Package Cn.csdn.android.view;

Import android.app.Activity;
Import Android.os.Bundle;
Import android.view.MotionEvent;
Import Android.view.View;
Import Android.view.View.OnTouchListener;
Import Android.widget.LinearLayout;

public class Customviewactivity extends activity{

@Override
protected void OnCreate (Bundle savedinstancestate) {
TODO auto-generated Method Stub
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);

LinearLayout main = (linearlayout) Findviewbyid (r.id.root);
Final Drawview draw = new Drawview (this);
Draw.setminimumwidth (300);
Draw.setminimumheight (500);

Draw.setontouchlistener (New Ontouchlistener () {

@Override
public boolean OnTouch (View V, motionevent event) {
Draw.currentx = Event.getx ();
Draw.currenty = Event.gety ();
Draw.invalidate ();
return true;
}
});
Main.addview (Draw);
}

}

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.