Photo inside the image view should not go cross on dragging
Posted
by
TGMCians
on Stack Overflow
See other posts from Stack Overflow
or by TGMCians
Published on 2012-11-25T11:01:33Z
Indexed on
2012/11/25
11:03 UTC
Read the original article
Hit count: 207
android
|android-imageview
I want photo inside the imageview should not go outside on dragging. In my code when i start to drag bitmap inside the imageview its goes out from imageview but i want when it cross the imageview its should come at starting point of imageview.
How to achieve this. please help me for this.
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
scaleCount=scaleCount+scale;
angleCount = addAngle(angleCount, Math.toDegrees(angle));
Log.v("Positions", "X: "+x+" " + "Y: "+y);
Log.d("ScaleCount", String.valueOf(scaleCount));
Log.d("Angle", String.valueOf(angleCount));
if (!isInitialized) {
int w = getWidth();
int h = getHeight();
position.set(w / 2, h / 2);
isInitialized = true;
}
Paint paint = new Paint();
Log.v("Height and Width", "Height: "+ getHeight() + "Width: "+ getWidth());
transform.reset();
transform.postTranslate(-width / 2.0f, -height / 2.0f);
transform.postRotate((float) Math.toDegrees(angle));
transform.postScale(scale, scale);
transform.postTranslate(position.getX(), position.getY());
canvas.drawBitmap(bitmap, transform, paint);
canvas.restore();
BitmapWidth=BitmapWidth+bitmap.getScaledWidth(canvas);
BitmapHeight=BitmapHeight+bitmap.getScaledHeight(canvas);
try {
/*paint.setColor(0xFF007F00);
canvas.drawCircle(vca.getX(), vca.getY(), 30, paint);
paint.setColor(0xFF7F0000);
canvas.drawCircle(vcb.getX(), vcb.getY(), 30, paint);*/
/*paint.setColor(0xFFFF0000);
canvas.drawLine(vpa.getX(), vpa.getY(), vpb.getX(), vpb.getY(), paint);
paint.setColor(0xFF00FF00);
canvas.drawLine(vca.getX(), vca.getY(), vcb.getX(), vcb.getY(), paint);*/
}
catch(NullPointerException e) {
// Just being lazy here...
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
vca = null;
vcb = null;
vpa = null;
vpb = null;
x=event.getX();
y=event.getY();
try {
touchManager.update(event);
if (touchManager.getPressCount() == 1) {
vca = touchManager.getPoint(0);
vpa = touchManager.getPreviousPoint(0);
position.add(touchManager.moveDelta(0));
}
else {
if (touchManager.getPressCount() == 2) {
vca = touchManager.getPoint(0);
vpa = touchManager.getPreviousPoint(0);
vcb = touchManager.getPoint(1);
vpb = touchManager.getPreviousPoint(1);
VMVector2D current = touchManager.getVector(0, 1);
VMVector2D previous = touchManager.getPreviousVector(0, 1);
float currentDistance = current.getLength();
float previousDistance = previous.getLength();
if (currentDistance-previousDistance != 0) {
scale *= currentDistance / previousDistance;
}
angle -= VMVector2D.getSignedAngleBetween(current, previous);
/*angleCount=angleCount+angle;*/
}
}
invalidate();
}
catch(Exception exception) {
// Log.d("VM", exception.getMessage());
}
return true;
}
© Stack Overflow or respective owner