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);
}
}