1

There are many questions like this, but most of them are asking on HOW to set GestureDetector to a View. I have done that already, and now I'm wondering, that android has provided nothing to let an image fling. The problem I have is that I don't know what to do with the velocityX/Y provided in that method. My last try looks like this:

public class TouchableImage extends SizeableImage 
    implements GestureDetector.OnGestureListener, 
    GestureDetector.OnDoubleTapListener,
    OnTouchListener{

    protected GestureDetector mDetector;

    protected Matrix mCurrentMatrix;

    public TouchableImage(Context context) {
        super(context);
        init();
    }

    public TouchableImage(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    protected void init() {
        mCurrentMatrix = new Matrix();
        mDetector = new GestureDetector(getContext(), this);
        mDetector.setOnDoubleTapListener(this);
        if (hasImage()){
            onNewImage();

        }
    }

    private void onNewImage() {
        setOnTouchListener(this);
        setScaleType(ScaleType.MATRIX);
        setImageMatrix(mCurrentMatrix);
    }

    @Override
    public void setImageDrawable(Drawable d){
        super.setImageDrawable(d);
        onNewImage();
    }



    @Override
    public void setImageBitmap(Bitmap bm) {
        super.setImageBitmap(bm);
        onNewImage();
    }

    public boolean hasImage(){
        return getDrawable() != null;
    }

    /*GestureDetector.OnGestureListener */
    @Override
    public boolean onDown(MotionEvent e) {
        // do nothing on one finger down
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        LogCat.d("ZoomableImage_FLING", "x:" + velocityX + " y:" + velocityY);
        float distanceX = velocityX / 1000;
        float distanceY = velocityY/ 1000;
        long end = System.currentTimeMillis() + (long)(Math.abs(velocityX)> Math   .abs(velocityY) ? Math.abs(velocityX) / 8: Math.abs(velocityY)/8);
            long now = System.currentTimeMillis();

        while (now < end){
            FlingRunnable fr = new FlingRunnable(e1, e2, distanceX, distanceY);
            postDelayed(fr, 10);
            distanceX -= distanceX / 100;
            distanceY -= distanceY / 100;
            now = System.currentTimeMillis();
        }

        return true;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        // do nothing onLongPress       
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
            float distanceY) {
        LogCat.d("ZoomableImage_SCROLL", "x:" + distanceX + " y:" + distanceY);
        mCurrentMatrix.postTranslate(distanceX * -1, distanceY *-1);
        setImageMatrix(mCurrentMatrix);
        return true;
    }

    @Override
    public void onShowPress(MotionEvent e) {
        // do nothing onShowPress
    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        // do nothing onSingleTapUp
        return true;
    }


    /*GestureDetector.OnDoubleTapListener*/
    @Override
    public boolean onDoubleTap(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean onDoubleTapEvent(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }

    /* Images onTouchListener */
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        /* simply dispatch the motion Events to our GestureDetector */
        return mDetector.onTouchEvent(event);
    }


    /* sizeable Image*/
    @Override
    public Matrix getCurrentMatrix() {
        return mCurrentMatrix;
    }

    @Override
    public void setImageBounds(PointF point) {

    }

    public class FlingRunnable implements Runnable{


        public MotionEvent e1;
        public MotionEvent e2;
        public float distanceX;
        public float distanceY;

        public FlingRunnable(MotionEvent e1, MotionEvent e2, float distanceX, floa t distanceY){
                this.e1 = e1;
            this.e2 = e2;
            this.distanceX = distanceX;
            this.distanceY = distanceY;
        }

        @Override
        public void run() {
            onScroll(e1, e2, distanceX, distanceY);
        }

    }

}    

One problem that I have is to find reasonable values for distanceX/Y and for end. Another problem that I have is that even if I have some values, the fling() seems to appear "at once" so like all onScroll() calls are added and applied at the same time.

NOTE: My build-target is 2.1 (Eclair) so I cannot use better detectors invented in 2.2 (Froyo)

mstill3
  • 132
  • 1
  • 2
  • 13
Rafael T
  • 15,401
  • 15
  • 83
  • 144

1 Answers1

-1

Look this code,

public class gesture extends Activity implements OnGestureListener
{
private GestureDetector gestureScanner;

@Override
public void onCreate(Bundle savedInstanceState)
 {
super.onCreate(savedInstanceState);
gestureScanner = new GestureDetector(this);
 Toast.makeText(gesture.this, "Perform Some gestures on screen",  Toast.LENGTH_LONG).show();
 setContentView(R.layout.main);
}

 @Override
 public boolean onTouchEvent(MotionEvent me)
 {
 return gestureScanner.onTouchEvent(me);
 }

 @Override
 public boolean onDown(MotionEvent e)
 {
 Toast.makeText(gesture.this, "Motion Event Performed", Toast.LENGTH_LONG).show();
 return true;
   }

 @Override
 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
Toast.makeText(gesture.this, "Fling Event performed", Toast.LENGTH_LONG).show();
return true;
}

@Override
public void onLongPress(MotionEvent e)
{
Toast.makeText(gesture.this, "Long-Press event performed", Toast.LENGTH_LONG).show();
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
{
Toast.makeText(gesture.this, "Scroll event performed", Toast.LENGTH_LONG).show();
return true;
}

@Override
public void onShowPress(MotionEvent e)
{
Toast.makeText(gesture.this, "Show-Press event performed", Toast.LENGTH_LONG).show();
}

@Override
public boolean onSingleTapUp(MotionEvent e)
{
Toast.makeText(gesture.this, "Single-Tap event performed", Toast.LENGTH_LONG).show();
 return true;
}
}

Documentation of GestureListener and Tutorials project

Here is the best solution according to your question look at this answers:

Fling gesture detection on grid layout and How to make horizontal scrolling view in android

Another Fling tutorials: how to implement both ontouch and also onfling in a same listview?

Community
  • 1
  • 1
Horrorgoogle
  • 7,858
  • 11
  • 48
  • 81
  • Your Code Sample was not useful, and won't work as aspected: If someone "flings" over an Image, there are over 10 Events arriving in a second. A Toast is shown min 2 secs, so a Log will be much better. Also I have that already in my question. The Question I have is more WHAT to do, to let an image (which is larger than the screen) "afterscroll" if someone "Flings". Unfortunately none of your links was usefull, too – Rafael T Jan 03 '12 at 21:35