Android custom View needs to override ondraw () and other methods

Source: Internet
Author: User

Android custom View needs to override ondraw () and other methods

Android custom View Requires override of ondraw () and other methods. This blog tells you how to write a custom View. We need to inherit the View and then rewrite some

There are many methods, depending on what method you need


First, write a custom View to inherit the View.

Package com. example. engineerjspview; import android. content. context; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. util. attributeSet; import android. view. motionEvent;/*** custom EngineerJspView * @ author Engineer-Jsp * @ date 2014.10.28 **/import android. view. view; public class EngineerJspView extends View {public float E_X = 188; public float E_Y = 188; Paint paint = new Paint (); public EngineerJspView (Context context) {super (context);} public EngineerJspView (Context context, AttributeSet set) {super (context, set) ;}@ Overrideprotected void onDraw (Canvas canvas) {super. onDraw (canvas); paint. setColor (Color. RED); canvas. drawCircle (E_X, E_Y, 88, paint);} @ Overridepublic boolean onTouchEvent (MotionEvent event) {E_X = event. getX (); E_X = event. getY (); invalidate (); return false ;}@ Overrideprotected void onAnimationStart () {super. onAnimationStart () ;}@ Overrideprotected void onAnimationEnd () {super. onAnimationEnd ();}}

Layout file:

     
  
 

Main activity:

Package com. example. engineerjspview;/*** custom EngineerJspView * @ author Engineer-Jsp * @ date 2014.10.28 **/import android. OS. bundle; import android. app. activity; import android. view. menu; import android. view. view; public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main );}}

Effect:



Custom View depends on your needs. It is not just about drawing a picture. Here I just want to explain how to rewrite the custom View ~~

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.