In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces Android how to customize View circle and drag circle to achieve the effect of following finger drag 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 customize View circle and drag circle to achieve follow finger drag effect article will have a harvest, let's take a look at it.
Several Bug I encountered in customizing the View circle for the first time:
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。 I will report an error when I define a full attribute match_parent. I just can't use any method. Just wait patiently for a while. It's possible that studio didn't have time 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) / / the parameter is the Abscissa, ordinate and radius of the circle, and create 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);}
The following is the ability to add a drag circle is very simple to achieve touch listening, the code is very little as follows:
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) / / parameters are circle Abscissa, ordinate, radius, create 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 coordinate of the finger touch position point.y = event.getY () / / start postInvalidate (); break;} return true;}} on "how to customize View circles and drag circles to achieve the effect of following fingers by Android", thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to customize the View circle and drag the circle to achieve the effect of following the finger". 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.