1

I've looked over the other questions about this and none apply to my situation. my onDraw method in a custom view is called constantly, about once ever .3 seconds.

I have a custom view, that draws a map of the inside of a building. The view is larger than the view area so I've wrapped the custom view inside a custom ScrollView and HorizontalScrollView. The onDraw method is large, so I can't post it here. However I have removed all code from onDraw and it did not change anything.

If I remove the use of the two scrolling views, onDraw is called once only and is only ever called again if I pinch zoom the map.

Edit: added in the full custom scroll class. the vertical scroll class is the same as this.

public class SCEHorizontalScrollViewBase extends HorizontalScrollView {
private GestureDetector mGestureDetector;


private String TAG = this.getClass().getName();

public SCEHorizontalScrollViewBase(Context context, AttributeSet attrs) {
    super(context, attrs);
    mGestureDetector = new GestureDetector(context, new XScrollDetector());
    setFadingEdgeLength(10);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    Log.d(TAG, "onInterceptTouchEvent");
    return super.onInterceptTouchEvent(ev) && mGestureDetector.onTouchEvent(ev) && ev.getPointerCount()==1;
}

// Return false if we're scrolling in the Y direction  
class XScrollDetector extends SimpleOnGestureListener {
    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        Log.d(TAG, "onScroll");
        super.onScroll(e1, e2, distanceX, distanceY);
        if(Math.abs(distanceY) < Math.abs(distanceX)) {
            return true;
        }
        return false;
    }
}

}

layout file added as well.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent" >

<mobile.base.SCEScrollViewBase
    android:fillViewport="true"
    android:id="@+id/warehouseScrollWidget"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:scrollbarFadeDuration="999999"
    android:scrollbarStyle="outsideOverlay" >

    <mobile.base.SCEHorizontalScrollViewBase
        android:fillViewport="true"
        android:id="@+id/warehouseHorizontalScrollWidget"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:scrollbarFadeDuration="999999"
        android:scrollbarStyle="outsideOverlay" >

        <mobile.base.widgets.SCEWidgetWarehouseView
            android:id="@+id/WarehouseView"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content" />
    </mobile.base.SCEHorizontalScrollViewBase>
</mobile.base.SCEScrollViewBase>
</RelativeLayout>

From my logs I get this.

08-27 15:28:32.376: mobile.base.SCEHorizontalScrollViewBase(21571): requestLayout
08-27 15:28:32.376: mobile.base.widgets.SCEWidgetWarehouseView(21571): requestLayout
08-27 15:28:32.386: mobile.base.SCEScrollViewBase(21571): requestLayout
08-27 15:28:32.386: mobile.base.SCEHorizontalScrollViewBase(21571): requestLayout
08-27 15:28:32.386: mobile.base.SCEScrollViewBase(21571): requestLayout
08-27 15:28:32.416: mobile.base.widgets.SCEWidgetWarehouseView(21571): onMeasure set 972.5,1765.0
08-27 15:28:32.416: mobile.base.widgets.SCEWidgetWarehouseView(21571): onMeasure set 972.5,1765.0
08-27 15:28:32.456: mobile.base.widgets.SCEWidgetWarehouseView(21571): onMeasure set 972.5,1765.0
08-27 15:28:32.456: mobile.base.widgets.SCEWidgetWarehouseView(21571): onMeasure set 972.5,1765.0
08-27 15:28:32.476: mobile.base.SCEScrollViewBase(21571): onDraw
08-27 15:28:32.486: mobile.base.SCEHorizontalScrollViewBase(21571): onDraw
08-27 15:28:32.486: mobile.base.widgets.SCEWidgetWarehouseView(21571): onDraw
08-27 15:28:32.877: mobile.base.SCEScrollViewBase(21571): onDraw
08-27 15:28:32.877: mobile.base.SCEHorizontalScrollViewBase(21571): onDraw
08-27 15:28:32.877: mobile.base.widgets.SCEWidgetWarehouseView(21571): onDraw
08-27 15:28:33.117: mobile.base.SCEScrollViewBase(21571): onDraw
08-27 15:28:33.117: mobile.base.SCEHorizontalScrollViewBase(21571): onDraw
08-27 15:28:33.117: mobile.base.widgets.SCEWidgetWarehouseView(21571): onDraw

Any ideas why it is doing this? It did not do this before, something changed and I don't know what. The screen used to scroll fairly faster before, and now it barely moves.

Barry Klecka
  • 73
  • 1
  • 8
  • Have you checked the docs to make sure you don't have to call to the super.onScroll() method? (just trying a lucky guess with that). Possibly post your whole class? – D-Dᴙum Aug 27 '12 at 19:47
  • Good thought but no, that didn't change anything. I added the full scroll class. I'm using two of these, one for horizontal and one for vertical. – Barry Klecka Aug 27 '12 at 20:36
  • Every `onDraw()` method in android will be called at the refresh rate of the screen e.g: 40 times per second on SDK < 16 and 60 times per second on SDK >=16 – tolgap Aug 27 '12 at 21:25
  • Are you sure about the that? If I remove the two scrolling views, onDraw for my custom view is called once at the beginning. Do you have a link to information on that? – Barry Klecka Aug 28 '12 at 13:19

1 Answers1

0

It might have something to do with the fact that listviews in scrollviews are a bad idea. Or at least they used to be. Its something to do with the underlying architecture of android. Romain Guy has commented on this post with regards to this fact How can I put a ListView into a ScrollView without it collapsing?

So i don't have an answer, i would hazard that there is some sort of invalidation going on thats causing the redraw.

My only solution, might be to move your code somewhere else and call it as a result of some other trigger. Or wrap the code to only execute every 1000th call.

Community
  • 1
  • 1
Emile
  • 11,451
  • 5
  • 50
  • 63