Getting started with Android's own definition controls (ii)

Source: Internet
Author: User

The last time we talked about a bunch of theories that implement our own defined controls. Some of the methods that can be overridden by the view class are listed, and our rewriting of these methods is the key to inheriting the view class to derive our own defined control


I've shown you this process in one of the simplest examples, no matter how complex you define a control. This is always the way of thinking, but because we only rewrite the OnDraw method to make people think strange. As a control, we have to add trouble to the listener for his implementation, which is not called a control.

here is another way to introduce a method that is often rewritten: publicboolean ontouchevent (motionevent event)


In this way, we put the listening part of the activity in the inside of the control. This ability is called a complete control, and its function is to create an area that includes a small ball that changes position depending on the touch of a finger.

Here's a look at this touch event method:

Publicboolean ontouchevent (motionevent event)


Added in API Level 1


Implement This method to handle touch screen motionevents.

If This method was used to detect click Actions, it isrecommended that the actions being performed by implementing and Cal Ling PerformClick (). This willensure consistent system behavior, including:

Obeying Click Sound Preferences

Dispatching Onclicklistener calls

Handling Action_click whenaccessibility features is enabled


Parameters

Event

The motion event.


Returns

True If the event was handled, false otherwise.

So we can put the work we've just done in the activity class into our own definition controls to implement

Just remove the setter and getter and rewrite the Touch event method:


public boolean ontouchevent (Motionevent motionevent) {Circlex  = Motionevent.getx (); Circley  = Motionevent.gety (); This.invalidate (); return true;

So we just need to simply call in the XML, everything is happy to conquer!

I'll put it together with the first one. Just like the tutorial I wrote about the adapter before. Source code I'll tidy it up and give it to everyone again. The second program my gaze is not so careful ... I'm not lazy. More busy recently (in fact, lazy)


I'll post the code below:


Activity's Code:


Package Com.example.customcomponentsdemo.activity;import Com.example.customcomponentsdemo.r;import Android.app.activity;import Android.content.context;import Android.os.bundle;public class MoveBallActivity2 extends activity{@Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.LAYOUT.ACTIVITY_MOVEBALL2);}}


A simple introduction to a lot of wood has!


Define your own code for view version number 2:

Package Com.example.customcomponentsdemo.component;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 DrawView2 extends View{private Context context;private float Circlex = 100;private float Circley = 100;private float Circler = 10;public DrawView2 (context context, AttributeSet Attrs) {Super (context, attrs); this.context = context;} @Overridepublic void OnDraw (Canvas canves) {Paint paint = new Paint ();p aint.setcolor (Color.Blue); Canves.drawcircle ( Circlex, Circley, Circler, paint);} @Overridepublic boolean ontouchevent (Motionevent motionevent) {Circlex  = Motionevent.getx (); Circley  = Motionevent.gety (); This.invalidate (); return true;}}

This is a lot more concise!


There are also XML layout files:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "vertical" >    <textview        android:layout_width= "wrap_content"        android:layout_height= " Wrap_content "        android:text=" This is Moveball demo version number 2 "        android:textcolor=" @color/white ">    </ textview>    <com.example.customcomponentsdemo.component.drawview2        android:layout_width= "Match_ Parent "        android:layout_height=" match_parent "        android:layout_margin=" 10DP ">    </ Com.example.customcomponentsdemo.component.drawview2>        </LinearLayout>


so our second tutorial will be here first, this time the theme is not this Ontouch method, but to tell you that the core of their own definition of the control is to rewrite these methods, and add the necessary logic. The view method is not many and many. I'm going to use this example to give you a little bit of a chance when you define your own control and choose the path to inherit view. It is important to take the time to understand and understand the overriding methods of these methods. Let me introduce you next time . Suppose that the View you define needs to have a property of its own defining what to do with it. The next talk will be the end of the series, because I have defined the view of the road is very far, I did not give some very difficult examples. I feel that the basic knowledge only has these, and after learning these the basis of their own definition of the control is finished, and the rest of us on the basis of the play. I will continue to add the following example if there are better examples.


Source code I will send you a link next time, I hope you can learn something ~


In addition, I am also a student, if there is a bad or wrong to write the place also please everyone more generous enlighten, thank you!

Getting started with Android's own definition controls (ii)

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.