[java]
view plain
copy
public class SlideThreeSelectView extends RelativeLayout { @InjectView(R.id.cartype_background) ImageView mBackgroundView; @InjectView(R.id.cartype_car) ImageView mCarTypeView; private DisplayMetrics mdDisplayMetrics; private int mHalfScreenWidth; public SlideThreeSelectView(Context context) { this(context, null); } public SlideThreeSelectView(Context context, AttributeSet attrs) { super(context, attrs); init(); } private void init() { initView(); initListener(); } private void initView() { View view = View.inflate(getContext(), R.layout.select_three_cartype_view, this); ButterKnife.inject(view); mdDisplayMetrics = new DisplayMetrics(); mdDisplayMetrics = getResources().getDisplayMetrics(); mHalfScreenWidth = mdDisplayMetrics.widthPixels / 2; } private void initListener() { mBackgroundView.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { int backgroundWidth = mBackgroundView.getWidth(); int offSetLength = backgroundWidth / 4; int carTypeViewHalfLength = mCarTypeView.getWidth() / 2; int carTypeViewHigh = mCarTypeView.getHeight(); switch (event.getAction()) { case MotionEvent.ACTION_UP: int upX = (int) event.getRawX(); updateCarTypeStatus(offSetLength, carTypeViewHalfLength, carTypeViewHigh, upX); break; default: break; } return true; } }); mCarTypeView.setOnTouchListener(new OnTouchListener() { private int mStartX = 0; @Override public boolean onTouch(View v, MotionEvent event) { int backgroundLeft = mBackgroundView.getLeft(); int backgroundWidth = mBackgroundView.getWidth(); int offSetLength = backgroundWidth / 4; int carTypeViewHalfLength = mCarTypeView.getWidth() / 2; int carTypeViewHigh = mCarTypeView.getHeight(); int upX; switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mStartX = (int) event.getRawX(); break; case MotionEvent.ACTION_MOVE: int newX = (int) event.getRawX(); int dX = newX - mStartX; int newl = mCarTypeView.getLeft() + dX; int newr = mCarTypeView.getRight() + dX; if (newl <= backgroundLeft || newr > (backgroundLeft + backgroundWidth)) { break; } mCarTypeView.layout( mCarTypeView.getLeft() + dX, mCarTypeView.getTop(), mCarTypeView.getRight() + dX, mCarTypeView.getBottom()); mStartX = (int) event.getRawX(); break; case MotionEvent.ACTION_CANCEL: upX = (int) event.getRawX(); updateCarTypeStatus(offSetLength, carTypeViewHalfLength, carTypeViewHigh, upX); break; case MotionEvent.ACTION_UP: upX = (int) event.getRawX(); updateCarTypeStatus(offSetLength, carTypeViewHalfLength, carTypeViewHigh, upX); break; default: break; } return true; } }); } private void updateCarTypeStatus(int offSetLength, int carTypeViewHalfLength, int carTypeViewHigh, int upX) { if (upX < mHalfScreenWidth - offSetLength) { mCarTypeView.layout( mBackgroundView.getLeft(), mBackgroundView.getTop(), mBackgroundView.getLeft() + (2 * carTypeViewHalfLength), mBackgroundView.getTop() + carTypeViewHigh); } else if (upX >= (mHalfScreenWidth - offSetLength) && upX <= (mHalfScreenWidth + offSetLength)) { mCarTypeView.layout( mBackgroundView.getLeft() + (2 * offSetLength) - carTypeViewHalfLength, mBackgroundView.getTop(), mBackgroundView.getLeft() + (2 * offSetLength) + carTypeViewHalfLength, mBackgroundView.getTop() + carTypeViewHigh); } else if (upX > (mHalfScreenWidth + offSetLength)) { mCarTypeView.layout( mBackgroundView.getLeft() + (4 * offSetLength) - (2 * carTypeViewHalfLength), mBackgroundView.getTop(), mBackgroundView.getLeft() + (4 * offSetLength), mBackgroundView.getTop() + carTypeViewHigh); } } }
绘制view思路
转载请注明原文地址: https://www.6miu.com/read-31107.html