a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 系統(tǒng) > Android > 正文

Android實(shí)現(xiàn)電影院選座效果

2019-10-21 21:26:35
字體:
供稿:網(wǎng)友

本文實(shí)例為大家分享了Android實(shí)現(xiàn)電影院選座效果展示的具體代碼,供大家參考,具體內(nèi)容如下

Android,電影院,選座

這是一個簡單的電影院選座效果,實(shí)現(xiàn)該效果大致分為三步:

1.自定義view進(jìn)行繪制;
2.手勢縮放效果的實(shí)現(xiàn);
3.手勢觸摸被選和未被選效果的實(shí)現(xiàn);

先來看第一步,效果的繪制;

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; //行數(shù) private int row; //列數(shù) 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; //標(biāo)識是否正在縮放 private boolean isScaling; private float scaleX, scaleY; //是否是第一次縮放 private boolean firstScale = true; private boolean isOnClick; private int downX, downY; private boolean pointer; //用于存儲已經(jīng)選在好的座位 public ArrayList<Point> list; /**  * 默認(rèn)的座位圖片的寬度,如果使用的自己的座位的圖片比這個尺寸大或者小,會縮放到這個大小  */ 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方法中進(jìn)行繪制;

@Override protected void onDraw(Canvas canvas) {  if (row <= 0 || column <= 0) {   return;  }  drawSeat(canvas);  super.onDraw(canvas); }

具體的繪制邏輯實(shí)在drawSeat(),方法中實(shí)現(xiàn)的;

/**  * 繪制  *  * @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);    }   }  } }

主要是計(jì)算繪制的位置,矩陣的縮放,根據(jù)是否被選進(jìn)行繪制不同的效果;

/**  * 繪制文字  *  * @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) + "座";  //實(shí)例化文字畫筆  TextPaint txtPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);  txtPaint.setColor(Color.WHITE);  //設(shè)置字體樣式  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畫筆進(jìn)行文字的繪制,在繪制文字的時候要注意基準(zhǔn)線;

/**  * 獲取基準(zhǔn)線  * @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方法中去實(shí)現(xiàn)具體的邏輯;

@Override public boolean onTouchEvent(MotionEvent event) {  int x = (int) event.getX();  int y = (int) event.getY();  //手勢縮放  scaleGuestureDetector.onTouchEvent(event);  //手勢  gestureDetector.onTouchEvent(event);  //獲取當(dāng)前操作的手指數(shù)量  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; }

剛觸摸去選擇的時候會有個手勢縮放的效果,手勢縮放系統(tǒng)提供了ScaleGestureDetector類可以很容易的實(shí)現(xiàn),具體的邏輯系統(tǒng)都已經(jīng)處理好了,在對應(yīng)的回調(diào)方法里面去實(shí)現(xiàn)就可以了;

/**  * 手勢縮放  */ ScaleGestureDetector scaleGuestureDetector = new ScaleGestureDetector(getContext(), new ScaleGestureDetector.OnScaleGestureListener() {  @Override  public boolean onScale(ScaleGestureDetector detector) {   //正在縮放的時候回調(diào)   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) {   //開始縮放的時候回調(diào)   return false;  }   @Override  public void onScaleEnd(ScaleGestureDetector detector) {   //縮放完成回調(diào)   isScaling = false;   firstScale = true;   } });

其他的手勢操作系統(tǒng)還提供了GestureDetector類,可以使用GestureDetector來實(shí)現(xiàn)具體的效果;

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;  } });

完成上面三步,效果也就大致實(shí)現(xiàn)了,提供外部設(shè)置的方法供調(diào)用就可以了;

/**  * 對外界提供的設(shè)置方法  * @param row  * @param column  */ public void setData(int row, int column) {  this.row = row;  this.column = column;  init();  invalidate(); }

源碼地址:Android實(shí)現(xiàn)電影院選座效果

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 中国一级大黄大黄大色毛片 | 日韩精品在线观看一区 | 国产激情偷乱视频一区二区三区 | 日本妇人成熟免费视频 | 91成人免费看片 | 精品视频在线观看 | 中文字幕高清 | 91视频国产区 | a视频在线播放 | 亚洲精彩视频在线观看 | 午夜视频在线观看网站 | 亚洲男人天堂2023 | 午夜国产精品视频 | 日本三级在线观看中文字 | 97久久精品人人澡人人爽 | 国产免费拔擦拔擦8x高清 | 久久免费小视频 | 99爱在线观看| 久久99热精品免费观看牛牛 | 九九精品免费 | 色欧美综合 | www.xxx免费 | 特级毛片在线 | 日本三级黄色大片 | 欧美日韩成人影院 | 国产色在线 | 黄色电影天堂 | 自拍小电影 | 守护甜心中文版 | 日韩电影一区二区三区 | 亚洲欧美在线人成swag | 国内在线一区 | 呦一呦二在线精品视频 | 成人精品在线视频 | 一区二区三区观看视频 | 91精品国产99久久久久久红楼 | 最新伦理片 | 日韩精品一区二区三区 | 成人午夜影院 | av在线播放免费 | 日韩精品一区二区三区中文在线 |