From 15010c55a8ebefe12ad81130a7684feb75e5e680 Mon Sep 17 00:00:00 2001 From: OGIL Date: Thu, 5 Jul 2018 11:24:03 +0530 Subject: [PATCH] Bezier Curves implementation --- .idea/caches/build_file_checksums.ser | Bin 0 -> 593 bytes .idea/codeStyles/Project.xml | 29 +++ .idea/compiler.xml | 22 --- .idea/copyright/profiles_settings.xml | 3 - .idea/misc.xml | 22 +-- .idea/vcs.xml | 6 + .../com/raodevs/handdraw/MainActivity.java | 2 +- app/src/main/res/layout/activity_main.xml | 2 - build.gradle | 3 +- gradle/wrapper/gradle-wrapper.properties | 4 +- .../com/raodevs/touchdraw/TouchDrawView.java | 167 ++++++++++++++---- 11 files changed, 180 insertions(+), 80 deletions(-) create mode 100644 .idea/caches/build_file_checksums.ser create mode 100644 .idea/codeStyles/Project.xml delete mode 100644 .idea/compiler.xml delete mode 100644 .idea/copyright/profiles_settings.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser new file mode 100644 index 0000000000000000000000000000000000000000..27923ccfd39bc9ae887eb767dee035e7d81a0620 GIT binary patch literal 593 zcmZ4UmVvdnh`~NNKUXg?FQq6yGexf?KR>5fFEb@IQ7^qHF(oHeub?PDD>b=9F91S2 zm1gFoxMk*~I%lLNXBU^|7Q2L-Ts|(GuF1r}nf0c10S0M9AKpD!OAVEysZzA~hG*W3~Y zUH#DF)S_bjvdps7?4rc1g8cjveW)S&h!84BEh@=OEiNr&sAFIQnRG?A=Vm{{CYF~C zoHO41aH=a|;7J079L#>W!UHA8%9`%IYG*qXy!Be%mUkr#0y+7~i8-Kf$1qhzZA;dy z_d6rPjb}cetAE|Ogh8w%zce|+r6{pnAG`T|mRH=$HN_%irhA>OyYnZxgh3FGv$yod s319o#@hJ90@B`Nysq28QNh~P9VYHQjz^{3!Ca;Pm=JjPCOb)C70H31Nod5s; literal 0 HcmV?d00001 diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..30aa626 --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml deleted file mode 100644 index 96cc43e..0000000 --- a/.idea/compiler.xml +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml deleted file mode 100644 index e7bedf3..0000000 --- a/.idea/copyright/profiles_settings.xml +++ /dev/null @@ -1,3 +0,0 @@ - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 5d19981..c0f68ed 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,18 +1,16 @@ - - - @@ -27,17 +25,7 @@ - - - - - - - - - - - + diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/raodevs/handdraw/MainActivity.java b/app/src/main/java/com/raodevs/handdraw/MainActivity.java index c5529f8..0310e03 100644 --- a/app/src/main/java/com/raodevs/handdraw/MainActivity.java +++ b/app/src/main/java/com/raodevs/handdraw/MainActivity.java @@ -8,7 +8,7 @@ import android.widget.Button; import com.raodevs.touchdraw.TouchDrawView; -import com.raodevs.touchdraw.TrialTouchDrawView; +import com.raodevs.touchdraw.TouchDrawView; public class MainActivity extends AppCompatActivity { diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 063048c..b154817 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -6,8 +6,6 @@ android:layout_height="match_parent" tools:context="com.raodevs.handdraw.MainActivity"> - - points; + private Point last_sparse; private ArrayList> paths;//keeps record of every different path and paint properties associated with it private Stack> backup;//keeps a backup for redoing the changes @@ -39,6 +42,7 @@ public TouchDrawView(Context context, @Nullable AttributeSet attrs) { mContext = context; path = new Path(); paths = new ArrayList<>(); + points = new ArrayList<>(); backup = new Stack<>(); initPaint(attrs); } @@ -48,7 +52,8 @@ public TouchDrawView(Context context, @Nullable AttributeSet attrs) { protected void onDraw(Canvas canvas) { super.onDraw(canvas); Paint paint = myPen.getPen(); - for(Pair p : paths){ + + for (Pair p : paths) { canvas.drawPath(p.first, p.second.getPen()); } canvas.drawPath(path, paint); @@ -59,19 +64,27 @@ protected void onDraw(Canvas canvas) { public boolean onTouchEvent(MotionEvent event) { float X = (int) event.getX(); float Y = (int) event.getY(); - int eventaction = event.getAction(); - switch (eventaction) { + int index = event.getActionIndex(); + int pointerId = event.getPointerId(index); + int event_action = event.getAction(); + switch (event_action) { case MotionEvent.ACTION_DOWN: - path.moveTo(X, Y); - break; - case MotionEvent.ACTION_MOVE: - path.lineTo(X, Y); + int history_buffer_length = event.getHistorySize(); // contains more points than active buffer + for (int i = 0; i < history_buffer_length; i++) { + Point point = new Point(); + point.x = event.getHistoricalX(i); + point.y = event.getHistoricalY(i); + points.add(point); + } + createBezierPath(); break; case MotionEvent.ACTION_UP: Pair pair = new Pair<>(path, myPen); paths.add(pair); + // reset + points.clear(); path = new Path(); String c = myPen.getPaint_color(); float w = myPen.getStroke_width(); @@ -84,24 +97,61 @@ public boolean onTouchEvent(MotionEvent event) { return true; } + private void createBezierPath() { + boolean start = true; + if (points.size() > 1) { // use 2 points only + for (int i = points.size() - 2; i < points.size(); i++) { + if (i >= 0) { + Point point = points.get(i); + + if (i == 0) { // start point + Point next = points.get(i + 1); + point.extraX = ((next.x - point.x) / 3); + point.extraY = ((next.y - point.y) / 3); + } else if (i == points.size() - 1) { //end point + Point prev = points.get(i - 1); + point.extraX = ((point.x - prev.x) / 3); + point.extraY = ((point.y - prev.y) / 3); + } else { // any other point + Point next = points.get(i + 1); + Point prev = points.get(i - 1); + point.extraX = ((next.x - prev.x) / 3); + point.extraY = ((next.y - prev.y) / 3); + } + } + } + } + + for (int i = 0; i < points.size(); i++) { + Point point = points.get(i); + if (start) { + start = false; + path.moveTo(point.x, point.y); + } else { + Point prev = points.get(i - 1); + path.cubicTo(prev.x + prev.extraX, prev.y + prev.extraY, point.x - point.extraX, point.y - point.extraY, point.x, point.y); + } + } + } + //Initializes and set all the values of paint and background color by taking values from xml - private void initPaint(AttributeSet atr){ + private void initPaint(AttributeSet atr) { myPen = new Pen(); TypedArray typedArray = mContext.getTheme().obtainStyledAttributes(atr, R.styleable.Canvas, 0, 0); String t_color = typedArray.getString(R.styleable.Canvas_paint_color); String t_size = typedArray.getString(R.styleable.Canvas_paint_width); - if(t_color != null){ + if (t_color != null) { myPen.setPaint_color(Color.parseColor(t_color)); } - if(t_size != null){ + if (t_size != null) { myPen.setStrokeWidth(Float.valueOf(t_size)); } String background_color = typedArray.getString(R.styleable.Canvas_bg_color); - if(background_color != null){ + if (background_color != null) { try { this.setBackgroundColor(Color.parseColor(background_color)); bg_color = String.format("#%06X", (0xFFFFFF & Color.parseColor(background_color))); - }catch (Exception e){ + } catch (Exception e) { Log.d("TouchDrawView", e.toString()); } @@ -113,7 +163,7 @@ private void initPaint(AttributeSet atr){ } //saves the screen data in storage - public void saveFile(String folderName, String fileName){ + public void saveFile(String folderName, String fileName) { Bitmap bitmap = this.getDrawingCache(); String path = Environment.getExternalStorageDirectory().getAbsolutePath(); @@ -121,10 +171,10 @@ public void saveFile(String folderName, String fileName){ if (!f.exists()) { f.mkdirs(); } - File file = new File(path+"/" + folderName + "/" + fileName + ".jpeg"); + File file = new File(path + "/" + folderName + "/" + fileName + ".jpeg"); FileOutputStream ostream; try { - if(file.exists()){ + if (file.exists()) { file.delete(); } file.createNewFile(); @@ -141,23 +191,28 @@ public void saveFile(String folderName, String fileName){ } //returns the bitmap file of the screen - public Bitmap getFile(){ + public Bitmap getFile() { + Bitmap file = this.getDrawingCache(); + return file; + } + + public Bitmap getBitmap() { Bitmap file = this.getDrawingCache(); return file; } //UnDo the last change done - public void undo(){ - if(paths.size() >=1){ - backup.push(paths.get(paths.size()-1)); - paths.remove(paths.size()-1); + public void undo() { + if (paths.size() >= 1) { + backup.push(paths.get(paths.size() - 1)); + paths.remove(paths.size() - 1); invalidate(); } } //ReDo the last change done - public void redo(){ - if(!backup.empty()){ + public void redo() { + if (!backup.empty()) { paths.add(backup.peek()); backup.pop(); invalidate(); @@ -165,9 +220,9 @@ public void redo(){ } //Clears the screen - public void clear(){ + public void clear() { backup.clear(); - for(Pair p : paths){ + for (Pair p : paths) { backup.push(p); } paths.clear(); @@ -175,15 +230,15 @@ public void clear(){ } //getters start - public String getPaintColor(){ + public String getPaintColor() { return myPen.getPaint_color(); } - public float getStrokeWidth(){ + public float getStrokeWidth() { return myPen.getStroke_width(); } - public String getBGColor(){ + public String getBGColor() { return bg_color; } @@ -191,21 +246,69 @@ public String getBGColor(){ //setters start - public void setPaintColor(int paintColor){ + public void setPaintColor(int paintColor) { myPen.setPaint_color(paintColor); } - public void setStrokeWidth(float paintWidth){ + public void setStrokeWidth(float paintWidth) { myPen.setStrokeWidth(paintWidth); } - public void setBGColor(int bgColor){ + public void setBGColor(int bgColor) { try { this.setBackgroundColor(bgColor); bg_color = String.format("#%06X", (0xFFFFFF & bgColor)); - }catch (Exception e){ + } catch (Exception e) { Log.d("Paint", e.toString()); } } //setters end + + public class Point { + private float x; + private float y; + private boolean flag = true; + private float extraX; + private float extraY; + + public Point(float x, float y) { + this.x = x; + this.y = y; + } + + public Point() { + } + + public float getX() { + return x; + } + + public void setX(float x) { + this.x = x; + } + + public float getY() { + return y; + } + + public void setY(float y) { + this.y = y; + } + + public float getExtraX() { + return extraX; + } + + public void setExtraX(float extraX) { + this.extraX = extraX; + } + + public float getExtraY() { + return extraY; + } + + public void setExtraY(float extraY) { + this.extraY = extraY; + } + } }