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 does Android draw a rectangle on the screen dynamically through touch

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces Android how to dynamically draw rectangle effect on the screen through touch related knowledge, the content is detailed and easy to understand, simple and fast operation, has a certain reference value, I believe that after reading this Android how to dynamically draw rectangle effect on the screen through touch article will have a harvest, let's take a look at it.

Overview of requirements:

Draw an area with your finger on the screen and return the circled area coordinates.

Technical implementation:

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

Code:

Custom View:

Public class GameView extends View {/ / declare Paint object private Paint mPaint = null; private int StrokeWidth = 5; private Rect rect = new Rect; / / manually draw a rectangle public GameView (Context context) {super (context); / / build object mPaint = new Paint (); mPaint.setColor (Color.RED); / / Open thread / / new Thread (this). Start () } @ Override protected void onDraw (Canvas canvas) {super.onDraw (canvas); / / set up zigzag mPaint.setAntiAlias (true); canvas.drawARGB (50 Magi 255 and 227Power0); mPaint.setStyle (Paint.Style.STROKE); mPaint.setStrokeWidth (StrokeWidth); mPaint.setColor (Color.GREEN); mPaint.setAlpha (100); / / draw green solid rectangle canvas.drawRect (100,200,400,200 + 400, mPaint) MPaint.setColor (Color.RED); canvas.drawRect (rect,mPaint);} @ Override public boolean onTouchEvent (MotionEvent event) {int x = (int) event.getX (); int y = (int) event.getY (); switch (event.getAction ()) {case MotionEvent.ACTION_DOWN: rect.right+=StrokeWidth; rect.bottom+=StrokeWidth; invalidate (rect); rect.left = x; rect.top = y Rect.right = rect.left; rect.bottom = rect.top; case MotionEvent.ACTION_MOVE: Rect old = new Rect (rect.left,rect.top,rect.right+StrokeWidth,rect.bottom+StrokeWidth); rect.right = x; rect.bottom = y; old.union (XMagi y); invalidate (old); break; case MotionEvent.ACTION_UP: break Default: break;} return true;// handles touch information, messages are no longer delivered}}

When calling, just add it directly in the onCreate () function:

Super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); gameView = new GameView (this); addContentView (gameView)

As needed, you can add a function that returns the delineated scope to the custom class.

Ps: it is important to note that when the screen needs to update the rectangle when the finger is moved, the original rectangle is deleted and the new rectangle is drawn. However, due to the existence of the thickness of the hollow rectangular edge

There will be a legacy situation, and the above problem can be solved by subtracting the border thickness.

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

Effect picture:

This is the end of the article on "how Android can dynamically draw rectangular effects on the screen by touch". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how Android can dynamically draw rectangular effects on the screen through touch". If you want to learn more, you are welcome to follow the industry information channel.

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

Internet Technology

Wechat

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

12
Report