Getting Started with Android custom Controls (ii)

Source: Internet
Author: User

The last time we talked about a bunch of theories that implement custom controls, we've enumerated some of the methods that can be overridden by the view class, and our rewriting of these methods is the key to inheriting the view class to derive the custom control


I've shown you this process in one of the simplest examples, no matter how complex a custom control is, but because we just rewrite the OnDraw method to make everyone feel weird, as a control, we actually have to add trouble for his implementation of the monitoring, This is not called a control.

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


In this way, we have built the listener part of the activity into the inside of the control, which is called a complete control, its function is to create an area, which contains a small ball that can change position according to the touch of the finger.

Let's take 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 custom controls to implement

Just get rid of 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 solve!

This example I will be with the first one together, and I wrote the adapter before the tutorial, the source code I will organize and then to everyone. The second program I note is not so much attention ... Not my lazy, recently busy (actually lazy)


Let me post the code:


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);}}


the introduction of a lot of wood has!


Customize the code for view version 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 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, the core of the custom control is to rewrite these methods, and add the required logic, the view method is not many and many, I will use this example to give you a try, It is important to take the time to understand and understand the overriding methods of these methods when you are customizing your own controls and choosing the path to inherit view. Let me introduce you next time . If a custom view needs to have a custom property of what we are going to do, the next one will be the end of the series, because the way to customize the view is far away, and I do not cite some hard examples, and I think the basic knowledge is only these, After learning this, the basis of custom control is finished, and the rest is on the basis of the play! If there is a better example then I will continue to add.


SOURCE I will send you the next time the link, I hope you can learn something ~


In addition, I am also a student, if there is bad or wrong to write the place also please give us a lot of advice, thank you!

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.