For many games that use screen control, you generally need to consider the long-press event. For example, in a gossip game, you need to use the long-press launch weapon. In combination with the android button model, we implement a long-press of a button with an image, for a clearer display principle, Android developer uses imagebutton as the base class.
- Public class repeatingimagebutton extends imagebutton {
- Private long mstarttime; // start with a long record
- Private int mrepeatcount; // repeated count
- Private repeatlistener mlistener;
- Private long minterval = 500; // timer trigger interval, that is, the press is calculated every 0.5 seconds.
- Public repeatingimagebutton (context ){
- This (context, null );
- }
- Public repeatingimagebutton (context, attributeset attrs ){
- This (context, attrs, Android. R. ATTR. imagebuttonstyle );
- }
- Public repeatingimagebutton (context, attributeset attrs, int defstyle ){
- Super (context, attrs, defstyle );
- Setfocusable (true); // allows focus
- Setlongclickable (true); // enable the long press event
- }
- Public void setrepeatlistener (repeatlistener L, long interval) {// implement repeated listener events
- Mlistener = L;
- Minterval = interval;
- }
- @ Override
- Public Boolean initialize mlongclick (){
- Mstarttime = systemclock. elapsedrealtime ();
- Mrepeatcount = 0;
- Post (mrepeater );
- Return true;
- }
- @ Override
- Public Boolean ontouchevent (motionevent event ){
- If (event. getaction () = motionevent. action_up) {// The principle of this method is the same as that of onkeyup. Here screen events are handled, and the onkeyup below processes physical button events on Android phones.
- Removecallbacks (mrepeater );
- If (mstarttime! = 0 ){
- Dorepeat (true );
- Mstarttime = 0;
- }
- }
- Return super. ontouchevent (event );
- }
- // Process the key or trackball press event of the navigation key event
- @ Override
- Public Boolean onkeydown (INT keycode, keyevent event ){
- Switch (keycode ){
- Case keyevent. keycode_dpad_center:
- Case keyevent. keycode_enter:
- Super. onkeydown (keycode, event );
- Return true;
- }
- Return super. onkeydown (keycode, event );
- }
- // When the button pops up, the notification duration is terminated.
- @ Override
- Public Boolean onkeyup (INT keycode, keyevent event ){
- Switch (keycode ){
- Case keyevent. keycode_dpad_center:
- Case keyevent. keycode_enter:
- Removecallbacks (mrepeater); // cancel repeated listener capture
- If (mstarttime! = 0 ){
- Dorepeat (true); // If the cumulative duration of a long press event is not 0, the long press is performed.
- Mstarttime = 0; // reset the timer
- }
- }
- Return super. onkeyup (keycode, event );
- }
- Private runnable mrepeater = new runnable () {// duplicate in the thread
- Public void run (){
- Dorepeat (false );
- If (ispressed ()){
- Postdelayed (this, minterval); // After the computing duration is pressed, the next accumulation is delayed.
- }
- }
- };
- Private void dorepeat (Boolean last ){
- Long Now = systemclock. elapsedrealtime ();
- If (mlistener! = NULL ){
- Mlistener. onrepeat (this, now-mstarttime, last? -1: mrepeatcount ++ );
- }
- }
The following is the definition of the repeated button listener interface. when calling the button, use the setrepeatlistener () method to implement the repeatlistener interface.
- Public interface repeatlistener {
- Void onrepeat (view V, long duration, int repeatcount); // The parameter is the user-passed button object, the parameter is the number of milliseconds of delay, and the third-bit repetition callback.
- }
- }
You can directly implement the repeatlistener interface in implements in your view.