Image Translation,Rotation and Zooming in android
private Drawable mIcon;
TouchExampleView view;
in onCreate() {
view = new TouchExampleView(this);
setContentView( view );
}
public class TouchExampleView extends View {
private static final int INVALID_POINTER_ID = -1;
private float mPosX;
private float mPosY;
private float mLastTouchX;
private float mLastTouchY;
private int mActivePointerId = INVALID_POINTER_ID;
private ScaleGestureDetector mScaleDetector;
float gx=0,gy=0;
int dg=0;
float dg1=0;
private List<Float> mScaleFactor = new ArrayList<Float>();
private List<Float> gx1 = new ArrayList<Float>();
private List<Float> gy1 = new ArrayList<Float>();
float mScaleFactor1=1.f;
int mScaleIndex=0;
public TouchExampleView(Context context) {
this(context, null, 0);
}
public TouchExampleView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public TouchExampleView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mIcon = this.getResources().getDrawable(R.drawable.red_glass);
mIcon.setBounds(0, 0, mIcon.getIntrinsicWidth(), mIcon.getIntrinsicHeight());
mIcon.setAlpha(225);
mScaleFactor.add(1.f);
gx1.add(2f);
gy1.add(2f);
mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
// Let the ScaleGestureDetector inspect all events.
mScaleDetector.onTouchEvent(ev);
final int action = ev.getAction();
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN: {
final float x = ev.getX();
final float y = ev.getY();
Log.d("Touch","DOWN "+"new x and y"+x+" "+y+"old x and Y"+mLastTouchX+" "+mLastTouchY);
mLastTouchX = x;
mLastTouchY = y;
mActivePointerId = ev.getPointerId(0);
break;
}
case MotionEvent.ACTION_MOVE: {
final int pointerIndex = ev.findPointerIndex(mActivePointerId);
final float x = ev.getX(pointerIndex);
final float y = ev.getY(pointerIndex);
float x1 = ev.getX(0) - ev.getX(1);
float y1 = ev.getY(0) - ev.getY(1);
dg = (int)(Math.toDegrees(Math.atan2(y1, x1)));
if(x<mLastTouchX+10 && mLastTouchX>x)
{
dg+=5;
}
// Only move if the ScaleGestureDetector isn't processing a gesture.
if (!mScaleDetector.isInProgress()) {
final float dx = x - mLastTouchX;
final float dy = y - mLastTouchY;
mPosX += dx;
mPosY += dy;
invalidate();
}
mLastTouchX = x;
mLastTouchY = y;
break;
}
case MotionEvent.ACTION_UP: {
dg=0;
mActivePointerId = INVALID_POINTER_ID;
break;
}
case MotionEvent.ACTION_CANCEL: {
mActivePointerId = INVALID_POINTER_ID;
break;
}
case MotionEvent.ACTION_POINTER_UP: {
dg=0;
Log.d("Touch","ACTION_POINTER_UP");
final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK)
>> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
final int pointerId = ev.getPointerId(pointerIndex);
if (pointerId == mActivePointerId) {
// This was our active pointer going up. Choose a new
// active pointer and adjust accordingly.
int newPointerIndex = pointerIndex == 0 ? 1 : 0;
if(newPointerIndex<0)
newPointerIndex=0;
mLastTouchX = ev.getX(newPointerIndex);
mLastTouchY = ev.getY(newPointerIndex);
mActivePointerId = ev.getPointerId(newPointerIndex);
}dg=0;
break;
}
case MotionEvent.ACTION_POINTER_1_DOWN:Log.d("Touch","ACTION_POINTER_DOWN 1");break;
case MotionEvent.ACTION_POINTER_2_DOWN:Log.d("Touch","ACTION_POINTER_DOWN 2");break;
case MotionEvent.ACTION_POINTER_3_DOWN:Log.d("Touch","ACTION_POINTER_DOWN 3");break;
case MotionEvent.ACTION_POINTER_INDEX_SHIFT:Log.d("Touch","ACTION_POINTER_INDEX_SHIFT ");break;
}
return true;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
Log.d("Touch","event"+event+"keycode"+keyCode);
if(keyCode==KeyEvent.KEYCODE_DPAD_UP)
{
dg+=10;
}
return super.onKeyDown(keyCode, event);
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
Log.d("HI","baseline onDraw"+seekbarposition+" onDraw");
//Image Moving....
canvas.translate(mPosX, mPosY);
float msf=mScaleFactor.get(mScaleIndex);
//Image Rotation....
canvas.rotate((float) (seekbarposition*3.6));
Log.d("Roation Angle","Angle:"+dg1);
//Image Scaling....
canvas.scale(msf, msf);
mIcon.draw(canvas);
canvas.restore();
}
//Code for Image Scaling............
private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
mScaleFactor1 *= detector.getScaleFactor();
gx = detector.getFocusX();
gy = detector.getFocusY();
float centerX = view.getWidth()/ 2;
float centerY = view.getHeight()/ 2;
if(rotation){
gx1.add(gx);
gy1.add(gy);
mRotateIndex++;
if(mRotateIndex!=0)
{
float newX=centerX-gx;
float newY=centerY-gy;
dg1 = (float) (Math.toDegrees(Math.atan2(newY, newX))-90);
}
Log.d("Touch","onScale gx............... "+gx+"gyyyyyyyyyyyyyyyyy: "+gy
+"mscale factor"+mScaleFactor1);
// Don't let the object get too small or too large.
mScaleFactor1 = Math.max(0.1f, Math.min(mScaleFactor1, 1.0f));
Rect r=mIcon.getBounds();
Log.d("HI","Image height"+r.height());
if(zoom){
mScaleFactor.add(mScaleFactor1);
mScaleIndex++;
}
invalidate();
}
return true;
}
@Override
public void onScaleEnd(ScaleGestureDetector detector) {
// TODO Auto-generated method stub
super.onScaleEnd(detector);
}
}
}
private Drawable mIcon;
TouchExampleView view;
boolean zoom=true,rotation=false;
in onCreate() {
view = new TouchExampleView(this);
setContentView( view );
}
public class TouchExampleView extends View {
private static final int INVALID_POINTER_ID = -1;
private float mPosX;
private float mPosY;
private float mLastTouchX;
private float mLastTouchY;
private int mActivePointerId = INVALID_POINTER_ID;
private ScaleGestureDetector mScaleDetector;
float gx=0,gy=0;
int dg=0;
float dg1=0;
private List<Float> mScaleFactor = new ArrayList<Float>();
private List<Float> gx1 = new ArrayList<Float>();
private List<Float> gy1 = new ArrayList<Float>();
float mScaleFactor1=1.f;
int mScaleIndex=0;
public TouchExampleView(Context context) {
this(context, null, 0);
}
public TouchExampleView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public TouchExampleView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mIcon = this.getResources().getDrawable(R.drawable.red_glass);
mIcon.setBounds(0, 0, mIcon.getIntrinsicWidth(), mIcon.getIntrinsicHeight());
mIcon.setAlpha(225);
mScaleFactor.add(1.f);
gx1.add(2f);
gy1.add(2f);
mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
// Let the ScaleGestureDetector inspect all events.
mScaleDetector.onTouchEvent(ev);
final int action = ev.getAction();
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN: {
final float x = ev.getX();
final float y = ev.getY();
Log.d("Touch","DOWN "+"new x and y"+x+" "+y+"old x and Y"+mLastTouchX+" "+mLastTouchY);
mLastTouchX = x;
mLastTouchY = y;
mActivePointerId = ev.getPointerId(0);
break;
}
case MotionEvent.ACTION_MOVE: {
final int pointerIndex = ev.findPointerIndex(mActivePointerId);
final float x = ev.getX(pointerIndex);
final float y = ev.getY(pointerIndex);
float x1 = ev.getX(0) - ev.getX(1);
float y1 = ev.getY(0) - ev.getY(1);
dg = (int)(Math.toDegrees(Math.atan2(y1, x1)));
if(x<mLastTouchX+10 && mLastTouchX>x)
{
dg+=5;
}
// Only move if the ScaleGestureDetector isn't processing a gesture.
if (!mScaleDetector.isInProgress()) {
final float dx = x - mLastTouchX;
final float dy = y - mLastTouchY;
mPosX += dx;
mPosY += dy;
invalidate();
}
mLastTouchX = x;
mLastTouchY = y;
break;
}
case MotionEvent.ACTION_UP: {
dg=0;
mActivePointerId = INVALID_POINTER_ID;
break;
}
case MotionEvent.ACTION_CANCEL: {
mActivePointerId = INVALID_POINTER_ID;
break;
}
case MotionEvent.ACTION_POINTER_UP: {
dg=0;
Log.d("Touch","ACTION_POINTER_UP");
final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK)
>> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
final int pointerId = ev.getPointerId(pointerIndex);
if (pointerId == mActivePointerId) {
// This was our active pointer going up. Choose a new
// active pointer and adjust accordingly.
int newPointerIndex = pointerIndex == 0 ? 1 : 0;
if(newPointerIndex<0)
newPointerIndex=0;
mLastTouchX = ev.getX(newPointerIndex);
mLastTouchY = ev.getY(newPointerIndex);
mActivePointerId = ev.getPointerId(newPointerIndex);
}dg=0;
break;
}
case MotionEvent.ACTION_POINTER_1_DOWN:Log.d("Touch","ACTION_POINTER_DOWN 1");break;
case MotionEvent.ACTION_POINTER_2_DOWN:Log.d("Touch","ACTION_POINTER_DOWN 2");break;
case MotionEvent.ACTION_POINTER_3_DOWN:Log.d("Touch","ACTION_POINTER_DOWN 3");break;
case MotionEvent.ACTION_POINTER_INDEX_SHIFT:Log.d("Touch","ACTION_POINTER_INDEX_SHIFT ");break;
}
return true;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
Log.d("Touch","event"+event+"keycode"+keyCode);
if(keyCode==KeyEvent.KEYCODE_DPAD_UP)
{
dg+=10;
}
return super.onKeyDown(keyCode, event);
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
Log.d("HI","baseline onDraw"+seekbarposition+" onDraw");
//Image Moving....
canvas.translate(mPosX, mPosY);
float msf=mScaleFactor.get(mScaleIndex);
//Image Rotation....
canvas.rotate((float) (seekbarposition*3.6));
Log.d("Roation Angle","Angle:"+dg1);
//Image Scaling....
canvas.scale(msf, msf);
mIcon.draw(canvas);
canvas.restore();
}
//Code for Image Scaling............
private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
mScaleFactor1 *= detector.getScaleFactor();
gx = detector.getFocusX();
gy = detector.getFocusY();
float centerX = view.getWidth()/ 2;
float centerY = view.getHeight()/ 2;
if(rotation){
gx1.add(gx);
gy1.add(gy);
mRotateIndex++;
if(mRotateIndex!=0)
{
float newX=centerX-gx;
float newY=centerY-gy;
dg1 = (float) (Math.toDegrees(Math.atan2(newY, newX))-90);
}
Log.d("Touch","onScale gx............... "+gx+"gyyyyyyyyyyyyyyyyy: "+gy
+"mscale factor"+mScaleFactor1);
// Don't let the object get too small or too large.
mScaleFactor1 = Math.max(0.1f, Math.min(mScaleFactor1, 1.0f));
Rect r=mIcon.getBounds();
Log.d("HI","Image height"+r.height());
if(zoom){
mScaleFactor.add(mScaleFactor1);
mScaleIndex++;
}
invalidate();
}
return true;
}
@Override
public void onScaleEnd(ScaleGestureDetector detector) {
// TODO Auto-generated method stub
super.onScaleEnd(detector);
}
}
}
No comments:
Post a Comment