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 classDrawview extends View { Public floatCurrentX = +;  Public floatCurrentY = -; //define, and create brushesPaint p =NewPaint ();  PublicDrawview (Context context) {super (context); }     PublicDrawview (Context context, AttributeSet Attrs) {Super (context, attrs); } @Overrideprotected voidOnDraw (canvas canvas) {super.ondraw (canvas); //set the color of a brushP.setcolor (color.red); //draw a small ballCanvas.drawcircle (CurrentX, CurrentY, the, p); }        /** * Override event handling method for touch event of this component*/@Override PublicBoolean ontouchevent (motioneventEvent) {        //Modify CurrentX, currenty two propertiesCurrentX =Event. GetX (); CurrentY=Event. GetY (); //notifies the current component to redraw itselfinvalidate (); 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>

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 {    @Override    protectedvoid  onCreate (Bundle Savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (r.layout.custom_layout);    }}

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 {    @Override    protectedvoid  onCreate (Bundle Savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (r.layout.custom_layout);    }}

Android custom components automatically draw circles with your fingers

Related Article

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.