您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關Android如何實現360度搖桿的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
新建一個類。舉例為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; // 固定搖桿背景圓形的半徑 private int RockerCircleR, SmallRockerCircleR; // 搖桿的X,Y坐標以及搖桿的半徑 private float SmallRockerCircleX, SmallRockerCircleY; private RudderListener listener = null; // 事件回調接口 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);// 抗鋸齒 setFocusable(true); setFocusableInTouchMode(true); setZOrderOnTop(true); sfh.setFormat(PixelFormat.TRANSPARENT);// 設置背景透明 } public void surfaceCreated(SurfaceHolder holder) { // 獲得控件最小值 int little = this.getWidth() < this.getHeight() ? this.getWidth() : this.getHeight(); // 根據屏幕大小繪制 SmallRockerCircleX = SmallRockerCircleY = coordinate = little / 2; // 固定搖桿背景圓形的半徑 RockerCircleR = (int) (little * 0.35); // 搖桿的半徑 SmallRockerCircleR = (int) (little * 0.15); draw(); } /*** * 得到兩點之間的弧度 */ public double getRad(float px1, float py1, float px2, float py2) { // 得到兩點X的距離 float x = px2 - px1; // 得到兩點Y的距離 float y = py1 - py2; // 算出斜邊長 float xie = (float) Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)); // 得到這個角度的余弦值(通過三角函數中的定理 :鄰邊/斜邊=角度余弦值) float cosAngle = x / xie; // 通過反余弦定理獲取到其角度的弧度 float rad = (float) Math.acos(cosAngle); // 注意:當觸屏的位置Y坐標<搖桿的Y坐標我們要取反值-0~-180 if (py2 < py1) { rad = -rad; } return rad; } @SuppressLint("ClickableViewAccessibility") @Override public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE) { // 當觸屏區域不在活動范圍內 if (Math.sqrt(Math.pow((coordinate - (int) event.getX()), 2) + Math.pow((coordinate - (int) event.getY()), 2)) >= RockerCircleR) { // 得到搖桿與觸屏點所形成的角度 double tempRad = getRad(coordinate, coordinate, event.getX(), event.getY()); // 保證內部小圓運動的長度限制 getXY(coordinate, coordinate, RockerCircleR, tempRad); } else {// 如果小球中心點小于活動區域則隨著用戶觸屏點移動即可 SmallRockerCircleX = (int) event.getX(); SmallRockerCircleY = (int) event.getY(); } } else if (event.getAction() == MotionEvent.ACTION_UP) { // 當釋放按鍵時搖桿要恢復搖桿的位置為初始位置 SmallRockerCircleX = coordinate; SmallRockerCircleY = coordinate; } draw(); if (listener != null) { listener.onSteeringWheelChanged((SmallRockerCircleX - coordinate) / RockerCircleR, (coordinate - SmallRockerCircleY) / RockerCircleR); } return true; } /** * * @param R * 圓周運動的旋轉點 * @param centerX * 旋轉點X * @param centerY * 旋轉點Y * @param rad * 旋轉的弧度 */ public void getXY(float centerX, float centerY, float R, double rad) { // 獲取圓周運動的X坐標 SmallRockerCircleX = (float) (R * Math.cos(rad)) + centerX; // 獲取圓周運動的Y坐標 SmallRockerCircleY = (float) (R * Math.sin(rad)) + centerY; } public void draw() { try { canvas = sfh.lockCanvas(); // canvas.drawColor(Color.WHITE); canvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);// 清除屏幕 // 設置透明度 paint.setColor(Color.CYAN); // 繪制搖桿背景 canvas.drawCircle(coordinate, coordinate, RockerCircleR, paint); paint.setColor(Color.RED); // 繪制搖桿 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) { } // 設置回調接口 public void setRudderListener(RudderListener rockerListener) { listener = rockerListener; } // 回調接口 public interface RudderListener { void onSteeringWheelChanged(float cross, float longitudinal); } }
主窗體中
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); } }
----------------------主窗體XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" > <GridLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:columnCount="3" > <com.example.blt.MySurfaceView android:id="@+id/rudder" android:layout_width="150dp" android:layout_height="150dp" android:layout_column="0" android:layout_gravity="left|bottom" /> <TextView android:id="@+id/textView2" android:layout_column="1" android:layout_gravity="left|top" android:text="TextView" /> <TableLayout android:layout_column="2" android:layout_gravity="bottom" android:layout_row="0" > <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/axis_x" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /> </TableRow> <TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content" > <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="自動" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> </TableRow> <TableRow android:id="@+id/tableRow3" android:layout_width="wrap_content" android:layout_height="wrap_content" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> </TableRow> <TableRow android:id="@+id/tableRow4" android:layout_width="wrap_content" android:layout_height="wrap_content" > <Button android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> <Button android:id="@+id/button6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> </TableRow> </TableLayout> </GridLayout> </RelativeLayout>
感謝各位的閱讀!關于“Android如何實現360度搖桿”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。