Analyse and handle scroll gestures in Imageview (with canvas) without a ScrollView

231 views Asked by At

I've tried many things to handle my problem, but it didn't really work and google also couldn't help me. I've an ImageView which contains a canvas. The canvas draws a part of a graph. So when the user scrolls (horizontally) the canvas should draw another part of the graph. For example:

The canvas width is 200px

The canvas draws the graph from x=0 to x=200

The user scrolls (horizontally), he moves his finger over 100px

The canvas should now draw the graph from x=100 to x=300

The canvas should redraw by every pixel the user scrolls (for a smooth scrolling) This solution did not work: Draw the whole graph and put it in a Scrollview. This delivers a OutOfMemoryError, because the canvas/bitmap is too big.

Also I need to do some other things, so I need to do this like I described above.

Here is the Code:

XML:

<de.touristenfahrerforum.Marcel.Fragments.SpeedGraph
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:id="@+id/graph"
    android:background="@android:color/black"
    android:name="de.touristenfahrerforum.MarcelMoiser.Fragments.SpeedGraph">
</de.touristenfahrerforum.Marcel.Fragments.SpeedGraph>

Java Class:

public class SpeedGraph extends ImageView
{
...
public SpeedGraph(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

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

public SpeedGraph(Context context) {
    super(context);
}

public void setup( ArrayList<Location> locations, ScrollViewListener scrollViewListener )
{ 
...
    Bitmap bitmap = Bitmap.createBitmap(speedCanvasWidth,speedCanvasHeight,Bitmap.Config.ARGB_8888);
    canvas        = new Canvas(bitmap);
    this.setImageBitmap(bitmap);
...}
...
private void drawGraph(int start, int end, int color)
{
    graphPaint.setColor(color);

    long startTime = locations.get(start).getTime();

    Path path = new Path();
    path.moveTo((locations.get(start).getTime()-startTime)/10*V.LOGICAL_DENSITY, speedCanvasHeight-(int)(locations.get(start).getSpeed()*3.6*SIZE_FACTOR));

    for( int it = start; it < end; it++ )
    {
        Location location = locations.get(it);
        path.lineTo((location.getTime()-startTime)/10*V.LOGICAL_DENSITY, speedCanvasHeight-(int)(location.getSpeed()*3.6*SIZE_FACTOR));
    }

    canvas.drawPath(path,graphPaint);
}
...

So I would like to implement something that recognizes horizontal scroll gestures and gives me a number of pixels that shows me the pixels the user has scrolled.

Thank you guys in advance

1

There are 1 answers

0
Hömafreak On

It's the first time that nobody answered, but i figured out how to solve my problem. The best way is to implement GestureDetector.OnGestureListener and overwrite all it's methods. Also you have to create a GestureDetector Object. The method OnScroll(...) is where to look for the pixels the user has scrolled. Here is some code:

public class SpeedGraph extends ImageView implements GestureDetector.OnGestureListener
{
    private Context context;
...
public void setup( ... )
{
    this.gestureDetector = new GestureDetector(context, this);
    gestureDetector.setIsLongpressEnabled(false);
...}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
{
    scrolled+=distanceX;
    canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
    drawGrid();
    redrawGraphsInCanvas();
    this.invalidate();
    return true;
}

@Override
public boolean dispatchTouchEvent(MotionEvent ev){
    gestureDetector.onTouchEvent(ev);
    return true;
}

...