Android custom components automatically draw circles with your fingers

Source: Internet
Author: User

First, customize a view subclass:

Package Com.example.androidtest0.myview;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 Drawview extends View {public float CurrentX = 40;public F Loat currenty = 50;//definition, and create brush paint p = new paint ();p ublic Drawview (context context) {super (context);} Public Drawview (context context, AttributeSet Attrs) {Super (context, attrs);} @Overrideprotected void OnDraw (canvas canvas) {super.ondraw (canvas);//Set the brush's color P.setcolor (color.red);// Draw a small ball canvas.drawcircle (CurrentX, CurrentY, p);} /** * Override event handling method for the component's Touch event */@Overridepublic Boolean ontouchevent (Motionevent event) {//modify CurrentX, currenty two properties CurrentX = Event.getx (); currenty = Event.gety ();//Notifies the current component to redraw itself invalidate (); return true;}    }

Main interface xml:

Custom_layout.xml

<pre name= "code" class= "java" ><?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:id= "@+id/root"    android:orientation= "vertical" ></LinearLayout>



Main activity:

Package Com.example.androidtest0;import Com.example.androidtest0.myview.drawview;import android.app.Activity; Import Android.os.bundle;import Android.widget.linearlayout;public class CustomView extends Activity {@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.custom_layout);//Get the LinearLayout container in the layout file linearlayout root = (linearlayout) Findviewbyid (r.id.root);// Create Drawview component Final Drawview Drawview = new Drawview (this);//Set the minimum width of the custom component, Height drawview.setminimumwidth (10); Drawview.setminimumheight (Root.addview); (Drawview);}
Effect:



Besides:

It can also be done in XML: It is also the first subclass of a view, as in the above. Then the main interface XML is as follows:

<?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:id=" @+id /root "    android:orientation=" vertical ">    <com.example.androidtest0.myview.drawview         android: Layout_width= "Match_parent" android:layout_height= "match_parent"        /></linearlayout>

The main activity file is as follows:

Package Com.example.androidtest0;import Com.example.androidtest0.myview.drawview;import android.app.Activity; Import Android.os.bundle;import Android.widget.linearlayout;public class CustomView extends Activity {@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( r.layout.custom_layout);}}


Android custom components automatically draw circles with your fingers

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.