简单实现电影院选座效果php,Android实现电影院选座效果

2024-01-09 13:10

本文主要是介绍简单实现电影院选座效果php,Android实现电影院选座效果,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本文实例为大家分享了Android实现电影院选座效果展示的具体代码,供大家参考,具体内容如下

cbd66fc8c5594cd4d55293c249d7e83c.gif

这是一个简单的电影院选座效果,实现该效果大致分为三步:

1.自定义view进行绘制;

2.手势缩放效果的实现;

3.手势触摸被选和未被选效果的实现;

先来看第一步,效果的绘制;

public class MoveSeatView extends View {

private final boolean DBG = false;

private Paint paint = new Paint();

private Matrix matrix = new Matrix();

private Matrix tempMatrix = new Matrix();

//座位水平间距

private int spacing;

//座位垂直间距

private int verSpacing;

//行号宽度

private int numberWidth;

//行数

private int row;

//列数

private int column;

//可选座位的图片

private Bitmap seatBitmap;

//选中时座位的图片

private Bitmap checkedSeatBitmap;

private int lastX;

private int lastY;

//整个座位图的宽度

private int seatBitmapWidth;

private int seatBitmapHeight;

private float screenHeight;

//屏幕的最小宽度

private int defaultScreenWidth;

//标识是否正在缩放

private boolean isScaling;

private float scaleX, scaleY;

//是否是第一次缩放

private boolean firstScale = true;

private boolean isOnClick;

private int downX, downY;

private boolean pointer;

//用于存储已经选在好的座位

public ArrayList list;

/**

* 默认的座位图片的宽度,如果使用的自己的座位的图片比这个尺寸大或者小,会缩放到这个大小

*/

private float defaultImgW = 40;

private float defaultImgH = 34;

/**

* 座位图片的宽度

*/

private int seatWidth = 40;

/**

* 座位图片的高度

*/

private int seatHeight = 34;

private float zoom;

float xScalel = 1;

float yScalel = 1;

public MoveSeatView(Context context) {

this(context, null);

}

public MoveSeatView(Context context, AttributeSet attrs) {

this(context, attrs, 0);

}

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

super(context, attrs, defStyleAttr);

}

private void init() {

spacing = (int) dip2px(5);

verSpacing = (int) dip2px(10);

defaultScreenWidth = (int) dip2px(80);

seatBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.seat_default);

xScalel = defaultImgW / seatBitmap.getWidth();

yScalel = defaultImgH / seatBitmap.getHeight();

checkedSeatBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.seat_green);

seatBitmapWidth = (int) (column * seatBitmap.getWidth() * xScalel + (column - 1) * spacing);

seatBitmapHeight = (int) (row * seatBitmap.getHeight() * yScalel + (row - 1) * verSpacing);

paint.setColor(Color.RED);

numberWidth = (int) dip2px(20);

screenHeight = dip2px(20);

list = new ArrayList<>();

matrix.postTranslate(numberWidth + spacing, screenHeight + 1 + verSpacing);

}

}

上面这些都是一些初始化动作,接下来在onDraw方法中进行绘制;

@Override

protected void onDraw(Canvas canvas) {

if (row <= 0 || column <= 0) {

return;

}

drawSeat(canvas);

super.onDraw(canvas);

}

具体的绘制逻辑实在drawSeat(),方法中实现的;

/**

* 绘制

*

* @param canvas

*/

private void drawSeat(Canvas canvas) {

zoom = getMatrixScaleX();

float translateX = getTranslateX();

float translateY = getTranslateY();

float scaleX = zoom;

float scaleY = zoom;

for (int i = 0; i < row; i++) {

float top = i * seatBitmap.getHeight() * yScalel * scaleY + i * verSpacing * scaleY + translateY;

float bottom = top + seatBitmap.getHeight() * yScalel * scaleY;

for (int j = 0; j < column; j++) {

float left = j * seatBitmap.getWidth() * xScalel * scaleX + j * spacing * xScalel * scaleX + translateX;

float right = left + seatBitmap.getWidth() * xScalel * scaleX;

tempMatrix.setTranslate(left, top);

tempMatrix.postScale(xScalel, yScalel, left, top);

tempMatrix.postScale(scaleX, scaleY, left, top);

if (isHave(i, j)) {

//绘制被选

canvas.drawBitmap(checkedSeatBitmap, tempMatrix, paint);

//绘制文字

drawText(canvas, i, j, top, left);

} else {

//绘制普通

canvas.drawBitmap(seatBitmap, tempMatrix, paint);

}

}

}

}

主要是计算绘制的位置,矩阵的缩放,根据是否被选进行绘制不同的效果;

/**

* 绘制文字

*

* @param canvas

* @param row

* @param column

* @param top

* @param left

*/

private void drawText(Canvas canvas, int row, int column, float top, float left) {

String txt = (row + 1) + "排";

String txt1 = (column + 1) + "座";

//实例化文字画笔

TextPaint txtPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);

txtPaint.setColor(Color.WHITE);

//设置字体样式

txtPaint.setTypeface(Typeface.DEFAULT_BOLD);

float seatHeight = this.seatHeight * getMatrixScaleX();

float seatWidth = this.seatWidth * getMatrixScaleX();

txtPaint.setTextSize(seatHeight / 3);

//获取中间线

float center = seatHeight / 2;

float txtWidth = txtPaint.measureText(txt);

float startX = left + seatWidth / 2 - txtWidth / 2;

//只绘制一行文字

if (txt1 == null) {

canvas.drawText(txt, startX, getBaseLine(txtPaint, top, top + seatHeight), txtPaint);

} else {

canvas.drawText(txt, startX, getBaseLine(txtPaint, top, top + center), txtPaint);

canvas.drawText(txt1, startX, getBaseLine(txtPaint, top + center, top + center + seatHeight / 2), txtPaint);

}

if (DBG) {

Log.d("drawTest", "top" + top);

}

}

这里是使用TextPaint画笔进行文字的绘制,在绘制文字的时候要注意基准线;

/**

* 获取基准线

* @param p

* @param top

* @param bottom

* @return

*/

private float getBaseLine(Paint p, float top, float bottom) {

Paint.FontMetrics fontMetrics = p.getFontMetrics();

int baseLine = (int) ((bottom + top - fontMetrics.bottom - fontMetrics.top) / 2);

return baseLine;

}

这样大致的绘制做完成了,剩下的第二步和第三步都涉及到手势触摸,在onTouchEvent方法中去实现具体的逻辑;

@Override

public boolean onTouchEvent(MotionEvent event) {

int x = (int) event.getX();

int y = (int) event.getY();

//手势缩放

scaleGuestureDetector.onTouchEvent(event);

//手势

gestureDetector.onTouchEvent(event);

//获取当前操作的手指数量

int pointerCount = event.getPointerCount();

if (pointerCount > 1) {

//多手指操作

pointer = true;

}

switch (event.getAction()) {

case MotionEvent.ACTION_DOWN:

pointer = false;

downX = x;

downY = y;

invalidate();

break;

case MotionEvent.ACTION_UP:

autoScale();

break;

case MotionEvent.ACTION_MOVE:

if (!isScaling && !isOnClick) {

int downDX = Math.abs(x - downX);

int downDY = Math.abs(y - downY);

if ((downDX > 10 || downDY > 10) && !pointer) {

int dx = x - lastX;

int dy = y - lastY;

matrix.postTranslate(dx, dy);

invalidate();

}

}

lastX = x;

lastY = y;

isOnClick = false;

break;

}

return true;

}

刚触摸去选择的时候会有个手势缩放的效果,手势缩放系统提供了ScaleGestureDetector类可以很容易的实现,具体的逻辑系统都已经处理好了,在对应的回调方法里面去实现就可以了;

/**

* 手势缩放

*/

ScaleGestureDetector scaleGuestureDetector = new ScaleGestureDetector(getContext(), new ScaleGestureDetector.OnScaleGestureListener() {

@Override

public boolean onScale(ScaleGestureDetector detector) {

//正在缩放的时候回调

isScaling = true;

float scaleFactor = detector.getScaleFactor();

if (getMatrixScaleY() * scaleFactor > 3) {

scaleFactor = 3 / getMatrixScaleY();

}

if (firstScale) {

scaleX = detector.getCurrentSpanX();

scaleY = detector.getCurrentSpanY();

firstScale = false;

}

if (getMatrixScaleY() * scaleFactor < 0.5) {

scaleFactor = 0.5f * getMatrixScaleY();

}

matrix.postScale(scaleFactor, scaleFactor, scaleX, scaleY);

invalidate();

return true;

}

@Override

public boolean onScaleBegin(ScaleGestureDetector detector) {

//开始缩放的时候回调

return false;

}

@Override

public void onScaleEnd(ScaleGestureDetector detector) {

//缩放完成回调

isScaling = false;

firstScale = true;

}

});

其他的手势操作系统还提供了GestureDetector类,可以使用GestureDetector来实现具体的效果;

GestureDetector gestureDetector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() {

@Override

public boolean onSingleTapConfirmed(MotionEvent e) {

int x = (int) e.getX();

int y = (int) e.getY();

for (int i = 0; i < row; i++) {

for (int j = 0; j < column; j++) {

int tempX = (int) ((j * seatWidth + j * spacing) * getMatrixScaleX() + getTranslateX());

int maxTempX = (int) (tempX + seatWidth * getMatrixScaleX());

int tempY = (int) ((seatHeight * i + i * verSpacing) * getMatrixScaleY() + getTranslateY());

int maxTempY = (int) (tempY + seatHeight * getMatrixScaleY());

if (x >= tempX && x <= maxTempX && y >= tempY && y <= maxTempY) {

if (isHave(i, j)) {

remove(i, j);

} else {

list.add(new Point(i, j));

}

}

}

}

float currentScaleY = getMatrixScaleY();

if (currentScaleY < 1.7) {

scaleX = x;

scaleY = y;

zoomAnimate(currentScaleY, 1.9f);

}

invalidate();

return true;

}

});

完成上面三步,效果也就大致实现了,提供外部设置的方法供调用就可以了;

/**

* 对外界提供的设置方法

* @param row

* @param column

*/

public void setData(int row, int column) {

this.row = row;

this.column = column;

init();

invalidate();

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

这篇关于简单实现电影院选座效果php,Android实现电影院选座效果的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Python中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句

在 Spring Boot 中实现异常处理最佳实践

《在SpringBoot中实现异常处理最佳实践》本文介绍如何在SpringBoot中实现异常处理,涵盖核心概念、实现方法、与先前查询的集成、性能分析、常见问题和最佳实践,感兴趣的朋友一起看看吧... 目录一、Spring Boot 异常处理的背景与核心概念1.1 为什么需要异常处理?1.2 Spring B

Python位移操作和位运算的实现示例

《Python位移操作和位运算的实现示例》本文主要介绍了Python位移操作和位运算的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 位移操作1.1 左移操作 (<<)1.2 右移操作 (>>)注意事项:2. 位运算2.1

如何在 Spring Boot 中实现 FreeMarker 模板

《如何在SpringBoot中实现FreeMarker模板》FreeMarker是一种功能强大、轻量级的模板引擎,用于在Java应用中生成动态文本输出(如HTML、XML、邮件内容等),本文... 目录什么是 FreeMarker 模板?在 Spring Boot 中实现 FreeMarker 模板1. 环

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义

SpringMVC 通过ajax 前后端数据交互的实现方法

《SpringMVC通过ajax前后端数据交互的实现方法》:本文主要介绍SpringMVC通过ajax前后端数据交互的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价... 在前端的开发过程中,经常在html页面通过AJAX进行前后端数据的交互,SpringMVC的controll

Spring Security自定义身份认证的实现方法

《SpringSecurity自定义身份认证的实现方法》:本文主要介绍SpringSecurity自定义身份认证的实现方法,下面对SpringSecurity的这三种自定义身份认证进行详细讲解,... 目录1.内存身份认证(1)创建配置类(2)验证内存身份认证2.JDBC身份认证(1)数据准备 (2)配置依

利用python实现对excel文件进行加密

《利用python实现对excel文件进行加密》由于文件内容的私密性,需要对Excel文件进行加密,保护文件以免给第三方看到,本文将以Python语言为例,和大家讲讲如何对Excel文件进行加密,感兴... 目录前言方法一:使用pywin32库(仅限Windows)方法二:使用msoffcrypto-too

C#使用StackExchange.Redis实现分布式锁的两种方式介绍

《C#使用StackExchange.Redis实现分布式锁的两种方式介绍》分布式锁在集群的架构中发挥着重要的作用,:本文主要介绍C#使用StackExchange.Redis实现分布式锁的... 目录自定义分布式锁获取锁释放锁自动续期StackExchange.Redis分布式锁获取锁释放锁自动续期分布式

springboot使用Scheduling实现动态增删启停定时任务教程

《springboot使用Scheduling实现动态增删启停定时任务教程》:本文主要介绍springboot使用Scheduling实现动态增删启停定时任务教程,具有很好的参考价值,希望对大家有... 目录1、配置定时任务需要的线程池2、创建ScheduledFuture的包装类3、注册定时任务,增加、删