航班機票座位選擇-android

weixin_34007886發表於2016-07-29

點子出自dribbble
在這個基礎上加入了縮圖
下載程式碼

1194532-42f542a2fdb7f76c.png
seat_png.png
1194532-a61f72c0e0263bff.gif
seat_gif.gif

定義列舉把座位分為已選,要選,未選。
座位分左中右
艙位分頭等,二等,經濟,末尾。

   enum SeatState {
        Normal, Selected, Selecting
    }

   enum SeatType {
        Left, Middle, Right
    }
  
   enum CabinType {
        Frist, Second, Tourist, Last
    }

定義HashMap來存放位置資訊

HashMap<String, RectF> mSeats= new HashMap<>();

HashMap的key是位置的序號,比如4排5坐 「4#5」未做唯一標識。
value的型別是RectF來判斷點是否在改區域中,來控制選中和未選中的區分。

RectF.contains(x, y)

利用Matrix對機艙和機翼放縮和平移
機艙和機翼都是利用貝賽爾曲線畫的,這個就不具體貼程式碼了

 matrix.postScale(sx,sy);
 matrix.postTranslate(dx,dy);
 canvas.drawBitmap(getBitmapFuselage(rectFCabinWidth), matrix, mPaint);
public Bitmap getBitmapFuselage(float rectFCabinWidth) {
        Canvas canvas = null;

        int w = getMeasuredWidth();
        int h = getMeasuredHeight();


        if (mBitmapFuselage == null) {
            mBitmapFuselage = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
            canvas = new Canvas(mBitmapFuselage);
            pathFuselage.moveTo(w / 2f - rectFCabinWidth / 2f - dip2px(2),
                    rectFCabin.top + rectFCabinWidth / 2f);
            pathFuselage.cubicTo(
                    w / 2f - rectFCabinWidth / 4f,

                    rectFCabin.top - rectFCabinWidth * 1.2f,
                    w / 2f + rectFCabinWidth / 4f,
                    rectFCabin.top - rectFCabinWidth * 1.2f,
                    w / 2f + rectFCabinWidth / 2f + dip2px(2),
                    rectFCabin.top + rectFCabinWidth / 2f
            );


            rectFCabin.top = rectFCabin.top + dip2px(10);//機翼向下平移距離
            pathFuselage.lineTo(w / 2f + rectFCabinWidth / 2f + dip2px(2)
                    , rectFCabin.top + rectFCabin.height() / 3f);

            pathFuselage.lineTo(w
                    , rectFCabin.top + rectFCabin.height() * 0.55f);

            pathFuselage.lineTo(w
                    , rectFCabin.top + rectFCabin.height() * 0.55f + rectFCabin.width() * 0.8f);

            pathFuselage.lineTo(rectFCabin.right + rectFCabin.width() / 2 * 1.5f
                    , rectFCabin.top + rectFCabin.height() / 2f + rectFCabin.height() / 6f / 2f);

            pathFuselage.lineTo(w / 2f + rectFCabinWidth / 2f + dip2px(2)
                    , rectFCabin.top + rectFCabin.height() / 2f + rectFCabin.height() / 6f / 2f);
//
            pathFuselage.lineTo(w / 2f + rectFCabinWidth / 2f + dip2px(2)
                    , rectFCabin.bottom - rectFCabinWidth / 2f);


            pathFuselage.cubicTo(
                    w / 2f + rectFCabinWidth / 4f,

                    rectFCabin.bottom + rectFCabinWidth * 2.5f,
                    w / 2f - rectFCabinWidth / 4f,
                    rectFCabin.bottom + rectFCabinWidth * 2.5f,
                    w / 2f - rectFCabinWidth / 2f - dip2px(2)
                    , rectFCabin.bottom - rectFCabinWidth / 2f
            );

            pathFuselage.lineTo(w / 2f - rectFCabinWidth / 2f - dip2px(2)
                    , rectFCabin.top + rectFCabin.height() / 2f + rectFCabin.height() / 6f / 2f);
            pathFuselage.lineTo(rectFCabin.left - rectFCabin.width() / 2 * 1.5f
                    , rectFCabin.top + rectFCabin.height() / 2f + rectFCabin.height() / 6f / 2f);
            pathFuselage.lineTo(0
                    , rectFCabin.top + rectFCabin.height() * 0.55f + rectFCabin.width() * 0.8f);

            pathFuselage.lineTo(0
                    , rectFCabin.top + rectFCabin.height() * 0.55f);

            pathFuselage.lineTo(w / 2f - rectFCabinWidth / 2f - dip2px(2)
                    , rectFCabin.top + rectFCabin.height() / 3f);
            pathFuselage.close();
            mPaint.setColor(Color.WHITE);
            mPaint.setAlpha(150);
            canvas.drawPath(pathFuselage, mPaint);
        }

        return mBitmapFuselage;
    }

自定義手勢滑動監聽

public abstract class MoveListerner implements OnTouchListener,
        OnGestureListener {
    public static final int MOVE_TO_LEFT = 0;
    public static final int MOVE_TO_RIGHT = MOVE_TO_LEFT + 1;
    public static final int MOVE_TO_UP = MOVE_TO_RIGHT + 1;
    public static final int MOVE_TO_DOWN = MOVE_TO_UP + 1;

    private static final int FLING_MIN_DISTANCE = 150;
    private static final int FLING_MIN_VELOCITY = 50;
    private boolean isScorllStart = false;
    private boolean isUpAndDown = false;
    GestureDetector mGestureDetector;
    float x1 = 0;
    float x2 = 0;
    float y1 = 0;
    float y2 = 0;

    public MoveListerner(Activity context) {
        super();
        mGestureDetector = new GestureDetector(context, this);
    }

    float startX = 0;
    float startY = 0;


    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (mGestureDetector.onTouchEvent(event)) {
            if (MotionEvent.ACTION_DOWN == event.getAction()) {
//                Touch(event.getX(), event.getY());
                startX = event.getX();
                startY = event.getY();
                return true;
            } else if (MotionEvent.ACTION_UP == event.getAction()) {
                if (Math.abs(event.getX() - startX) < 5 &&
                        Math.abs(event.getY() - startY) < 5) {
                    Touch(event.getX(), event.getY());
                    return true;

                }

            }
        }
        // 處理手勢結束
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_UP:
                endGesture();
                break;
        }
        return false;
    }

    private void endGesture() {
        isScorllStart = false;
        isUpAndDown = false;
//        Log.e("a", "AA:over");
        moveOver();
    }

    public abstract void moveDirection(View v, int direction, float distanceX, float distanceY);

    public abstract void moveUpAndDownDistance(MotionEvent event, int distance, int distanceY);

    public abstract void moveOver();

    public abstract void Touch(float x, float y);


    @Override
    public boolean onDown(MotionEvent e) {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    public void onShowPress(MotionEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
                            float distanceY) {
        float mOldY = e1.getY();
        int y = (int) e2.getRawY();
        if (!isScorllStart) {
            if (Math.abs(distanceX) / Math.abs(distanceY) > 2) {
                // 左右滑動
                isUpAndDown = false;
                isScorllStart = true;
            } else if (Math.abs(distanceY) / Math.abs(distanceX) > 3) {
                // 上下滑動
                isUpAndDown = true;
                isScorllStart = true;
            } else {
                isScorllStart = false;
            }
        } else {
            // 算滑動速度的問題了
            if (isUpAndDown) {
                // 是上下滑動,關閉左右檢測
                if (mOldY + 5 < y) {
                    moveUpAndDownDistance(e2, -3, (int) distanceY);
                } else if (mOldY + 5 > y) {
                    moveUpAndDownDistance(e2, 3, (int) distanceY);
                }
            }
        }
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                           float velocityY) {
//        Log.e("a", "AA:A" + velocityX + ":" + velocityY);
        if (isUpAndDown)
            return false;
        if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE
                && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
            // Fling left
            moveDirection(null, MOVE_TO_LEFT, e1.getX() - e2.getX(), e1.getY() - e2.getY());
        } else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE
                && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
            // Fling right
            moveDirection(null, MOVE_TO_RIGHT, e2.getX() - e1.getX(), e2.getY() - e1.getY());
        } else if (e1.getY() - e2.getY() > FLING_MIN_DISTANCE
                && Math.abs(velocityY) > FLING_MIN_VELOCITY) {
            // Fling up
            moveDirection(null, MOVE_TO_UP, 0, e1.getY() - e2.getY());
        } else {
            // Fling down
            moveDirection(null, MOVE_TO_DOWN, 0, e2.getY() - e1.getY());
        }
        return false;
    }
}

Usage

        mFlightSeatView.startAnim(true);// false zoom in,true zoom out
        mFlightSeatView.setEmptySelecting();//clear 
        mFlightSeatView.setSeatSelected(row, column);

如果覺得還不錯,就給我打賞顆星星吧^^
程式碼下載

相關文章