views:

19

answers:

0

Hi All. I'm trying to create a sample project using the ScrollView. I want to be able to draw a simple graphic, which is taller than the device screen height, in a sub view of the ScrollView such that it will vertically scroll. If I modify my below code slightly, such that I setContentView(cv) instead of setContentView(sv), the onDraw metod is called. However, with the code as below, the onDraw method of CustomView is never called. Any help is greatly appreciated.

Thanks, Matt

public class ScrollActivity extends Activity {
static Integer height;
static Integer width;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //get height and width of screen
    height = this.getResources().getDisplayMetrics().heightPixels;
    width = this.getResources().getDisplayMetrics().widthPixels;
    //create views
    ScrollView sv = new ScrollView(this);
    CustomView cv = new CustomView(this);
    //add custom view with drawn rectangle to scrollview
    sv.addView(cv);
    //set content view as scrollview
    setContentView(sv);

    CharSequence output = height.toString() + "..." + width.toString();
    Toast.makeText(getApplicationContext(), output, Toast.LENGTH_SHORT)
    .show();

}

private static class CustomView extends View {


    public CustomView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    protected void onDraw(Canvas canvas) {
        ShapeDrawable rect = new ShapeDrawable(new RectShape());
        rect.getPaint().setColor(0xFFFF0000);
        rect.setBounds(50, 50, width - 100, height - 100);
        rect.draw(canvas);
    }

}

}