Android uses a touch to dynamically draw a rectangle and an android rectangle on the screen.

Source: Internet
Author: User

Android uses a touch to dynamically draw a rectangle and an android rectangle on the screen.
Requirement Overview:

Draw an area with your finger on the screen and return the coordinates of the area in the circle.

Technical implementation:

Customize the View, set the paint brush and corresponding parameters, and judge the touch event in the onTouchEvent () callback function. Draw a rectangle.

Code:

Custom View:

1 public class GameView extends View {
 2 // Declare the Paint object
 3 private Paint mPaint = null;
 4 private int StrokeWidth = 5;
 5 private Rect rect = new Rect (0,0,0,0); // Draw the rectangle manually
 6
 7 public GameView (Context context) {
 8 super (context);
 9 // Build object
10 mPaint = new Paint ();
11 mPaint.setColor (Color.RED);
12 // Start the thread
13 // new Thread (this) .start ();
14}
15 @Override
16 protected void onDraw (Canvas canvas) {
17 super.onDraw (canvas);
18 // Set without aliasing
19 mPaint.setAntiAlias (true);
20 canvas.drawARGB (50,255,227,0);
21 mPaint.setStyle (Paint.Style.STROKE);
22 mPaint.setStrokeWidth (StrokeWidth);
23 mPaint.setColor (Color.GREEN);
24 mPaint.setAlpha (100);
25 // Draw a solid green rectangle
26 canvas.drawRect (100, 200, 400, 200 + 400, mPaint);
27 mPaint.setColor (Color.RED);
28 canvas.drawRect (rect, mPaint);
29}
30 @Override
31 public boolean onTouchEvent (MotionEvent event) {
32 int x = (int) event.getX ();
33 int y = (int) event.getY ();
34 switch (event.getAction ()) {
35 case MotionEvent.ACTION_DOWN:
36 rect.right + = StrokeWidth;
37 rect.bottom + = StrokeWidth;
38 invalidate (rect);
39 rect.left = x;
40 rect.top = y;
41 rect.right = rect.left;
42 rect.bottom = rect.top;
43
44 case MotionEvent.ACTION_MOVE:
45 Rect old =
46 new Rect (rect.left, rect.top, rect.right + StrokeWidth, rect.bottom + StrokeWidth);
47 rect.right = x;
48 rect.bottom = y;
49 old.union (x, y);
50 invalidate (old);
51 break;
52
53 case MotionEvent.ACTION_UP:
54 break;
55 default:
56 break;
57}
58 return true; // The touch information is processed, and the message is no longer delivered
59}
60
61}

During the call, you only need to add it directly in the onCreate () function:

1         super.onCreate(savedInstanceState);
2         setContentView(R.layout.activity_main);
3 
4         gameView = new GameView(this);
5         addContentView(gameView);  

You can add a function with the return range to the custom class as needed.

Ps: It should be noted that when the finger moves, when the screen needs to update the rectangle, the original rectangle is deleted in principle and the new rectangle is drawn. However, due to the thickness of the hollow rectangular edge,

There will be legacy situations. At this time, minus the border thickness can solve the above problem.

Rect old = new Rect(rect.left,rect.top,rect.right+StrokeWidth,rect.bottom+StrokeWidth);
:


 


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.