A single virtual joystick Development

Source: Internet
Author: User
Tags gety

Mainly primary des three parts, respectively is the calling class, touch handler class, utility class.

Touch handler class:Appsinglerocker

Package com. seuic;

Import com. seuic. util. mathutils;

Import Android. content. context;
Import Android. Graphics. Canvas;
Import Android. Graphics. color;
Import Android. Graphics. paint;
Import Android. Graphics. pixelformat;
Import Android. Graphics. Point;
Import Android. Graphics. porterduff. mode;
Import Android. View. motionevent;
Import Android. View. surfaceholder;
Import Android. View. surfaceview;
Import Android. View. surfaceholder. Callback;

Public class appsinglerocker extends surfaceview implements callback {

Private surfaceholder mholder;
Private paint mpaint;
Public point mrockerposition; // rocker position
Private point mctrlpoint = new point (80, 80); // rocker starting position
Private int mrudderradius = 20; // Rocker Arm radius
Private int mwheelradius = 60; // rocker range radius

Int ishide = 0;
Private singlerudderlistener listener = NULL; // Event Callback Interface
Public static final int action_rudder = 1, action_attack_devicemove = 2, action_stop = 3, action_attack_cameramove = 4;
Public javasinglerocker (context ){
Super (context );
This. setkeepscreenon (true );
Mholder = getholder ();
Mholder. addcallback (this );
Mpaint = new paint ();
Mpaint. setcolor (color. Green );
Mpaint. setantialias (true); // anti-aliasing
Mrockerposition = new point (mctrlpoint );
Setfocusable (true );
Setfocusableintouchmode (true );
Setzorderontop (true );
Mholder. setformat (pixelformat. Transparent); // sets the background transparency.
}

// Callback Interface
Public interface singlerudderlistener {
Void onsteeringwheelchanged (INT action, int angle );
}

// Set the callback Interface
Public void setsinglerudderlistener (singlerudderlistener rockerlistener ){
Listener = rockerlistener;
}

Int Len;
@ Override
Public Boolean ontouchevent (motionevent event ){
Try {
If (ishide = 0 ){
Switch (event. getaction ()){
Case motionevent. action_down:
Len = mathutils. getlength (mctrlpoint. X, mctrlpoint. Y, event. getx (), event. Gety ());
// If the screen contact point is not within the rocker wave range, do not process it
If (LEN> mwheelradius ){
Return true;
}
Break;
Case motionevent. action_move:
Len = mathutils. getlength (mctrlpoint. X, mctrlpoint. Y, event. getx (), event. Gety ());
If (LEN <= mwheelradius ){
// If the finger is in the active range of the joystick, the joystick is in the touch position of the finger
Mrockerposition. Set (INT) event. getx (), (INT) event. Gety ());

} Else {
// Set the joystick position so that it is at the edge of the Active range of the joystick in the direction of the finger touch
Mrockerposition = mathutils. getborderpoint (mctrlpoint, new point (INT) event. getx (), (INT) event. Gety (), mwheelradius );
}
If (listener! = NULL ){
Float radian = mathutils. getradian (mctrlpoint, new point (INT) event. getx (), (INT) event. Gety ()));
Listener. onsteeringwheelchanged (action_rudder, getanglecouvert (radian ));
}
Break;
Case motionevent. action_up:
Mrockerposition = new point (mctrlpoint );
Listener. onsteeringwheelchanged (action_stop, 0 );
Break;
}
Canvas_ OK ();
Thread. Sleep (60 );
} Else {
Thread. Sleep (200 );
}
} Catch (exception e ){

}
Return true;
}

// Obtain the joystick offset from 0 to °.
Private int getanglecouvert (float radian ){
Int TMP = (INT) math. Round (radian/Math. Pi * 180 );
If (TMP <0 ){
Return-TMP;
} Else {
Return 180 + (180-TMP );
}
}

Public void surfacecreated (surfaceholder holder ){
Canvas_ OK ();
}

Public void surfacechanged (surfaceholder holder, int format, int width,
Int height ){

}

Public void surfacedestroyed (surfaceholder holder ){

}
// Set whether to hide
Public void hided (int opt)
{
Ishide = OPT;
Canvas_ OK ();
}
 
// Draw the image
Public void canvas_ OK (){
Canvas canvas = NULL;
Try {
Canvas = mholder. lockcanvas ();
Canvas. drawcolor (color. Transparent, mode. Clear); // clear the screen
Mpaint. setcolor (color. Cyan );
Mpaint. setalpha (100 );
Canvas. drawcircle (mctrlpoint. X, mctrlpoint. Y, mwheelradius, mpaint); // draw range
Mpaint. setcolor (color. Red );
Mpaint. setalpha (100 );
Canvas. drawcircle (mrockerposition. X, mrockerposition. Y, mrudderradius, mpaint); // draw the joystick
} Catch (exception e ){
E. printstacktrace ();
} Finally {
If (canvas! = NULL ){
Mholder. unlockcanvasandpost (canvas );
}
}
}

}

 

Utility Class:Mathutils

Package com. seuic. util;

Import Android. Graphics. Point;
Public class mathutils {
// Obtain the linear distance between two points
Public static int getlength (float X1, float Y1, float X2, float Y2 ){
Return (INT) math. SQRT (math. Pow (x1-x2, 2) + math. Pow (y1-y2, 2 ));
}
/**
* Obtains the coordinates of a point on a line segment. The length is A. X-cutradius.
* @ Param A:
* @ Param B point B
* @ Param cutradius truncation distance
* @ Return truncation point
*/
Public static point getborderpoint (point a, point B, int cutradius ){
Float radian = getradian (A, B );
Return new point (. X + (INT) (cutradius * Math. cos (radian),. X + (INT) (cutradius * Math. sin (radian )));
}

// Obtain the angle radians of the horizontal line
Public static float getradian (point a, point B ){
Float Lena = B. x-a.x;
Float lenb = B. y-a.y;
Float lenc = (float) math. SQRT (Lena * Lena + lenb * lenb );
Float ang = (float) math. ACOs (Lena/lenc );
Ang = ang * (B. Y <A. Y? -1: 1 );
Return Ang;
}
}

Calling class:Singlerocker

Package com. seuic;

Import com. seuic. appsinglerocker. singlerudderlistener;

Import Android. OS. Bundle;
Import Android. util. log;
Import Android. widget. relativelayout;
Import Android. App. activity;

Public class singlerocker extends activity {
 
Relativelayout parent;
Appsinglerocker;
Public static final string main = "singlerocker ";
@ Override
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Parent = new relativelayout (this );
Relativelayout. layoutparams rockercanvasparams = new relativelayout. layoutparams (160,160 );
Rockercanvasparams. addrule (relativelayout. center_vertical );
Appsinglerocker = new appsinglerocker (this );
Parent. addview (appsinglerocker, rockercanvasparams );
Setcontentview (parent );
Appsinglerocker. setsinglerudderlistener (New singlerudderlistener (){

@ Override
Public void onsteeringwheelchanged (INT action, int angle ){
Log. E (main, angle + "");
}
});
}

}

 

 

Source Address:

Http://download.csdn.net/download/jwzhangjie/5184324

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.