android 贪食蛇教程,Android自定义View系列之《贪吃蛇大作战》方向操作键效果实现...

本文主要是介绍android 贪食蛇教程,Android自定义View系列之《贪吃蛇大作战》方向操作键效果实现...,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前段时间很火的一款贪吃蛇游戏,可玩性很高,几点规则改造就将传统的贪吃蛇改活了,当时我拿过13000多分,还嘚瑟了很久。今天来个教程10分钟实现它。。。额,不是,实现它的方向操作按钮效果,看下图左下角的那两个同心圆。

c8e4add4780a

贪吃蛇大作战

用户手指触碰屏幕任意位置,内圆就往用户手指那个方向移动至外圆边界内切,实现后效果图如下所示。

c8e4add4780a

效果图

先看两张图,分别是Android坐标系与Android View尺寸函数的含义,其中,Android坐标系往右x轴递增,往下y轴递增,不多说。

c8e4add4780a

Android坐标系

c8e4add4780a

Android-View-Size

下面开始编码

1)创建HandleView类,继承自View

/**

* 贪吃蛇大作战方向控制按钮效果

*/

public class HandleView extends View {

public HandleView(Context context) {

this(context, null);

}

public HandleView(Context context, AttributeSet attrs) {

this(context, attrs, 0);

}

public HandleView(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

}

@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

// TODO

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

}

@Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) {

super.onLayout(changed, left, top, right, bottom);

}

@Override protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

// TODO

}

}

上述代码可以作为几乎所有自定义View的初始代码模板。

方向操作按钮等宽等高,我们不想它在xml布局时被设置成宽高不等的长方形,所以需要在onMeasure函数里进行处理。

2)重载onMeasure

@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

setMeasuredDimension(getDefaultSize2(getSuggestedMinimumWidth(), widthMeasureSpec),

getDefaultSize2(getSuggestedMinimumHeight(), heightMeasureSpec));

int childWidthSize = getMeasuredWidth();

widthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidthSize, MeasureSpec.EXACTLY);

heightMeasureSpec = MeasureSpec.makeMeasureSpec(childWidthSize, MeasureSpec.EXACTLY);

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

}

/**

* Compare to: {@link android.view.View#getDefaultSize(int, int)}

* If mode is AT_MOST, return the child size instead of the parent size

* (unless it is too big).

*/

private static int getDefaultSize2(int size, int measureSpec) {

int result = size;

int specMode = MeasureSpec.getMode(measureSpec);

int specSize = MeasureSpec.getSize(measureSpec);

switch (specMode) {

case MeasureSpec.UNSPECIFIED:

result = size;

break;

case MeasureSpec.AT_MOST:

result = Math.min(size, specSize);

break;

case MeasureSpec.EXACTLY:

result = specSize;

break;

}

return result;

}

3)画外圆

public class HandleView extends View {

private Paint mPaintForCircle;

// ...

@Override protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

// 背景透明

canvas.drawColor(Color.TRANSPARENT);

// 外圆半径

int radiusOuter = getWidth() / 2;

// 内圆半径

int radiusInner = getWidth() / 5;

// 圆心坐标(cx,cy)

float cx = getWidth() / 2;

float cy = getHeight() / 2;

if (null == mPaintForCircle) {

mPaintForCircle = new Paint();

}

mPaintForCircle.setAntiAlias(true);

mPaintForCircle.setStyle(Paint.Style.FILL);

// 画外圆

mPaintForCircle.setColor(Color.argb(0x7f, 0x11, 0x11, 0x11));

canvas.drawCircle(cx, cy, radiusOuter, mPaintForCircle);

// TODO 画内圆

}

}

4)画内圆

内圆是运动的,它的位置与用户的手指触摸坐标有关,按照效果,用户可以触摸的范围是包裹HandleView的ViewGroup(FrameLayout之类的),这里先写个接口用于获取手指触摸坐标。

public class HandleView extends View {

private HandleReaction mHandleReaction;

public void setHandleReaction(HandleReaction handleReaction) {

mHandleReaction = handleReaction;

}

public interface HandleReaction {

/**

* 获取用户触摸坐标

* @return

*/

float[] getTouchPosition();

}

// ...

}

c8e4add4780a

示意图

内圆半径固定,位置由圆心的坐标决定,所以关键是得出内圆的圆心坐标随用户手指的触摸坐标的变化而变化的函数关系,让我们建立方程式:(用工具画太花时间,将就手画,见谅!P.S.好像回到中学有木有)

c8e4add4780a

建立方程组

c8e4add4780a

结果

由公式可得出,分母(开平方根那个数的值)是cx2和cy2都需要的公用的值,命名为ratio,计算代码如下。

float[] touchPosition = mHandleReaction.getTouchPosition();

double ratio = (radiusOuter - radiusInner) /

Math.sqrt(

Math.pow(touchPosition[0] - cx, 2) +

Math.pow(touchPosition[1] - cy, 2));

float cx2 = (float) (ratio * (touchPosition[0] - cx) + cx);

float cy2 = (float) (ratio * (touchPosition[1] - cy) + cy);

mPaintForCircle.setColor(Color.argb(0xff, 0x11, 0x11, 0x11));

canvas.drawCircle(cx2, cy2, radiusInner, mPaintForCircle);

5)获取触摸坐标

建立MainActivity,布局文件就不给出了,文末附带源码地址。

public class MainActivity extends AppCompatActivity

implements HandleView.HandleReaction, View.OnTouchListener {

private float[] mTouchPosition = null;

private HandleView mHandleView;

@Override protected void onCreate(@Nullable Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

FrameLayout frameLayout = (FrameLayout) findViewById(R.id.frameLayout);

frameLayout.setOnTouchListener(this);

mHandleView = (HandleView) findViewById(R.id.handleView);

mHandleView.setHandleReaction(this);

}

@Override public boolean onTouch(View view, MotionEvent motionEvent) {

switch (motionEvent.getAction()) {

case MotionEvent.ACTION_DOWN:

case MotionEvent.ACTION_MOVE: {

mTouchPosition = new float[2];

mTouchPosition[0] = motionEvent.getX();

mTouchPosition[1] = motionEvent.getY();

mHandleView.invalidate();

return true;

}

case MotionEvent.ACTION_UP: {

mTouchPosition = null;

mHandleView.invalidate();

return true;

}

}

return false;

}

@Override public float[] getTouchPosition() {

return mTouchPosition;

}

}

6)坐标修正

表面上,上面内圆圆心计算代码是正确的,但实际上,由于我们的HandleView通过接口从它的父布局那里拿到了触摸坐标与HandleView内部坐标的参考坐标系不是同一个,他们相差一个HandleView相对于它父布局的getLeft与getTop的偏移,参照图Android-View-Size,所以需要对计算代码进行修正,如下:

// 经过修正后的内圆圆心坐标代码

double ratio = (radiusOuter - radiusInner) /

Math.sqrt(

Math.pow(touchPosition[0] - cx - getLeft(), 2) +

Math.pow(touchPosition[1] - cy - getTop(), 2));

float cx2 = (float) (ratio * (touchPosition[0] - cx - getLeft()) + cx);

float cy2 = (float) (ratio * (touchPosition[1] - cy - getTop()) + cy);

最后附上源码地址

GitHub源码

这篇关于android 贪食蛇教程,Android自定义View系列之《贪吃蛇大作战》方向操作键效果实现...的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/154870

相关文章

Python中Tkinter GUI编程详细教程

《Python中TkinterGUI编程详细教程》Tkinter作为Python编程语言中构建GUI的一个重要组件,其教程对于任何希望将Python应用到实际编程中的开发者来说都是宝贵的资源,这篇文... 目录前言1. Tkinter 简介2. 第一个 Tkinter 程序3. 窗口和基础组件3.1 创建窗

基于C++的UDP网络通信系统设计与实现详解

《基于C++的UDP网络通信系统设计与实现详解》在网络编程领域,UDP作为一种无连接的传输层协议,以其高效、低延迟的特性在实时性要求高的应用场景中占据重要地位,下面我们就来看看如何从零开始构建一个完整... 目录前言一、UDP服务器UdpServer.hpp1.1 基本框架设计1.2 初始化函数Init详解

Java中Map的五种遍历方式实现与对比

《Java中Map的五种遍历方式实现与对比》其实Map遍历藏着多种玩法,有的优雅简洁,有的性能拉满,今天咱们盘一盘这些进阶偏基础的遍历方式,告别重复又臃肿的代码,感兴趣的小伙伴可以了解下... 目录一、先搞懂:Map遍历的核心目标二、几种遍历方式的对比1. 传统EntrySet遍历(最通用)2. Lambd

springboot+redis实现订单过期(超时取消)功能的方法详解

《springboot+redis实现订单过期(超时取消)功能的方法详解》在SpringBoot中使用Redis实现订单过期(超时取消)功能,有多种成熟方案,本文为大家整理了几个详细方法,文中的示例代... 目录一、Redis键过期回调方案(推荐)1. 配置Redis监听器2. 监听键过期事件3. Redi

SpringBoot全局异常拦截与自定义错误页面实现过程解读

《SpringBoot全局异常拦截与自定义错误页面实现过程解读》本文介绍了SpringBoot中全局异常拦截与自定义错误页面的实现方法,包括异常的分类、SpringBoot默认异常处理机制、全局异常拦... 目录一、引言二、Spring Boot异常处理基础2.1 异常的分类2.2 Spring Boot默

基于SpringBoot实现分布式锁的三种方法

《基于SpringBoot实现分布式锁的三种方法》这篇文章主要为大家详细介绍了基于SpringBoot实现分布式锁的三种方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、基于Redis原生命令实现分布式锁1. 基础版Redis分布式锁2. 可重入锁实现二、使用Redisso

SpringBoo WebFlux+MongoDB实现非阻塞API过程

《SpringBooWebFlux+MongoDB实现非阻塞API过程》本文介绍了如何使用SpringBootWebFlux和MongoDB实现非阻塞API,通过响应式编程提高系统的吞吐量和响应性能... 目录一、引言二、响应式编程基础2.1 响应式编程概念2.2 响应式编程的优势2.3 响应式编程相关技术

C#实现将XML数据自动化地写入Excel文件

《C#实现将XML数据自动化地写入Excel文件》在现代企业级应用中,数据处理与报表生成是核心环节,本文将深入探讨如何利用C#和一款优秀的库,将XML数据自动化地写入Excel文件,有需要的小伙伴可以... 目录理解XML数据结构与Excel的对应关系引入高效工具:使用Spire.XLS for .NETC

自定义注解SpringBoot防重复提交AOP方法详解

《自定义注解SpringBoot防重复提交AOP方法详解》该文章描述了一个防止重复提交的流程,通过HttpServletRequest对象获取请求信息,生成唯一标识,使用Redis分布式锁判断请求是否... 目录防重复提交流程引入依赖properties配置自定义注解切面Redis工具类controller

Nginx更新SSL证书的实现步骤

《Nginx更新SSL证书的实现步骤》本文主要介绍了Nginx更新SSL证书的实现步骤,包括下载新证书、备份旧证书、配置新证书、验证配置及遇到问题时的解决方法,感兴趣的了解一下... 目录1 下载最新的SSL证书文件2 备份旧的SSL证书文件3 配置新证书4 验证配置5 遇到的http://www.cppc