Android game development

Source: Internet
Author: User

Principle and code example of Long-press events
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 minteger = 500;
// Timer trigger interval, that is, the press is calculated every 0.5 seconds.
Public RepeatingImageButton (Context context ){
This (context, null );
} Public RepeatingImageButton (Context context, AttributeSet attrs ){
This (context, attrs, android. R. attr. imageButtonStyle );
} Public RepeatingImageButton (Context context, AttributeSet attrs, int defStyle ){
Super (context, attrs, defStyle );
SetFocusable (true );
// SetLongClickable (true );
// Enable long press event}
Public void setRepeatListener (RepeatListener l,
Long interval) {// implement repeated pressing event listener 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 ){//
This method works the same way as 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 );
} // Key or trackball press event @ Override to process the navigation key event
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 length is pressed to end @ 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 long-pressed 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 );
// The calculation duration is followed by a delay of the next accumulate }}};
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 a Button, use setRepeatListener () 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. }}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.