Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize 360degree rocker with Android

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how Android implements a 360-degree joystick. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Create a new class. For example, MySurfaceView

Package com.example.blt;import android.annotation.SuppressLint;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.PixelFormat;import android.graphics.PorterDuff.Mode;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.SurfaceHolder;import android.view.SurfaceView;import android.view.SurfaceHolder.Callback;public class MySurfaceView extends SurfaceView implements Callback {private SurfaceHolder sfh; private Canvas canvas; private Paint paint Private int coordinate; / / fixed the radius of the background circle of the rocker private int RockerCircleR, the X-ray coordinates of SmallRockerCircleR; / / and the radius of the rocker private float SmallRockerCircleX, SmallRockerCircleY; private RudderListener listener = null; / / event callback interface public MySurfaceView (Context context) {super (context) } public MySurfaceView (Context context, AttributeSet as) {super (context, as); this.setKeepScreenOn (true); sfh = getHolder (); sfh.addCallback (this); paint = new Paint (); paint.setColor (Color.GREEN); paint.setAntiAlias (true) / / Anti-aliasing setFocusable (true); setFocusableInTouchMode (true); setZOrderOnTop (true); sfh.setFormat (PixelFormat.TRANSPARENT); / / set background transparency} public void surfaceCreated (SurfaceHolder holder) {/ / get the minimum of the control int little = this.getWidth () < this.getHeight ()? This.getWidth (): this.getHeight (); / / draw SmallRockerCircleX = SmallRockerCircleY = coordinate = little / 2 according to the screen size; / / fix the radius of the background circle of the rocker RockerCircleR = (int) (little * 0.35) / / the radius of the rocker SmallRockerCircleR = (int) (little * 0.15); draw () } / * get the Radian between two points * / public double getRad (float px1, float py1, float px2, float py2) {/ / get the distance between two points X float x = px2-px1; / / get the distance between two points Y float y = py1-py2 / / calculate the length of the bevel float xie = (float) Math.sqrt (Math.pow (x, 2) + Math.pow (y, 2)); / / get the cosine of this angle (through the theorem in trigonometric function: adjacent edge / bevel = angle cosine) float cosAngle = x / xie / / the Radian of the angle float rad = (float) Math.acos (cosAngle) is obtained by the inverse cosine theorem. / / Note: when the position of the touch screen Y coordinate = RockerCircleR) {/ / get the angle between the rocker and the touch screen point double tempRad = getRad (coordinate, coordinate, event.getX (), event.getY ()) / / guarantee the length limit of the inner circle movement getXY (coordinate, coordinate, RockerCircleR, tempRad);} else {/ / if the center point of the ball is less than the active area, then SmallRockerCircleX = (int) event.getX () SmallRockerCircleY = (int) event.getY ();}} else if (event.getAction () = = MotionEvent.ACTION_UP) {/ / the position of the rocker to restore the rocker when the button is released is the original position SmallRockerCircleX = coordinate SmallRockerCircleY = coordinate;} draw (); if (listener! = null) {listener.onSteeringWheelChanged ((SmallRockerCircleX-coordinate) / RockerCircleR, (coordinate-SmallRockerCircleY) / RockerCircleR) } return true } / * * @ param R * rotation point of circular motion * @ param centerX * rotation point X * @ param centerY * rotation point Y * @ param rad * Arc of rotation Degree * / public void getXY (float centerX Float centerY, float R, double rad) {/ / obtain the X coordinate of circular motion SmallRockerCircleX = (float) (R * Math.cos (rad)) + centerX / / get the Y coordinate of circular motion SmallRockerCircleY = (float) (R * Math.sin (rad)) + centerY;} public void draw () {try {canvas = sfh.lockCanvas (); / / canvas.drawColor (Color.WHITE) Canvas.drawColor (Color.TRANSPARENT, Mode.CLEAR); / / clear screen / / set transparency paint.setColor (Color.CYAN); / / draw joystick background canvas.drawCircle (coordinate, coordinate, RockerCircleR, paint) Paint.setColor (Color.RED); / / draw rocker canvas.drawCircle (SmallRockerCircleX, SmallRockerCircleY, SmallRockerCircleR, paint) } catch (Exception e) {/ / TODO: handle exception} finally {try {if (canvas! = null) sfh.unlockCanvasAndPost (canvas) } catch (Exception e2) {} public void surfaceChanged (SurfaceHolder holder, int format, int width) Int height) {} public void surfaceDestroyed (SurfaceHolder holder) {} / / set callback interface public void setRudderListener (RudderListener rockerListener) {listener = rockerListener The callback interface public interface RudderListener {void onSteeringWheelChanged (float cross, float longitudinal);}}

In the main form

Package com.example.blt;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class ControlActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState) SetContentView (R.layout.activity_control); MySurfaceView temp = (MySurfaceView) findViewById (R.id.rudder) Temp.setRudderListener (new MySurfaceView.RudderListener () {@ Override public void onSteeringWheelChanged (float cross, float longitudinal) {/ / TODO Auto-generated method stub Log.v ("change", "c" + cross + "l" + longitudinal) ((TextView) findViewById (R.id.textView2)) .setText ("c:" + cross + "l:" + longitudinal);}}) ((Button) findViewById (R.id.button2)) .setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) { / / TODO Auto-generated method stub}}) } @ Override public boolean onCreateOptionsMenu (Menu menu) {/ / Inflate the menu; this adds items to the action bar if it is present. / / getMenuInflater () .inflate (R.menu.control, menu); return false;} @ Override public boolean onOptionsItemSelected (MenuItem item) {/ / Handle action bar item clicks here. The action bar will / / automatically handle clicks on the Home/Up button, so long / / as you specify a parent activity in AndroidManifest.xml. Int id = item.getItemId (); if (id = = R.id.action_settings) {return true;} return super.onOptionsItemSelected (item);}}

-- main form XML

Thank you for reading! This is the end of the article on "how to realize the Android rocker". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report