How does Android create a draggable picture control
This example shares the specific code for Android to create a draggable picture control for your reference. The specific contents are as follows
Heavy load, self drawing
1. Derive a control class from View and call the parent constructor in the constructor.
2. Overload its OnDraw function to draw pictures inside. (there is a sense of deja vu with MFC of windows. Maybe Android has borrowed the mode of windows)
Message processing
The message of dragging a picture mainly deals with pressing and moving messages, and overloads ontouchevent. Mathematical knowledge (translation): in action_ When down, record the coordinate points in action_ When moving, calculate the translation amount according to the current position and the position when pressing. Refresh the control to cause the control to redraw. When redrawing, move the upper left coordinate of the drawing.
At first, I just received action_ Down message, action_ The MOVE message is not captured and searched by the Internet. It turned out that I finally called the parent class function return super.onTouchEvent (event) at onTouchEvent. If false is returned in the parent class, it means that you don't pay attention to these messages and subsequent actions_ Move and action_ You won't come in.
Code and configuration
XML configuration for activity
Control's self drawn code
The getcenterpos function is a method to calculate the appropriate screen center according to the picture size.
Run program
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.