Android------a small ball that moves with your finger

Source: Internet
Author: User

The small ball that follows the finger is implemented by a callback:

So what is a callback, the callback here is the way the two events are handled in Android. One is the listener mechanism implemented through listeners, and one is the callback mechanism implemented by its own method.


Based on the listening processing mechanism, mainly involves three kinds of objects:


EventSource (Event source): Usually the individual components;

Event: The operation of the component;

EventListener (Event Listener): responds.

The so-called event listener is actually an instance of a Java class that implements a particular interface.

There are four ways to implement event listeners in your program:

1. Internal class Form

2. External class Form

3.Activity itself as an event listener

4. Anonymous inner class


In general, the latter two are more commonly used.



Callback-based listening mechanism:

The event source is unified with the event listener, or the event listener disappears.

Callback-based event handling can be achieved by customizing the view.



What is the difference between the two?



The event listener mechanism is a delegate-type event handler, and the event source and event listener are not uniform or separate, not together.

Instead of the listener, the event source, which is a callback-based event-handling mechanism, can be implemented by itself.



Callback-based event propagation:

Almost all callback-based event-handling methods have a Boolean-type return value that identifies whether the processing method can handle the event fully.


Returns true, indicating that the processing method has fully handled the event and that the event is not propagated

Returns false, indicating that the processing method did not fully handle the event and that the event is propagated.


For callback-based event propagation, what happens on a component not only fires the callback method on that component, it also triggers a callback method for the activity of the component----as long as the event can propagate to the activity.



Instance:


A small ball that moves with your finger: custom view


Mainactivity.java:


Package Com.example.ontoucheventdemo;import Android.app.activity;import Android.os.bundle;import android.view.Menu Import Android.view.menuitem;public class Mainactivity extends Activity {@Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);}}




Drawviewdemo.java:


package Com.example.ontoucheventdemo;import Android.content.context;import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.paint;import Android.util.attributeset;import Android.view.motionevent;import Android.view.view;public class Drawviewdemo extends view{public float CurrentX = 40; public float currenty = 50;//definition. Create brush Paint p = new Paint ();p ublic Drawviewdemo (Context context,attributeset set) {//TODO auto-generated constructor STUBSU Per (context,set);} @Overrideprotected void OnDraw (canvas canvas) {//TODO auto-generated method Stubsuper.ondraw (canvas);// Sets the color of the Brush P.setcolor (color.red);//Draw a ball//parameter is: center coordinates, RADIUS, the brush used canvas.drawcircle (CurrentX, CurrentY, p);} @Overridepublic boolean ontouchevent (Motionevent event) {//TODO auto-generated method stub//Modify current coordinates this.currentx = Event.getx (); this.currenty = Event.gety ();//Redraw small ball this.invalidate (); return true;}} 


Activity_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:paddingbottom= "@dimen/activity_vertical_margin"    android: paddingleft= "@dimen/activity_horizontal_margin"    android:paddingright= "@dimen/activity_horizontal_margin"    android:paddingtop= "@dimen/activity_vertical_margin"    tools:context= " Com.example.ontoucheventdemo.MainActivity ">    <com.example.ontoucheventdemo.drawviewdemo         android: Layout_width= "Fill_parent"        android:layout_height= "fill_parent"        /></linearlayout>







Android------a small ball that moves with your finger

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.