OnTouchEvent, ontouchevent used

Source: Internet
Author: User

OnTouchEvent, ontouchevent used

Create a "MyView" Class

Package onTouchEvent; import android. content. context; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. view. motionEvent; import android. view. view; public class MyView extends View {private int textX = 20, textY = 20; public MyView (Context context) {super (context); setFocusable (true ); // TODO Auto-generated constructor stub} // brush onDraw method @ Override protected void onDraw (Canvas canvas) {// TODO Auto-generated method stub super. onDraw (canvas); // initialize Paint paint = new Paint (); // set the color of paint. setColor (Color. GREEN); // set the font size to paint. setTextSize (50); // write the text canvas on the canvas. drawText ("Android", textX, textY, paint) ;}@ Override public boolean onTouchEvent (MotionEvent event) {// TODO Auto-generated method stub // obtain the X coordinate assignment of the user's finger touch screen and the X coordinate textX = (int) event of the text. getX (); // obtain the Y coordinate assignment of the user's finger touch screen and the text's Y coordinate textY = (int) event. getY (); // re-paint the cloth invalidate (); return true ;}}

In MainActivity, set setContentView to the current MyView, that is

setContentView(new MyView(this));

Run the following command:

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.