Android custom components automatically circle with fingers

Source: Internet
Author: User

Android custom components automatically circle with fingers

First, define 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 float currentY = 50; // define and create Paint p = new Paint (); public DrawView (Context context) {super (context);} public DrawView (Context context, AttributeSet attrs) {super (context, attrs);} @ Overrideprotected void onDraw (Canvas canvas Canvas) {super. onDraw (canvas); // sets the paint brush color p. setColor (Color. RED); // draw a small ball canvas. drawCircle (currentX, currentY, 15, p);}/*** rewrite event processing method for touch events of the component */@ Overridepublic boolean onTouchEvent (MotionEvent event) {// modify the currentX and currentY attributes currentX = event. getX (); currentY = event. getY (); // notify the current component to redraw its own invalidate (); return true ;}}

Main Interface XML:

Custom_layout.xml

  
  



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); // obtain the LinearLayout container LinearLayout root = (LinearLayout) findViewById (R. id. root); // create the DrawView component final DrawView drawView = new DrawView (this); // set the minimum width and height of the custom component drawView. setMinimumWidth (10); drawView. setMinimumHeight (10); root. addView (drawView );}}
Effect:



<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + s/20y9auzekjuw.vcd4kpha + u7m/ydLU08NYTUy1xLe9yr2jutKyysfK18/fingerprint + PHByZSBjbGFzcz0 = "brush: java;">


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


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.