public class PageGallery extends Gallery implements OnScaleGestureListener, OnGestureListener{
private static final String TAG = "Multitouch eventi";
static final int NONE = 0;
static final int DRAG = 1;
int mode = NONE;
private Context c;
private final LayoutInflater mInflater;
private ScaleGestureDetector mScaleDetector;
private GestureDetector mDetector;
private float mScaleFacto开发者_运维百科r = 1.f;
float nuova_distanza_tocchi, vecchia_distanza_tocchi, init_x, init_y;
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();
PointF mid = new PointF();
PointF start = new PointF();
ImageView pagina;
public PageGallery(Context context, AttributeSet atSet) {
super(context, atSet);
mInflater = LayoutInflater.from(context);
c = context;
mDetector = new GestureDetector(c,this);
mScaleDetector = new ScaleGestureDetector(c, this);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
pagina = (ImageView) super.getSelectedView();
if (mDetector.onTouchEvent(event)) { // gesure detector per i movimenti
Log.d("onTouchEvent", "--[ MOVIMENTO ]--");
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
init_x = event.getX();
init_y = event.getY();
midPoint(mid, event);
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
mode = DRAG;
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
matrix.set(savedMatrix);
matrix.postTranslate(event.getX() - start.x, event.getY() - start.y);
}
break;
}
pagina = (ImageView) super.getSelectedView();
pagina.setImageMatrix(matrix);
return true;
}
else if(mScaleDetector.onTouchEvent(event)) { // scale detector per lo zoom
Log.d("onTouchEvent", "--[ SCALE ]--");
return true;
}
else
return false;
}
public boolean onScaleBegin(ScaleGestureDetector detector) {
Log.d(TAG, "-- onScaleBegin --");
matrix = pagina.getImageMatrix();
savedMatrix.set(matrix);
start.set(init_x, init_y);
return true;
}
public boolean onScale(ScaleGestureDetector detector) {
mScaleFactor *= detector.getScaleFactor();
mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f));
if (nuova_distanza_tocchi > 10f) {
matrix.set(savedMatrix);
matrix.postScale(mScaleFactor, mScaleFactor, mid.x, mid.y);
Log.d("ZOOMMING",matrix.toShortString());
}
else {
matrix.set(savedMatrix);
matrix.postTranslate(init_x - start.x, init_y - start.y);
Log.d("PANNING",matrix.toShortString());
}
pagina.setImageMatrix(matrix);
pagina.invalidate();
Log.d("MATRIX", matrix.toString());
return true;
}
public void onScaleEnd(ScaleGestureDetector detector) {
Log.d(TAG, "-- onScaleEnd --");
vecchia_distanza_tocchi = detector.getPreviousSpan();
nuova_distanza_tocchi = detector.getCurrentSpan();
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Log.d(TAG, "-- onFling --");
float velMax = 2500f;
float velMin = 1000f;
float velX = Math.abs(velocityX);
if (velX > velMax) {
velX = velMax;
} else if (velX < velMin) {
velX = velMin;
}
velX -= 600;
int k = 500000;
int speed = (int) Math.floor(1f / velX * k);
setAnimationDuration(speed);
int kEvent;
if (isScrollingLeft(e1, e2)) {
kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
} else {
kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
}
onKeyDown(kEvent, null);
return true;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
Log.d(TAG, "-- onScroll --");
return super.onScroll(e1, e2, distanceX, distanceY);
}
private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2){
return e2.getX() > e1.getX();
}
private void midPoint(PointF point, MotionEvent event) {
float x = event.getX(0) + event.getX(1);
float y = event.getY(0) + event.getY(1);
point.set(x / 2, y / 2);
}
}
I extends a Gallery that implements onScaleGestureListener and I set an onItemClickListener in the main activity.
This code works fine: I can pinch to zoom all the images view in my gallery and I can pan the ImageView that I have zoomed. But how can I disable the Gallery scroll when I zoom an image and then enable too? Sorry for my bad english Thnks!
I think you must use call the method dispatchTouchEvent()
in the class you want to handle the 'touch' event, instead of the Gallery
精彩评论