The ball that follows the finger.
Crize_demo \ draw_demo \ src \ main \ java \ com \ ly \ draw_demo \ DrawView. java
1 import android. content. context; 2 import android. graphics. canvas; 3 import android. graphics. color; 4 import android. graphics. paint; 5 import android. view. view; 6 7 public class DrawView extends View {8 9 public float currentX = 40; 10 public float currentY = 50; 11 12 public DrawView (Context context) {13 super (context ); 14} 15 16 @ Override17 protected void onDraw (Canvas canvas) {18 super. onDraw (canvas); 19 // create Paint brush 20 paint = new Paint (); 21 // set paint color 22 Paint. setColor (Color. RED); 23 // draw a small circle (as a ball) 24 canvas. drawCircle (currentX, currentY, 15, paint); 25} 26}
Crize_demo \ draw_demo \ src \ main \ res \ layout \ activity_main.xml
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"2 xmlns:tools="http://schemas.android.com/tools"3 android:layout_width="match_parent"4 android:layout_height="match_parent"5 android:orientation="horizontal"6 android:id="@+id/root"7 tools:context=".MainActivity">8 9 </LinearLayout>
Crize_demo \ draw_demo \ src \ main \ java \ com \ ly \ draw_demo \ MainActivity. java
1 import android. app. activity; 2 import android. OS. bundle; 3 import android. view. motionEvent; 4 import android. view. view; 5 import android. widget. linearLayout; 6 7 public class MainActivity extends Activity {8 9 @ Override10 protected void onCreate (Bundle savedInstanceState) {11 super. onCreate (savedInstanceState); 12 setContentView (R. layout. activity_main); 13 // get the LinearLayout Container 14 LinearLayout root = (LinearLayout) findViewById (R. id. root); 15 // create the DrawView component 16 final DrawView drawView = new DrawView (this); 17 // set the width and height of the custom frame to 18 drawView. setMinimumWidth (300); 19 drawView. setMinimumHeight (500); 20 // bind a Touch event to the drawView (Touch screen listening event) 21 drawView. setOnTouchListener (new View. onTouchListener () {22 @ Override23 public boolean onTouch (View v, MotionEvent event) {24 // modify the currentX and currentY attributes of the drawView component. currentX = event. getX (); 26 drawView. currentY = event. getY (); 27 // notify the drawView component to redraw 28 drawView. invalidate (); 29 // return true indicates that the processing method has handled the event 30 return true; 31} 32}); 33 root. addView (drawView); 34} 35 36 37}