/* 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.sim3xml; 

2.  

3. import android.app.Activity

4. import android.content.Context

5. import android.graphics.Canvas

6. import android.graphics.Color

7. import android.graphics.Paint

8. import android.graphics.Path

9. import android.os.Bundle

10. import android.util.AttributeSet

11. import android.view.MotionEvent

12. import android.view.View

13. import android.view.View.OnClickListener

14. import android.widget.Button

15. import android.widget.SeekBar

16.  

17. public class MainActivity extends Activity

18.   private static SeekBar  sb; 

19.  

20.   @Override 

21.   protected void onCreate(Bundle savedInstanceState) { 

22.     super.onCreate(savedInstanceState); 

23.     setContentView(R.layout.activity_main); 

24.     Button bt = (Button) findViewById(R.id.button1); 

25.     bt.setOnClickListener(new OnClickListener() { 

26.       @Override 

27.       public void onClick(View vb) { 

28.         ((Cnv) findViewById(R.id.cnv1)).clear(); 

29.       } 

30.     }); 

31.     sb = (SeekBar) findViewById(R.id.seekBar1); 

32.   } 

33.  

34.   public static class Cnv extends View

35.     private Path  path; 

36.     private Paint  paint; 

37.     private Paint  txpaint; 

38.     int            nr  = 0

39.  

40.     @Override 

41.     protected void onDraw(Canvas c) { 

42.       c.drawColor(Color.LTGRAY); 

43.       c.drawPath(path, paint); 

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

45.     } 

46.  

47.     public Cnv(Context c, AttributeSet atr) { 

48.       super(c, atr); 

49.       path = new Path(); 

50.       paint = new Paint(); 

51.       paint.setAntiAlias(true); 

52.       paint.setColor(Color.RED); 

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

54.       txpaint = new Paint(); 

55.       txpaint.setAntiAlias(true); 

56.       txpaint.setColor(Color.BLUE); 

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

58.     } 

59.  

60.     @Override 

61.     public boolean onTouchEvent(MotionEvent ev) { 

62.       float x = ev.getX(); 

63.       float y = ev.getY(); 

64.       int val = sb.getProgress(); 

65.       paint.setStrokeWidth(val); 

66.       txpaint.setTextSize(val * 5); 

67.       switch (ev.getAction()) { 

68.         case MotionEvent.ACTION_DOWN: 

69.           path.moveTo(x, y); 

70.           break

71.         case MotionEvent.ACTION_MOVE: 

72.           path.lineTo(x, y); 

73.           invalidate(); 

74.           break

75.         case MotionEvent.ACTION_UP: 

76.           path.lineTo(x, y); 

77.           invalidate(); 

78.           break

79.       } 

80.       return true

81.     } 

82.  

83.     public void clear() { 

84.       path.rewind(); 

85.       invalidate(); 

86.     } 

87.   } 

88.

89.  

90. ==== res/layout/activity_main.xml ========= 

91. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

92.     xmlns:tools="http://schemas.android.com/tools" 

93.      android:id="@+id/LinearLayout1" 

94.     android:layout_width="match_parent" 

95.     android:layout_height="match_parent" 

96.     android:background="#ffffff00" 

97.     android:orientation="vertical" 

98.     tools:context=".MainActivity"

99.  

100.     <Button 

101.         android:id="@+id/button1" 

102.         android:layout_width="wrap_content" 

103.         android:layout_height="wrap_content" 

104.         android:text="Clear path" 

105.         android:textSize="20sp" /> 

106.  

107.     <SeekBar 

108.         android:id="@+id/seekBar1" 

109.         android:layout_width="match_parent" 

110.         android:layout_height="wrap_content" 

111.         android:max="33" /> 

112.      

113.      <view  

114.         xmlns:android="http://schemas.android.com/apk/res/android" 

115.         class="com.javalessons.android.sim3xml.MainActivity$Cnv" 

116.         android:id="@+id/cnv1" 

117.          android:layout_width="fill_parent" 

118.          android:layout_height="fill_parent"  /> 

119.  </LinearLayout