Android footstep---ball with finger movement

Source: Internet
Author: User

This study is mainly in the package in addition to automatically generated Mainactivity.java, and then create a new class, Drawview, mainactivity inside call Drawview.

In Drawview, it is primarily a rewrite of the OnDraw method,----The method is responsible for drawing a small ball at the specified location of the component.

Package Org.crazyit.helloworld;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=40;     public float currenty=50;          A constructor method must be overridden public     Drawview (context context) {         super (context);         TODO auto-generated constructor stub     }     //override OnDraw method canvas painting  @Override public     void OnDraw (canvas Canvas) {               super.ondraw (canvas);         Create brush         Paint paint=new paint ();         Paint.setcolor (color.red);         Draw a small round         canvas.drawcircle (CurrentX, currenty, +, paint);      }}

Quote Drawview in Mainactivity.

Package Org.crazyit.helloworld;import android.app.Activity; Import Android.os.Bundle; Import Android.util.log;import android.view.MotionEvent; Import Android.view.View; Import Android.widget.LinearLayout; public class Mainactivity extends Activity {public void onCreate (Bundle savedinstancestate) {super.oncreate (s         Avedinstancestate);         Setcontentview (R.layout.activity_main);         LinearLayout root= (linearlayout) Findviewbyid (r.id.root);         Create an object that you inherit from view final Drawview drawview=new Drawview (this);         Sets the maximum width and height of the custom component Drawview.setminimumwidth (300);             Drawview.setminimumheight (500); Bind Tounch Event Drawview.setontouchlistener for Drawview component (new View.ontouchlistener () {public Boolean OnT                Ouch (View V, motionevent event) {LOG.E ("OnTouch", "OnTouch");                 Drawview.currentx=event.getx ();                  Drawview.currenty=event.gety (); Notifies the Drawview component to redraw DRawview.invalidate ();             Returns true to indicate that the method has handled the event return true;         }         });     Root.addview (Drawview); }}

In the Main.xml:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" fill_parent "     android:layout_height=" Fill_parent     " android:orientation= "vertical"      android:background= "#00FFCC"//define background color    android:id= "@+id/root" > </ Linearlayout>   

Final Result:

Android footstep---ball with finger movement

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.