/* Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of JavaLessons.com
This code is for educational purposes only. Use at own risk.*/

1. package com.javalessons.android.simple3; 

2. import android.app.Activity

3. import android.content.Context

4. import android.graphics.Canvas

5. import android.graphics.Color

6. import android.graphics.Paint

7. import android.graphics.Path

8. import android.os.Bundle

9. import android.util.TypedValue

10. import android.view.MotionEvent

11. import android.view.View

12. import android.view.View.OnClickListener

13. import android.view.ViewGroup.LayoutParams

14. import android.widget.Button

15. import android.widget.LinearLayout

16. import android.widget.SeekBar

17.  

18. public class MainActivity extends Activity

19.   private LinearLayout ll;  

20.   private Button bt ; 

21.   private SeekBar sb ; 

22.   private Cnv cnv; 

23.   @Override 

24.   protected void onCreate(Bundle savedInstanceState) { 

25.     super.onCreate(savedInstanceState); 

26.  

27.     ll = new LinearLayout(this); 

28.     ll.setOrientation(LinearLayout.VERTICAL); 

29.     ll.setBackgroundColor(Color.YELLOW); 

30.     cnv = new Cnv(this); 

31.     bt = new Button(this); 

32.     bt.setText("Clear path"); 

33.     bt.setLayoutParams( 

34.         new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 

35.     bt.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20); 

36.  

37.     bt.setOnClickListener(  

38.       new OnClickListener() { 

39.       @Override 

40.       public void onClick(View vb) { 

41.         cnv.clear(); 

42.       } 

43.       }); 

44.     ll.addView(bt); 

45.  

46.     sb=new SeekBar(this); 

47.     sb.setMax(33); 

48.  

49.     ll.addView(sb); 

50.     ll.addView(cnv); 

51.  

52.     setContentView(ll); 

53.   } 

54.  

55.   public class Cnv extends View

56.     private Path    path; 

57.     private Paint   paint; 

58.     private Paint   txpaint; 

59.     int nr = 0

60.     @Override 

61.     protected void onDraw(Canvas c) { 

62.       int val = sb.getProgress(); 

63.       paint.setStrokeWidth(val); 

64.       txpaint.setTextSize(val*5); 

65.        

66.       c.drawColor(Color.LTGRAY); 

67.       c.drawPath(path, paint); 

68.       c.drawText("Testing " + (++nr), 22, 77, txpaint); 

69.     } 

70.  

71.     @Override 

72.     public boolean onTouchEvent(MotionEvent ev) { 

73.       float x = ev.getX(); 

74.       float y = ev.getY(); 

75.        

76.       switch (ev.getAction()) { 

77.       case MotionEvent.ACTION_DOWN: 

78.         path.moveTo(x, y); 

79.         break

80.       case MotionEvent.ACTION_MOVE: 

81.         path.lineTo(x, y); 

82.         invalidate(); 

83.         break

84.       case MotionEvent.ACTION_UP: 

85.         path.lineTo(x, y); 

86.         invalidate(); 

87.         break

88.       } 

89.       return true

90.     } 

91.     public void clear() { 

92.       path.rewind(); 

93.       invalidate(); 

94.     } 

95.     public Cnv(Context c) { 

96.       super(c); 

97.       path = new Path(); 

98.       paint = new Paint(); 

99.       paint.setAntiAlias(true); 

100.       paint.setColor(Color.RED); 

101.       paint.setStyle(Paint.Style.STROKE); 

102.       txpaint = new Paint(); 

103.       txpaint.setAntiAlias(true); 

104.       txpaint.setColor(Color.BLUE); 

105.       txpaint.setStyle(Paint.Style.STROKE); 

106.     } 

107.   } 

108.