Advertisement
Guest User

Untitled

a guest
Jul 31st, 2012
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.13 KB | None | 0 0
  1. Android: Drawing on Canvas in Scrollview
  2. public class DrawPoints extends myActivity {
  3.  
  4.  
  5. @Override
  6. public void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.routes);
  9.  
  10. }
  11.  
  12. public static class SampleView extends View {
  13. private Paint mPaint = new Paint();
  14. private float[] mPts;
  15.  
  16. /*here comes declaration of parametars
  17.  
  18.  
  19.  
  20. private void buildPoints() {
  21.  
  22. /*here comes some coding*/
  23.  
  24. }
  25. }
  26.  
  27. public SampleView(Context context, AttributeSet attributeset) {
  28. super(context, attributeSet);
  29.  
  30. buildPoints();
  31. }
  32.  
  33. @Override
  34. protected void onDraw(Canvas canvas) {
  35. Paint paint = mPaint;
  36.  
  37. //here also comes code
  38. }
  39. }
  40. }
  41.  
  42. <?xml version="1.0" encoding="utf-8"?>
  43. <ScrollView
  44. xmlns:android="http://schemas.android.com/apk/res/android"
  45. android:id="@+id/scrollView1"
  46. android:layout_width="fill_parent"
  47. android:layout_height="fill_parent" >
  48. <HorizontalScrollView
  49. android:id="@+id/scrollView2"
  50. android:layout_width="fill_parent"
  51. android:layout_height="fill_parent">
  52.  
  53. <!-- This code is just to make sure that scroll views work how
  54. I want them to work, image size is 625*351 px
  55. <ImageView
  56. android:id="@+id/image_View1"
  57. android:layout_width="fill_parent"
  58. android:layout_height="fill_parent"
  59. android:src="@drawable/bus_design"
  60. /> -->
  61. <my.package.DrawPoints.SampleView
  62. android:layout_width="fill_parent"
  63. android:layout_height="fill_parent" />
  64.  
  65.  
  66. </HorizontalScrollView>
  67. </ScrollView>
  68.  
  69. View v = new SampleView(this);
  70. addContentView(v, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
  71. ViewGroup.LayoutParams.FILL_PARENT));
  72.  
  73. View v = new SampleView(this);
  74. ScrollView.LayoutParams lp = new ScrollView.LayoutParams(1000, 1000);
  75.  
  76. addContentView(v, lp);
  77.  
  78. View v = new SampleView(this);
  79. FrameLayout fl = new FrameLayout(this);
  80. fl.findViewById(R.id.FrameLayout1);
  81. fl.addView(v);
  82.  
  83. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  84. android:id="@+id/app_layout" android:orientation="horizontal"
  85. android:layout_width="fill_parent" android:layout_height="fill_parent">
  86. <!-- SCENE -->
  87. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  88. android:id="@+id/scene_layout"
  89. android:drawingCacheQuality="low"
  90. android:orientation="horizontal" android:layout_width="fill_parent"
  91. android:layout_height="fill_parent">
  92. <com.testiranje.Kristijan.TwoDScrollView
  93. android:id="@+id/scene_scroller" android:drawingCacheQuality="low"
  94. android:scrollbars="horizontal"
  95.  
  96. android:layout_width="fill_parent" android:layout_height="fill_parent">
  97. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  98. android:id="@+id/scene_container"
  99. android:drawingCacheQuality="low"
  100. android:background="@drawable/map"
  101. android:layout_width="fill_parent" android:layout_height="fill_parent">
  102.  
  103. <com.testiranje.Kristijan.SampleView
  104. android:layout_height="fill_parent"
  105. android:layout_width="fill_parent">
  106.  
  107. </com.testiranje.Kristijan.SampleView>
  108.  
  109. <!-- <ImageView xmlns:android="http://schemas.android.com/apk/res/android"
  110. android:id="@+id/scene_background" android:drawingCacheQuality="low"
  111. android:background="@drawable/map"
  112. android:layout_width="fill_parent" android:layout_height="fill_parent" /> -->
  113. </RelativeLayout>
  114. </com.testiranje.Kristijan.TwoDScrollView>
  115. </RelativeLayout>
  116. </RelativeLayout>
  117.  
  118. public class SampleView extends View {
  119. private Paint mPaint = new Paint();
  120. private float[] mPts;
  121.  
  122. private static final float SIZE = 1000;
  123. private static final int SEGS = 50;
  124. private static final int X = 0;
  125. private static final int Y = 1;
  126.  
  127. private void buildPoints() {
  128. final int ptCount = (SEGS + 1) * 2;
  129. mPts = new float[ptCount * 2];
  130.  
  131. float value = 0;
  132. final float delta = SIZE / SEGS;
  133. for (int i = 0; i <= SEGS; i++) {
  134. mPts[i*4 + X] = SIZE - value;
  135. mPts[i*4 + Y] = 0;
  136. mPts[i*4 + X + 2] = 0;
  137. mPts[i*4 + Y + 2] = value;
  138. value += delta;
  139. }
  140. }
  141.  
  142. public SampleView(Context context){
  143. super(context);
  144. //initSampleView();
  145. buildPoints();
  146.  
  147. }
  148.  
  149. //This constructor is very important because withouth of this
  150. //you can't insert this view in xml
  151. public SampleView(Context context, AttributeSet attrs) {
  152. super(context, attrs);
  153. //initSampleView();
  154. buildPoints();
  155. }
  156.  
  157. /*private final void initSampleView() {
  158. mPaint = new Paint();
  159. mPaint.setAntiAlias(true);
  160. setPadding(3, 3, 3, 3);
  161. }*/
  162.  
  163. @Override
  164. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  165. setMeasuredDimension(measureWidth(widthMeasureSpec),
  166. measureHeight(heightMeasureSpec));
  167. }
  168.  
  169. /**
  170. * Determines the width of this view
  171. * @param measureSpec A measureSpec packed into an int
  172. * @return The width of the view, honoring constraints from measureSpec
  173. */
  174. private int measureWidth(int measureSpec) {
  175. int result = 0;
  176. //This is because of background image in relativeLayout, which is 1000*1000px
  177. measureSpec = 1001;
  178. int specMode = MeasureSpec.getMode(measureSpec);
  179. int specSize = MeasureSpec.getSize(measureSpec);
  180.  
  181. if (specMode == MeasureSpec.UNSPECIFIED) {
  182. // We were told how big to be
  183. result = specSize;
  184. }
  185.  
  186. return result;
  187. }
  188.  
  189. /**
  190. * Determines the height of this view
  191. * @param measureSpec A measureSpec packed into an int
  192. * @return The height of the view, honoring constraints from measureSpec
  193. */
  194. private int measureHeight(int measureSpec) {
  195. int result = 0;
  196. //This is because of background image in relativeLayout, which is 1000*1000px
  197. measureSpec = 1001;
  198. int specMode = MeasureSpec.getMode(measureSpec);
  199. int specSize = MeasureSpec.getSize(measureSpec);
  200.  
  201.  
  202. if (specMode == MeasureSpec.UNSPECIFIED) {
  203. // Here we say how Heigh to be
  204. result = specSize;
  205. }
  206. return result;
  207. }
  208.  
  209. @Override
  210. protected void onDraw(Canvas canvas) {
  211. Paint paint = mPaint;
  212.  
  213. canvas.translate(10, 10);
  214.  
  215. paint.setColor(Color.RED);
  216. paint.setStrokeWidth(0);
  217. canvas.drawLines(mPts, paint);
  218.  
  219.  
  220. paint.setColor(Color.BLUE);
  221. paint.setStrokeWidth(3);
  222. canvas.drawPoints(mPts, paint);
  223.  
  224. }
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement