Skip to content

Instantly share code, notes, and snippets.

@SeongUgJung
Last active August 29, 2015 14:12
Show Gist options
  • Save SeongUgJung/273e5212d827d04ad64a to your computer and use it in GitHub Desktop.
Save SeongUgJung/273e5212d827d04ad64a to your computer and use it in GitHub Desktop.
Android RxJava 와 Lambda
@EView
public class MagneticPlateView extends View {
// ...중략...
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
drawIronPipe(canvas);
drawMagneticBall(canvas);
canvas.restore();
}
private void drawMagneticBall(Canvas canvas) {
canvas.drawCircle(ballCenterPoint.x, ballCenterPoint.y, ballRadious, ballPaint);
}
private void drawIronPipe(Canvas canvas) {
final int ballCenterX = ballCenterPoint.x;
final int ballCenterY = ballCenterPoint.y;
for (int rowIdx = 0; rowIdx < pipeRowCount; ++rowIdx) {
for (int colIdx = 0; colIdx < pipeColumnCount; ++colIdx) {
int startX = colIdx * (pipeLength + pipeMargin);
int startY = rowIdx * pipeLength;
double radian = Math.atan2(ballCenterX - startX, ballCenterY - startY);
int stopX = (int) (startX + Math.sin(radian) * pipeLength);
int stopY = (int) (startY + Math.cos(radian) * pipeLength);
canvas.drawLine(startX, startY, stopX, stopY, pipePaint);
}
}
}
}
@EActivity(R.layout.act_main)
public class MainActivity extends Activity {
private PublishSubject<MotionEvent> touchDownSubject;
private PublishSubject<MotionEvent> touchMoveSubject;
private float lastTouchedX = -1;
private float lastTouchedY = -1;
@ViewById(R.id.mpv_main)
MagneticPlateView magneticPlateView;
@AfterInject
void initObject() {
touchDownSubject = initTouchDownSubject();
touchMoveSubject = initTouchMoveSubject();
}
private PublishSubject<MotionEvent> initTouchMoveSubject() {
PublishSubject<MotionEvent> touchMoveSubject = PublishSubject.create();
touchMoveSubject
.filter(motionEvent -> motionEvent.getAction() == MotionEvent.ACTION_MOVE)
.map(motionEvent -> {
float touchX = motionEvent.getX();
float touchY = motionEvent.getY();
int[] diffTouchDistance = new int[2];
diffTouchDistance[0] = (int) (touchX - lastTouchedX);
diffTouchDistance[1] = (int) (touchY - lastTouchedY);
lastTouchedX = touchX;
lastTouchedY = touchY;
return diffTouchDistance;
})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(diffXY -> {
magneticPlateView.setTouchDiff(diffXY[0], diffXY[1]);
magneticPlateView.invalidate();
});
return touchMoveSubject;
}
private PublishSubject<MotionEvent> initTouchDownSubject() {
PublishSubject<MotionEvent> touchDownSubject = PublishSubject.create();
touchDownSubject
.filter(motionEvent -> motionEvent.getAction() == MotionEvent.ACTION_DOWN)
.observeOn(Schedulers.io())
.subscribe(motionEvent -> {
lastTouchedX = motionEvent.getX();
lastTouchedY = motionEvent.getY();
});
return touchDownSubject;
}
@Touch(R.id.mpv_main)
void onPlateTouch(View view, MotionEvent event) {
touchDownSubject.onNext(event);
touchMoveSubject.onNext(event);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment