0

I am trying to draw a bitmap in a ScrollView with multiple components in it. I made a custom view that extends a ScrollView and this custom view is being used as a parent of multiple views (TextViews mostly, all in all, it's just a big list that has a scrolling feature). In this custom ScrollView I'm calling onDraw and creating a bitmap in it which needs to be drawn on top of the other views in the custom ScrollView. My onDraw is as follows:

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    Bitmap bitmap = BitmapFactory.decodeResource(AppData.instance().getResources(),
            R.drawable.cabin);
    canvas.drawBitmap(bitmap, canvas.getWidth()/3, canvas.getHeight()/3, null);
}

I know that onDraw is getting called each time I scroll, so I assume there's some clash of the views in the layout. Pretty certain that the issue is related to having other views in the custom scrollView, since I also tried using this custom view in a plain activity like so:

DrawerScrollView.java

public class DrawerScrollView extends ScrollView {

    int mX;
    int mY;

    private OnScrollViewListener mOnScrollViewListener;

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

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

    public DrawerScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void setOnScrollViewListener(OnScrollViewListener onScrollViewListener) {
        mOnScrollViewListener = onScrollViewListener;
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        mOnScrollViewListener.onScrollChanged(this, l, t, oldl, oldt);
        mX = l;
        mY = t;
    }

    public interface OnScrollViewListener {
        void onScrollChanged(DrawerScrollView drawerScrollView, int l, int t, int oldl, int oldt);
    }

    @Override
    protected void onAttachedToWindow()
    {
        setLayerType(LAYER_TYPE_SOFTWARE, null);
    }

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    Bitmap bitmap = BitmapFactory.decodeResource(AppData.instance().getResources(),
            R.drawable.cabin);
    canvas.drawBitmap(bitmap, canvas.getWidth()/3, canvas.getHeight()/3, null);
}
}

This is the layout main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.blah.DrawerScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

    </com.blah.DrawerScrollView>

</LinearLayout>

And here's the activity MainActivity.java

public class MainActivity extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
        }
}

When I run this, the bitmap is being displayed in the Activity. But when the DrawerScrollView has other views in it and has to draw the bitmap on top of them (all the other views are being displayed), bitmap is not there. Btw, another insight is that in this case DrawerScrollView is a child of a DrawerLayout (hence the name). Any feedback would be greatly appreciated! I assume there might be some trickery in how ScrollView is being rendered, but I'm not sure.

Thanks a lot!

lulu
  • 261
  • 1
  • 4
  • 16
  • Did you take a look at [this](http://stackoverflow.com/questions/10303678/android-drawing-on-canvas-in-scrollview)? – Antrromet Feb 14 '15 at 01:31
  • Thanks for your comment @antrromet Just tried it, when I use onMeasure, my Bitmap does get drawn indeed, but it's the only thing that gets drawn, my actual layout with all of the views disappear (and I want my layout with the textviews and Bitmap drawn on top). Any suggestions? – lulu Feb 14 '15 at 04:36
  • Ok am not sure about that. How about you make your ScrollView background transparent and then in the full view group you set the background to that of your bitmap. In this way, the scrollview background will show the bitmap(as its background is transparent) and also the views on top of ScrollView will be drawn. – Antrromet Feb 14 '15 at 19:36

0 Answers0