ModeSwitchView (used for horizontal switching in the camera) and modeswitchview

Source: Internet
Author: User

ModeSwitchView (used for horizontal switching in the camera) and modeswitchview

Core

public class ModeSwitchView extends LinearLayout implements View.OnClickListener {    private ModeScroller mCameraScroller;    private TextView document;    private TextView idcard;    private TextView whiteboard;    private TextView paperwork;    public static final int MODE_DOCUMENT = 0;    public static final int MODE_IDCARD = 1;    public static final int MODE_WHITEBOARD = 2;    public static final int MODE_PAPERWORK = 3;    public static class IndexUtil {        public final static int MIN_INDEX = 0;        public final static int MAX_INDEX = 3;        public final static int DEFAULT_INDEX = ModeSwitchView.MODE_DOCUMENT;        private static int SELECTED_INDEX = DEFAULT_INDEX;        public static int getCurrentSelectedIndex() {            return SELECTED_INDEX;        }        public static void setSelectedIndex(int index) {            SELECTED_INDEX = index;        }    }    private OnStateChangeListener mListener;    public interface OnStateChangeListener {        void onStateChange(int state);    }    public void setOnStateChangeListener(OnStateChangeListener mListener) {        this.mListener = mListener;    }    public ModeSwitchView(Context context, AttributeSet attrs) {        super(context, attrs);        LayoutInflater.from(context).inflate(R.layout.argus_mode_scroller_layout, this, true);        mCameraScroller = (ModeScroller) findViewById(R.id.camera_scroller);        document = (TextView) mCameraScroller.findViewById(R.id.document);        idcard = (TextView) mCameraScroller.findViewById(R.id.idcard);        whiteboard = (TextView) mCameraScroller.findViewById(R.id.whiteboard);        paperwork = (TextView) mCameraScroller.findViewById(R.id.paperwork);        document.setOnClickListener(this);        idcard.setOnClickListener(this);        whiteboard.setOnClickListener(this);        paperwork.setOnClickListener(this);    }    public void moveLeft() {        if (IndexUtil.getCurrentSelectedIndex() <= IndexUtil.MIN_INDEX) {            return;        }        mCameraScroller.leftIndex = IndexUtil.getCurrentSelectedIndex() - 1;        mCameraScroller.rightIndex = IndexUtil.getCurrentSelectedIndex();        int dx = Math.round((mCameraScroller.getChildAt(mCameraScroller.leftIndex).getWidth() + mCameraScroller.getChildAt(mCameraScroller.rightIndex).getWidth()) / 2.0F);        if (!mCameraScroller.mScroller.isFinished()) {            return;        }        mCameraScroller.mScroller.startScroll(mCameraScroller.getScrollX(), 0, -dx, 0, mCameraScroller.duration);        mCameraScroller.scrollToNext(mCameraScroller.rightIndex, mCameraScroller.leftIndex);        IndexUtil.setSelectedIndex(IndexUtil.getCurrentSelectedIndex() - 1);        mCameraScroller.invalidate();        notifyUI(IndexUtil.getCurrentSelectedIndex());    }    public void moveRight() {        if (IndexUtil.getCurrentSelectedIndex() >= IndexUtil.MAX_INDEX) {            return;        }        mCameraScroller.leftIndex = IndexUtil.getCurrentSelectedIndex();        mCameraScroller.rightIndex = IndexUtil.getCurrentSelectedIndex() + 1;        int dx = Math.round((mCameraScroller.getChildAt(mCameraScroller.leftIndex).getWidth() + mCameraScroller.getChildAt(mCameraScroller.rightIndex).getWidth()) / 2.0F);        if (!mCameraScroller.mScroller.isFinished()) {            return;        }        mCameraScroller.mScroller.startScroll(mCameraScroller.getScrollX(), 0, dx, 0, mCameraScroller.duration);        mCameraScroller.scrollToNext(mCameraScroller.leftIndex, mCameraScroller.rightIndex);        IndexUtil.setSelectedIndex(IndexUtil.getCurrentSelectedIndex() + 1);        mCameraScroller.invalidate();        notifyUI(IndexUtil.getCurrentSelectedIndex());    }    public void moveTo(int index) {        int selectedIndex = IndexUtil.getCurrentSelectedIndex();        View selectView = mCameraScroller.getChildAt(selectedIndex);        View dstView = mCameraScroller.getChildAt(index);        int dx = Math.abs((int) ((selectView.getRight() - selectView.getWidth() / 2f) - (dstView.getRight() - dstView.getWidth() / 2f)));        if (index < selectedIndex) {            dx = -dx;        }        if (!mCameraScroller.mScroller.isFinished()) {            return;        }        mCameraScroller.mScroller.startScroll(mCameraScroller.getScrollX(), 0, dx, 0, mCameraScroller.duration);        mCameraScroller.scrollToNext(selectedIndex, index);        IndexUtil.setSelectedIndex(index);        mCameraScroller.invalidate();        notifyUI(index);    }    @Override    public void onClick(View v) {        switch (v.getId()) {            case R.id.document:                moveTo(MODE_DOCUMENT);                break;            case R.id.idcard:                moveTo(MODE_IDCARD);                break;            case R.id.whiteboard:                moveTo(MODE_WHITEBOARD);                break;            case R.id.paperwork:                moveTo(MODE_PAPERWORK);                break;        }    }    public void setCurrentMode(int index) {        if (index > IndexUtil.MAX_INDEX || index < IndexUtil.MIN_INDEX) {            return;        }        moveTo(index);    }    private void notifyUI(int index) {        if (mListener != null) {            mListener.onStateChange(index);        }    }    private boolean mScrolling;    private float touchDownX;    private float touchDownY;    @Override    public boolean onInterceptTouchEvent(MotionEvent event) {        switch (event.getAction()) {            case MotionEvent.ACTION_DOWN:                touchDownX = event.getX();                touchDownY = event.getY();                mScrolling = false;                break;            case MotionEvent.ACTION_MOVE:                int slop = ViewConfiguration.get(getContext()).getScaledTouchSlop();                if (Math.abs(touchDownX - event.getX()) >= slop || Math.abs(touchDownY - event.getY()) >= slop) {                    mScrolling = true;                } else {                    mScrolling = false;                }                break;            case MotionEvent.ACTION_UP:                mScrolling = false;                break;        }        return mScrolling;    }    @Override    public boolean onTouchEvent(MotionEvent event) {        switch (event.getAction()) {            case MotionEvent.ACTION_DOWN:                return true;            case MotionEvent.ACTION_UP:                int deltaX = (int) (event.getX() - touchDownX);                int deltaY = (int) (event.getY() - touchDownY);                int slop = ViewConfiguration.get(getContext()).getScaledTouchSlop();                if (Math.abs(deltaX) > slop) {                    // swipe left                    if (deltaX < -Math.abs(deltaY) * 2) {                        moveRight();                    }                    // swipe right                    if (deltaX >= Math.abs(deltaY) * 2) {                        moveLeft();                    }                }                break;        }        return super.onTouchEvent(event);    }}
public class ModeScroller extends ViewGroup {    public Scroller mScroller;    public int leftIndex;    public int rightIndex;    public int duration = 250;    public ModeScroller(Context context, AttributeSet attrs) {        super(context, attrs);        mScroller = new Scroller(context);    }    public final void scrollToNext(int preIndex, int nextIndex) {        TextView selectedText = (TextView) getChildAt(preIndex);        if (selectedText != null) {            selectedText.setTextColor(getResources().getColor(android.R.color.white));        }        selectedText = (TextView) getChildAt(nextIndex);        if (selectedText != null) {            selectedText.setTextColor(getResources().getColor(R.color.chosen_color_yellow));        }    }    public void computeScroll() {        if (mScroller.computeScrollOffset()) {            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());            invalidate();        }        super.computeScroll();    }    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        measureChildren(widthMeasureSpec, heightMeasureSpec);        setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec));    }    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {        int cCount = getChildCount();        int selectedMode = ModeSwitchView.IndexUtil.getCurrentSelectedIndex();        int widthOffset = 0;        for (int i = 0; i < cCount; i++) {            View childView = getChildAt(i);            if (i < selectedMode) {                widthOffset += childView.getMeasuredWidth();            }        }        int childLeft = 0;        int childRight = 0;        for (int i = 0; i < cCount; i++) {            View childView = getChildAt(i);            if (i != 0) {                View preView = getChildAt(i - 1);                childLeft = preView.getRight();                childRight = childLeft + childView.getMeasuredWidth();            } else {                childLeft = (getWidth() - getChildAt(selectedMode).getMeasuredWidth()) / 2 - widthOffset + mScroller.getFinalX();                childRight = childLeft + childView.getMeasuredWidth();            }            childView.layout(childLeft, top, childRight, bottom);        }        TextView indexText = (TextView) getChildAt(ModeSwitchView.IndexUtil.getCurrentSelectedIndex());        indexText.setTextColor(getResources().getColor(R.color.chosen_color_yellow));    }}

Layout File

 
     
      
           
            
             
       
        
       
      
     
    
   
  
 

Style

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.