42848

Recreate an object without deleting the last one

Question:

I'm creating an app that allows user to draw on the screen. My code actually creates a Path and next creates a line. But this when the user takes up his finger of the screen and goes to another part of the screen it make a line as it happen in the next video. <a href="http://youtu.be/CBv1wtUC2g4" rel="nofollow">http://youtu.be/CBv1wtUC2g4</a>

The following code is the code of the drawview.

public class DrawView extends View implements OnTouchListener { private static final String TAG = "DrawView"; List<Point> points = new ArrayList<Point>(); Paint paint = new Paint(); int action; int draw=0; Boolean finger=true; public DrawView(Context context) { super(context); setFocusable(true); setFocusableInTouchMode(true); this.setOnTouchListener(this); paint.setColor(Color.BLACK); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(5); paint.setAntiAlias(true); } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub Path path = new Path(); boolean first = true; for(int i = 0; i < points.size(); i += 2){ Point point = points.get(i); if(first){ first = false; path.moveTo(point.x, point.y); } else if(i < points.size() - 1){ Point next = points.get(i + 1); path.quadTo(point.x, point.y, next.x, next.y); } else { path.lineTo(point.x, point.y); } } canvas.drawPath(path, paint); } public boolean onTouch(View view, MotionEvent event) { action = event.getAction(); Point point = new Point(); point.x = event.getX(); point.y = event.getY(); points.add(point); invalidate(); Log.d(TAG, "point: " + point); if (action == MotionEvent.ACTION_MOVE) { finger = true; } if (action == MotionEvent.ACTION_UP) { finger = false; } draw++; return true; } } class Point { float x, y; @Override public String toString() { return x + ", " + y; } }

Also I think that maybe what i want I need is an array that contains objects (paths and lines) but i don't know how to do it. Using this method i think that i'm will be able to go back deleting the last object can be this possible?

Answer1:

You need to create a different "Path" each time the user uplifts the finger. I've changed your code to make that. Try it to see if it's what you need.

import java.util.ArrayList; import java.util.List; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; public class DrawView extends View implements OnTouchListener { private static final String TAG = "DrawView"; private List<List<Point>> _paths = new ArrayList<List<Point>>(); private List<Point> _lastPath; private Paint _paint = new Paint(); private Path _path = new Path(); public DrawView(Context context) { super(context); setFocusable(true); setFocusableInTouchMode(true); setOnTouchListener(this); _paint.setColor(Color.BLACK); _paint.setStyle(Paint.Style.STROKE); _paint.setStrokeWidth(5); _paint.setAntiAlias(true); } @Override protected void onDraw(Canvas canvas) { for (List<Point> pointsPath : _paths) { _path.reset(); boolean first = true; for (int i = 0; i < pointsPath.size(); i += 2) { Point point = pointsPath.get(i); if (first) { first = false; _path.moveTo(point.x, point.y); } else if (i < pointsPath.size() - 1) { Point next = pointsPath.get(i + 1); _path.quadTo(point.x, point.y, next.x, next.y); } else { _path.lineTo(point.x, point.y); } } canvas.drawPath(_path, _paint); } } public boolean onTouch(View view, MotionEvent event) { Point point = new Point(); point.x = event.getX(); point.y = event.getY(); Log.d(TAG, "point: " + point); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: _lastPath = new ArrayList<Point>(); _lastPath.add(point); _paths.add(_lastPath); break; case MotionEvent.ACTION_MOVE: _lastPath.add(point); break; } invalidate(); return true; } private class Point { float x, y; @Override public String toString() { return x + ", " + y; } } }

Recommend

  • Circle movement upon rectangle Collision
  • Differences between drawing an Ellipse in Android and Java
  • Exception in the iconization of JInternalFrame with DefaultDesktopManager
  • write text on image and show it to a imageview
  • Ruby on Rails App deployed to heroku showing “We're sorry, but something went wrong”
  • Wrong row deleted from custom listview with spinner
  • Bootstrap (v3.3.4) glyphicons not displayed in IE when refresh page (F5)
  • Spring: No transaction manager has been configured
  • JPA flush vs commit
  • Elasticsearch script query involving root and nested values
  • Implementing “partial void” in VB
  • Why use database factory in asp.net mvc?
  • Sending HTML Form Data to Spring REST Web Service
  • How do I configure context broker accept post requests from my remote sensor?
  • How can I extract results of aggregate queries in slick?
  • OOP Javascript - Is “get property” method necessary?
  • Android screen density dpi vs ppi
  • PHP - How to update data to MySQL when click a radio button
  • How would I use PHP exceptions to define a redirect?
  • How to extract text from Word files using C#?
  • Javascript Callbacks with Object constructor
  • what is the difference between the asp.net mvc application and asp.net web application
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • need help with bizarre java.net.HttpURLConnection behavior
  • Turn off referential integrity in Derby? is it possible?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Bitwise OR returns boolean when one of operands is nil
  • Add sale price programmatically to product variations
  • sending mail using smtp is too slow
  • Busy indicator not showing up in wpf window [duplicate]
  • Sorting a 2D array using the second column C++
  • costura.fody for a dll that references another dll
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Observable and ngFor in Angular 2
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • java string with new operator and a literal
  • How do I use LINQ to get all the Items that have a particular SubItem?