本文实例为大家分享了Android实现电影院选座效果展示的具体代码,供大家参考,具体内容如下
这是一个简单的电影院选座效果,实现该效果大致分为三步:
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);
}
}