I'm scaling the canvas, when I move it, it is going out of the screen.
Screenshot of before translating(i.e., when canvas.onDraw()
executes for the first time)
After Move:
I need some condition to say that don't go beyond this point(x-axis - 20).
canvas.save();
int maxX, minX;
minX = /* some condition */;
maxX = /* some condition */;
if (mPosX > maxX)
mPosX = maxX;
if (mPosX < minX)
mPosX = minX;
canvas.translate(mPosX, 0);
canvas.scale(xScaleFactor, yScaleFactor,
px / xScaleFactor + left, py / yScaleFactor + canvas.getClipBounds().top);
paint.setColor(ContextCompat.getColor(getContext(), R.color.green));
canvas.drawRect(20, 20, width, height, paint);
While moving:
@Override
public boolean onTouchEvent(MotionEvent ev) {
mScaleDetector.onTouchEvent(ev);
int eid = ev.getAction();
switch (eid & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
px = ev.getX();
py = ev.getY();
mLastTouchX = px;
mActivePointerId = ev.getPointerId(0);
break;
case MotionEvent.ACTION_MOVE:
if (eid == MotionEvent.ACTION_POINTER_UP || eid == MotionEvent.ACTION_POINTER_DOWN) {
return false;
}
pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK)
>> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
final float x = ev.getX(pointerIndex);
final float dx = x - mLastTouchX;
mPosX += dx;
invalidate();
mLastTouchX = x;
break;
.......
Kindly help to get some condition which will restrict the rectangle from moving away from the screen. So that the left white space is not created. Every time the canvas invalidates, rectangle should start from position 20 of x-axis.
EDIT:
private Matrix m;
m = new Matrix();
m.mapRect(new RectF(20, 0, mPosX, 0));
// in onDraw()
canvas.save();
m.setTranslate(mPosX, 0);
m.setScale(xScaleFactor, yScaleFactor,
(px + canvas.getClipBounds().left)/ xScaleFactor, (py + canvas.getClipBounds().top)/ yScaleFactor);
canvas.concat(m);
paint.setColor(ContextCompat.getColor(getContext(), R.color.green));
canvas.drawRect(20, 20, width, height, paint);
still it is going out