ANDROID - 在Gallery小部件中滚动,多点,平移ImageView

时间:2011-04-01 14:38:07

标签: scroll gallery zoom imageview pan

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 mScaleFactor = 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);
}
}

我扩展了一个实现onScaleGestureListener的Gallery,并在主活动中设置了一个onItemClickListener。

此代码工作正常:我可以捏缩放我的图库中的所有图像视图,我可以平移我缩放的ImageView。但是,当我缩放图像然后启用时,如何禁用图库滚动? 对不起,我的英语不好 Thnks!

1 个答案:

答案 0 :(得分:1)

我认为您必须在要处理“触摸”事件的班级中调用方法dispatchTouchEvent(),而不是使用图库