[转]SurfaceView horizontal scrolling

本文转自:http://stackoverflow.com/questions/6778883/surfaceview-horizontal-scrolling

问:

 

I'm writing an application which will create a graph and draw it on a SurfaceView.

Eventually the graph needs to be able to update live

but for now I want the SurfaceView to be scrollable horizontally

so that the user can see all the data.

Is this possible?

 

答:

 

You need to place your custom view inside a horizontal scroll view as a separeate class.

 When you create an instace of your custom graph view you tell it to size itself

according to the width of the graph by overriding the onMeasure method:

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {      
   
super.onMeasure(widthMeasureSpec, heightMeasureSpec);       
 
   
this.setMeasuredDimension(graphWidth, graphHeight); 
} 

graphWidth = barLenghtInPixels * barCount + extraSpaceInPixels;

You can place your customView in an xml layout using a custom tag like <com.myApplication.GraphView...>

or use myScrollView.addView(myCustomView) and add it into the HorizontalScrollView,

before you call setContentView(myLayout).

 

"HorizontalScrollView myScrollView = new HorizontalScrollView(this)" should be within onCreate

 

 

 

posted on 2012-06-04 13:43  freeliver54  阅读(1047)  评论(0编辑  收藏  举报

导航