I am dragging , moving , zooming and rotating my image , all is working fine. but I have serious problem and that is my Image flicker due to some problem and starts appear from the top of screen. First let me show my code what I am doing and then I will tell you
**What I have done **
@Override
public boolean onTouch(View v, MotionEvent event) {
ImageView view = (ImageView) v;
view.setImageMatrix(matrix);
view.setScaleType(ImageView.ScaleType.MATRIX);
float scale;
// Dump touch event to log
// dumpEvent(event1);
// Handle touch events here...
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN: //first finger down only
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
Log.d(TAG, "mode=DRAG" );
mode = DRAG;
break;
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
if (oldDist > 10f) {
savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
}
lastEvent = new float[4];
lastEvent[0] = event.getX(0);
lastEvent[1] = event.getX(1);
lastEvent[2] = event.getY(0);
lastEvent[3] = event.getY(1);
d = rotation(event);
break;
case MotionEvent.ACTION_UP: //first finger lifted
case MotionEvent.ACTION_POINTER_UP: //second finger lifted
mode = NONE;
Log.d(TAG, "mode=NONE" );
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
toggleSaveButtonState();
// ...
matrix.set(savedMatrix);
// matrix.postTranslate(event.getX() - start.x, event.getY()
// - start.y);
matrix.getValues(matrixValues);
matrixX = matrixValues[2];
matrixY = matrixValues[5];
width = matrixValues[0] * (((view).getDrawable()
.getIntrinsicWidth() / 2));
height = matrixValues[4] * (((view).getDrawable()
.getIntrinsicHeight() / 2));
dx = event.getX() - start.x;
dy = event.getY() - start.y;
float newX = event.getX() - start.x;
Log.v("bounds out", "matrixsX=" + matrixX+ " dx" + dx);
//if image will go outside left bound
if (matrixX + dx < 0-width){
dx = -matrixX-width;
Log.v("bounds", "matrixsX=" + matrixX + " dx" + dx + " width" + width + "image width = " + view.getWidth()/2);
// if (dx != -0.0) {
//
// // dx=(float)-0.0;
// }}
}
//if image will go outside right bound
if (matrixX + dx + width > view.getWidth()) {
dx = view.getWidth() - matrixX - width;
Log.v("bounds", "matrixsX=" + matrixX + " dx" + dx + " width" + width + "image width = " + view.getWidth());
}
//if image will go oustside top bound
if (matrixY + dy < 0) {
dy = -matrixY;
}
//if image will go outside bottom bound
if (matrixY + dy + height > view.getHeight()) {
dy = view.getHeight() - matrixY - height;
}
matrix.postTranslate(dx, dy);
} else if (mode == ZOOM && event.getPointerCount() == 2) {
float newDist = spacing(event);
matrix.set(savedMatrix);
if (newDist > 10f) {
scale = newDist / oldDist;
float[] values = new float[9];
matrix.getValues(values);
float currentScale = values[Matrix.MSCALE_X];
if (scale * currentScale > MAX_ZOOM) {
scale = MAX_ZOOM / currentScale;
}
else if (scale * currentScale < MIN_ZOOM)
{ scale = MIN_ZOOM / currentScale;}
matrix.postScale(scale, scale, mid.x, mid.y);
}
if (lastEvent != null) {
newRot = rotation(event);
float r = newRot - d;
matrix.postRotate(r, view.getMeasuredWidth() / 2,
view.getMeasuredHeight() / 2);
}
}
break;
}
// Perform the transformation
view.setImageMatrix(matrix);
return true; // indicate event was handled
}
This piece of code I think you can found almost all over every where I have slightly updated it to put some limits such as going off the screen etc.
**Problem **
When I start my application I want it to be appear in bottom but some how it sticks to middle , and when I touch the image view it flicks , it looks like that it starts it self from top corner and then come down to the point where we have touched it and Also it becomes bigger on some devices and smaller on some devices.
I think this is due to this line.
view.setScaleType(ImageView.ScaleType.MATRIX);
if I remove this line and put this line in xml such as
android:scaleType="matrix"
Then My image stick to the top which I do not want to be happen
So what can I do and How can I make things possible this is what I want
What I Want
I want my Image to show on the bottom of the screen
I want my image never gets flicking effect and should never be re sized
I want the touch to be happen when touch on the image inside the Image view
Please tell me How can I get my desired results.