The ball that follows the finger.

Source: Internet
Author: User

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}

 

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.