[Android] toggle button – Custom Control – drag effect

Override the ontouchevent () method of view and pass in the motionevent object

Call the getaction () method of the motionevent object to get the current action

Switch to judge the current action

The event is motionevent.action_ Down is the first time your finger touches the screen

The event is motionevent.action_ Move is the movement of the finger on the screen

The event is motionevent.action_ Up is when the finger leaves the screen

When your finger touches the screen

Defines the last coordinate lastx of the finger

Call the geTx () method of the motionevent object to get the value of lastx

When the finger moves on the screen

Defines the distance dis that the finger moves laterally

Call geTx () - lastx is the moving distance

The left side of the sliding button is defined as the moving distance

Judge that the slidebtnleft is in a reasonable position, 0 to the width of the background image - the width of the slide button

Call the invalidate () method to refresh the view

Onclick event and ontouchevent conflict

Define a flag isdrag variable. If drag occurs, assign this variable to true

Judge this variable in the onclick () method

When the finger is raised

Judge the current slidebtnleft to determine whether the current button is on or off

Slidebtnleft can judge the current state by comparing half of maxleft

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>