Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to determine whether the click location is within the specified area in android

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to judge whether the click position is in the specified area in android". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian slowly and deeply to study and learn together "how to judge whether the click position is in the specified area in android"!

Draw Fan

To determine whether the position of the click is within the sector area, first draw the sector. The method of drawing the sector can be used by canvas.drawArc() method. Here we're talking about using the Path method, and then calling canvas.drawPath(ovalPath,paint); to draw.

The main codes are as follows:

@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //Move the canvas origin to the center position canvas.translate((getWidth() + getPaddingLeft() - getPaddingRight()) / 2, (getHeight() + getPaddingTop() - getPaddingBottom()) / 2); ovalPath.moveTo(0, 0); ovalPath.lineTo(100, 0); RectF oval = new RectF(-100,-100,100,100); ovalPath.addArc(oval, 0, 160); ovalPath.lineTo(0, 0); ovalPath.close(); RectF r = new RectF(); ovalPath.computeBounds(r, true); mRegion.setPath(ovalPath, new Region((int)r.left, (int) r.top, (int) r.right,(int)r.bottom)); Paint paint = new Paint(); paint.setColor(Color.RED); paint.setAntiAlias(true); paint.setStrokeWidth(1); paint.setStyle(Paint.Style.STROKE); canvas.drawPath(ovalPath,paint); mCenterCircleX = (getWidth() + getPaddingLeft() - getPaddingRight()) / 2; mCenterCircleY = (getHeight() + getPaddingTop() - getPaddingBottom()) / 2;}

The method to obtain the click position and determine whether it is within the sector is as follows:

public boolean onTouchEvent(MotionEvent event) { float x; float y; switch (event.getAction()) { case MotionEvent.ACTION_DOWN: x = event.getX()-mCenterCircleX; y = event.getY()-mCenterCircleY; boolean b = mRegion.contains((int) x, (int) y); Log.d(TAG, "onTouchEvent: b: "+b +" x: "+ x+" y: "+y); break; } return true;}

Note here: After obtaining the click position, subtract the offset of the coordinates, otherwise you will not get the correct result.

Thank you for reading, the above is "android how to judge whether the click position is in the specified area" content, after the study of this article, I believe that everyone on android how to judge whether the click position in the specified area this problem has a deeper understanding, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report