1414import java .util .Map ;
1515
1616public class MyCanvas extends View {
17- private Paint paint ;
18- private Path path ;
19- private Map <Path , Integer > paths ;
20- private int color ;
21- private float curX ;
22- private float curY ;
23- private float startX ;
24- private float startY ;
25- private PathsChangedListener listener ;
17+ private Paint mPaint ;
18+ private Path mPath ;
19+ private Map <Path , Integer > mPaths ;
20+ private PathsChangedListener mListener ;
21+
22+ private int mColor ;
23+ private float mCurX ;
24+ private float mCurY ;
25+ private float mStartX ;
26+ private float mStartY ;
2627
2728 public MyCanvas (Context context , AttributeSet attrs ) {
2829 super (context , attrs );
2930
30- path = new Path ();
31- paint = new Paint ();
32- paint .setColor (Color .BLACK );
33- paint .setStyle (Paint .Style .STROKE );
34- paint .setStrokeJoin (Paint .Join .ROUND );
35- paint .setStrokeCap (Paint .Cap .ROUND );
36- paint .setStrokeWidth (5f );
37- paint .setAntiAlias (true );
38-
39- paths = new LinkedHashMap <>();
40- paths .put (path , paint .getColor ());
31+ mPath = new Path ();
32+ mPaint = new Paint ();
33+ mPaint .setColor (Color .BLACK );
34+ mPaint .setStyle (Paint .Style .STROKE );
35+ mPaint .setStrokeJoin (Paint .Join .ROUND );
36+ mPaint .setStrokeCap (Paint .Cap .ROUND );
37+ mPaint .setStrokeWidth (5f );
38+ mPaint .setAntiAlias (true );
39+
40+ mPaths = new LinkedHashMap <>();
41+ mPaths .put (mPath , mPaint .getColor ());
4142 pathsUpdated ();
4243 }
4344
4445 public void setListener (PathsChangedListener listener ) {
45- this .listener = listener ;
46+ this .mListener = listener ;
4647 }
4748
4849 public void undo () {
49- if (paths .size () <= 0 )
50+ if (mPaths .size () <= 0 )
5051 return ;
5152
5253 Path lastKey = null ;
53- for (Path key : paths .keySet ()) {
54+ for (Path key : mPaths .keySet ()) {
5455 lastKey = key ;
5556 }
5657
57- paths .remove (lastKey );
58+ mPaths .remove (lastKey );
5859 pathsUpdated ();
5960 invalidate ();
6061 }
6162
6263 public void setColor (int newColor ) {
63- color = newColor ;
64+ mColor = newColor ;
6465 }
6566
6667 public Bitmap getBitmap () {
@@ -75,46 +76,46 @@ public Bitmap getBitmap() {
7576 protected void onDraw (Canvas canvas ) {
7677 super .onDraw (canvas );
7778
78- for (Map .Entry <Path , Integer > entry : paths .entrySet ()) {
79- paint .setColor (entry .getValue ());
80- canvas .drawPath (entry .getKey (), paint );
79+ for (Map .Entry <Path , Integer > entry : mPaths .entrySet ()) {
80+ mPaint .setColor (entry .getValue ());
81+ canvas .drawPath (entry .getKey (), mPaint );
8182 }
8283
83- paint .setColor (color );
84- canvas .drawPath (path , paint );
84+ mPaint .setColor (mColor );
85+ canvas .drawPath (mPath , mPaint );
8586 }
8687
8788 private void actionDown (float x , float y ) {
88- path .reset ();
89- path .moveTo (x , y );
90- curX = x ;
91- curY = y ;
89+ mPath .reset ();
90+ mPath .moveTo (x , y );
91+ mCurX = x ;
92+ mCurY = y ;
9293 }
9394
9495 private void actionMove (float x , float y ) {
95- path .quadTo (curX , curY , (x + curX ) / 2 , (y + curY ) / 2 );
96- curX = x ;
97- curY = y ;
96+ mPath .quadTo (mCurX , mCurY , (x + mCurX ) / 2 , (y + mCurY ) / 2 );
97+ mCurX = x ;
98+ mCurY = y ;
9899 }
99100
100101 private void actionUp () {
101- path .lineTo (curX , curY );
102+ mPath .lineTo (mCurX , mCurY );
102103
103104 // draw a dot on click
104- if (startX == curX && startY == curY ) {
105- path .lineTo (curX , curY + 2 );
106- path .lineTo (curX + 1 , curY + 2 );
107- path .lineTo (curX + 1 , curY );
105+ if (mStartX == mCurX && mStartY == mCurY ) {
106+ mPath .lineTo (mCurX , mCurY + 2 );
107+ mPath .lineTo (mCurX + 1 , mCurY + 2 );
108+ mPath .lineTo (mCurX + 1 , mCurY );
108109 }
109110
110- paths .put (path , paint .getColor ());
111+ mPaths .put (mPath , mPaint .getColor ());
111112 pathsUpdated ();
112- path = new Path ();
113+ mPath = new Path ();
113114 }
114115
115116 private void pathsUpdated () {
116- if (listener != null && paths != null ) {
117- listener .pathsChanged (paths .size ());
117+ if (mListener != null && mPaths != null ) {
118+ mListener .pathsChanged (mPaths .size ());
118119 }
119120 }
120121
@@ -125,8 +126,8 @@ public boolean onTouchEvent(MotionEvent event) {
125126
126127 switch (event .getAction ()) {
127128 case MotionEvent .ACTION_DOWN :
128- startX = x ;
129- startY = y ;
129+ mStartX = x ;
130+ mStartY = y ;
130131 actionDown (x , y );
131132 break ;
132133 case MotionEvent .ACTION_MOVE :
0 commit comments