Android自定义View游戏方向轮盘转向盘方向盘

在画之前做一些规划,把View分成6份:在这里插入图片描述
一份就是小圆的半径,两份就是大圆的半径,大圆的圆心始终是中心,小圆位置根据手指变化,当我们手指超出大圆的范围时候就把他固定在大圆的边缘上(利用相似三角形来计算位置)。

    private void computePosition(float x, float y) {
        float dx = x-width/2;
        float dy = y-height/2;
        float hpy = (float) Math.sqrt(dx*dx+dy*dy);
        if (hpy <= maxR){
            minX = x;
            minY = y;
        }else{
            minX = dx * maxR / hpy + width/2;//由于零点是在中心
            minY = dy * maxR / hpy + height/2;//由于零点是在中心
        }
    }

传入手指的位置x,y,计算出小圆的中心坐标minX,minY;
完整代码:


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

public class SteerView extends View {
    Paint pMax,pMin;
    int width,height;
    int maxR;
    int minR;
    float minX,minY;

    private void init(){
        pMax = new Paint();
        pMax.setAntiAlias(true);
        pMax.setColor(Color.parseColor("#999999"));

        pMin = new Paint();
        pMin.setAntiAlias(true);
        pMin.setColor(Color.parseColor("#555555"));
    }

    public SteerView(Context context) {
        super(context);
        init();
    }

    public SteerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                computePosition(event.getX(),event.getY());
                break;
            case MotionEvent.ACTION_UP:
                minX = width/ 2;
                minY = height / 2;
                break;
        }
        invalidate();
        return true;
    }

    private void computePosition(float x, float y) {
        float dx = x-width/2;
        float dy = y-height/2;
        float hpy = (float) Math.sqrt(dx*dx+dy*dy);
        if (hpy <= maxR){
            minX = x;
            minY = y;
        }else{
            minX = dx * maxR / hpy + width/2;//由于零点是在中心
            minY = dy * maxR / hpy + height/2;//由于零点是在中心
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.parseColor("#eeeeee"));
        canvas.drawCircle(width/2,height/2,maxR,pMax);
        canvas.drawCircle(minX,minY,minR,pMin);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        width = right-left;
        height = bottom-top;
        minR = Math.min(width,height) / 6;
        maxR = minR*2;
        minX = width/ 2;
        minY = height / 2;
    }
}

到这里我们就画出了轮盘的一个功能,最后我们可以在computePosition函数里添加一个返回角度的功能。
首先先写一个接口,用来回调角度值,这里直接在自定义View里面接着写了。

public interface AngleCallback{
        public void onEvent(float angle);
    }

定义来使用

AngleCallback angleCallback;

    public void setAngleCallback(AngleCallback angleCallback) {
        this.angleCallback = angleCallback;
    }

在computePosition函数里添加一个返回角度的代码。

        float angle = -(float)Math.toDegrees((float) Math.atan(dy/dx));
        if (dx < 0){//二三象限
            angle += 180;
        }else if(dy > 0){//四象限
            angle += 360;
        }
        if (angleCallback!=null)//
            angleCallback.onEvent(angle);

最终自定义view的代码:


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

public class SteerView extends View {
    Paint pMax,pMin;
    int width,height;
    int maxR;
    int minR;
    float minX,minY;

    AngleCallback angleCallback;

    public void setAngleCallback(AngleCallback angleCallback) {
        this.angleCallback = angleCallback;
    }

    private void init(){
        pMax = new Paint();
        pMax.setAntiAlias(true);
        pMax.setColor(Color.parseColor("#999999"));

        pMin = new Paint();
        pMin.setAntiAlias(true);
        pMin.setColor(Color.parseColor("#555555"));
    }

    public SteerView(Context context) {
        super(context);
        init();
    }

    public SteerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                computePosition(event.getX(),event.getY());
                break;
            case MotionEvent.ACTION_UP:
                minX = width/ 2;
                minY = height / 2;
                break;
        }
        invalidate();
        return true;
    }

    private void computePosition(float x, float y) {
        float dx = x-width/2;
        float dy = y-height/2;
        float hpy = (float) Math.sqrt(dx*dx+dy*dy);
        if (hpy <= maxR){
            minX = x;
            minY = y;
        }else{
            minX = dx * maxR / hpy + width/2;//由于零点是在中心
            minY = dy * maxR / hpy + height/2;//由于零点是在中心
        }

        float angle = -(float)Math.toDegrees((float) Math.atan(dy/dx));
        if (dx < 0){//二三象限
            angle += 180;
        }else if(dy > 0){//四象限
            angle += 360;
        }
        if (angleCallback!=null)//
            angleCallback.onEvent(angle);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.parseColor("#eeeeee"));
        canvas.drawCircle(width/2,height/2,maxR,pMax);
        canvas.drawCircle(minX,minY,minR,pMin);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        width = right-left;
        height = bottom-top;
        minR = Math.min(width,height) / 6;
        maxR = minR*2;
        minX = width/ 2;
        minY = height / 2;
    }

    public interface AngleCallback{
        public void onEvent(float angle);
    }
}

在layout布局里添加view

    <com.hongying.shakegame.SteerView
        android:id="@+id/steer"
        android:layout_width="200dp"
        android:layout_height="200dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

在MainActivity里打印一下角度值:

public class MainActivity extends AppCompatActivity {
    SteerView steerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        steerView = findViewById(R.id.steer);
        steerView.setAngleCallback(new SteerView.AngleCallback() {
            @Override
            public void onEvent(float angle) {
                Log.d("TAG", String.valueOf(angle));
            }
        });
    }
}

完成了
在这里插入图片描述
在这里插入图片描述

  • 15
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dear Mr. Ma

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值