java - Scrolling over large canvas -


i need understanding fundamentals of scrolling on items drawn canvas in android. suppose want create timeline time @ 0 top of visualization , time increased timeline continues rendered below previous point. if wish render on android know create bunch of items on canvas overriding ondraw(). however, let's suppose visualization bigger screen allows.

for example in first picture below large black box contains entire canvas render it. create blue line runs vertically , down several yellow, green , blue rectangles. red box represents screen of android rendering visualization. opens items drawn items contained within red box show on screen.

before_scroll

now if user scroll down, items appeared below red box in view while items have gone out of confines of red box no longer visable, represented in second picture.

after_scroll

i believe need use scrollables i'm quite lost how so. i've read on page http://developer.android.com/training/custom-views/custom-drawing.html explaining how create own customer images , page http://developer.android.com/training/custom-views/making-interactive.html explaining how make ui interactive, think i'm missing something.

a sample code illustrates problem (this basic, assume there logic dictating boxes/lines go, etc.) follows:

package com.example.scrolltest;  import com.example.scrolltest.draw;  import android.os.bundle; import android.app.activity; import android.graphics.color;  public class mainactivity extends activity {     draw draw;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);      draw = new draw(this);     draw.setbackgroundcolor(color.white);     setcontentview(draw);     } } 

and

package com.example.scrolltest;   import android.content.context; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.view.view;   public class draw extends view {     paint paint = new paint();      public draw(context context) {         super(context);                 }      @override     public void ondraw(canvas canvas) {          paint.setcolor(color.green);         canvas.drawrect(30, 30, 90, 200, paint);         paint.setcolor(color.blue);          canvas.drawline(100, 20, 100, 1900, paint);          paint.setcolor(color.green);         canvas.drawrect(200, 2000, 400, 3000, paint);         } } 

what cannot figure out though, how use scrollable scroll down rectangles off of screen. i'm unsure if started correctly or should use drawables instead...

simple method (if height required not large).

use scrollview , add draw view in it. compute required height view in onmeasure.

@override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);  draw = new draw(this); draw.setbackgroundcolor(color.white); scrollview scrollview = new scrollview(this); scrollview.addview(draw); setcontentview(scrollview); }  public class draw extends view {         paint paint = new paint();          public draw(context context) {             super(context);                     }          @override         protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {             // compute height required render view             // assume width match_parent.             int width = measurespec.getsize(widthmeasurespec);             int height = 3000 + 50; // since 3000 bottom of last rect drawn added , 50 padding.             setmeasureddimension(width, height);         }          @override         public void ondraw(canvas canvas) {              paint.setcolor(color.green);             canvas.drawrect(30, 30, 90, 200, paint);             paint.setcolor(color.blue);              canvas.drawline(100, 20, 100, 1900, paint);              paint.setcolor(color.green);             canvas.drawrect(200, 2000, 400, 3000, paint);             }     } 

Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -