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 realize circle and drag circle to follow finger drag by Android

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "Android how to achieve circle and drag circle to follow finger drag". Xiaobian shows you the operation process through actual cases, the operation method is simple and fast, and it is practical. I hope this article "how to achieve circle and drag circle to follow finger drag" can help you solve the problem.

Simply customize a circle is very simple, it only takes a few steps to drag the circle to add to achieve the touch event.

1. If you drag a circle, the width and height of the custom circle set in the xml is the size of the space it can move, not the size of the circle control, and if you define 100dp dragging it beyond this distance of 100dp, the circle will not be visible, like the following, if you want to move around the entire screen, just give the wide and high match_parent properties directly.

two。 The custom view in the layout will prompt you to compile and click Build to compile it.

Let's start by writing code: first, simply create a circle and create a class that inherits the View implementation of the onDraw method

Public class CustomView extends View {/ / create point object parameters as x and y coordinates private PointF point = new PointF (100,100); public CustomView (Context context) {super (context);} public CustomView (Context context, @ Nullable AttributeSet attrs) {super (context, attrs);} public CustomView (Context context, @ Nullable AttributeSet attrs, int defStyleAttr) {super (context, attrs, defStyleAttr) } @ Override protected void onDraw (Canvas canvas) {super.onDraw (canvas) Create / / if the circle does not come out, it means that the space of width and height defined in your xml is not large enough. Here is the circle in the coordinate position of the screen. Xml defines the area where the circle can be displayed. If the coordinates of the circle you define are too small and exceed this area, it will not be displayed. Xml defines width and height to fill the screen canvas.drawCircle (point.x,point.y, 50, new Paint ()). }}

The name of the self-defined view class in XML:

Just create a circle and run it directly. You don't have to change anything in ManActivity.

@ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main);}

Public class CustomView extends View {/ / create point object parameters as x and y coordinates private PointF point = new PointF (100,100); public CustomView (Context context) {super (context);} public CustomView (Context context, @ Nullable AttributeSet attrs) {super (context, attrs);} public CustomView (Context context, @ Nullable AttributeSet attrs, int defStyleAttr) {super (context, attrs, defStyleAttr) } @ Override protected void onDraw (Canvas canvas) {super.onDraw (canvas) / / the parameter is the Abscissa, ordinate and radius of the circle Create / / if the circle does not come out, it means that the space of width and height defined in your xml is not large enough. Here is the circle in the coordinate position of the screen. Xml defines the area where the circle can be displayed. If the coordinates of the circle you define are too small and exceed this area, it will not be displayed. Xml defines width and height to fill the screen canvas.drawCircle (point.x,point.y, 50, new Paint ()). } / / Touch event @ Override public boolean onTouchEvent (MotionEvent event) {/ / get touch event switch (event.getAction ()) {case MotionEvent.ACTION_DOWN: break / / ACTION_MOVE do not set break, otherwise the circle will not follow the finger movement and will only release the screen when the circle goes directly to the stop position of the screen case MotionEvent.ACTION_MOVE: case MotionEvent.ACTION_UP: / / get the x coordinate of the finger touch position point.x = event.getX () / / get the y coordinates of the finger touch position point.y = event.getY (); / / start postInvalidate (); break;} return true;}} about "Android how to achieve the circle and drag the circle to follow the finger drag", thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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