Advertisement
Guest User

Untitled

a guest
Jul 31st, 2015
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 KB | None | 0 0
  1. public class MainActivity_ extends Activity {
  2. ImageView im;
  3. public static List<Point> points;
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_main);
  8. im=(ImageView) findViewById(R.id.imageView1);
  9. Context c = im.getContext();
  10. DrawView pic=new DrawView(c);
  11.  
  12. }
  13.  
  14.  
  15.  
  16. public class DrawView extends ImageView implements View.OnTouchListener{
  17.  
  18. Bitmap resultingImage;
  19. private Paint paint;
  20.  
  21. int DIST = 2;
  22. boolean flgPathDraw = true;
  23.  
  24. Point mfirstpoint = null;
  25. boolean bfirstpoint = false;
  26.  
  27. Point mlastpoint = null;
  28.  
  29. Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
  30. R.drawable.gallery_12);// convert image to bitmap
  31. Context mContext;
  32.  
  33.  
  34. public Bitmap get(){
  35. return resultingImage;
  36. }
  37.  
  38. public DrawView(Context c) {
  39. super(c);
  40.  
  41. mContext = c;
  42. setFocusable(true);
  43. setFocusableInTouchMode(true);
  44.  
  45. paint = new Paint(Paint.ANTI_ALIAS_FLAG);
  46. paint.setStyle(Paint.Style.STROKE);
  47. paint.setPathEffect(new DashPathEffect(new float[] { 10, 20 }, 0));
  48. paint.setStrokeWidth(5);
  49. paint.setColor(Color.WHITE);
  50.  
  51. this.setOnTouchListener(this);
  52. points = new ArrayList<Point>();
  53.  
  54. bfirstpoint = false;
  55. }
  56.  
  57. public DrawView(Context context, AttributeSet attrs) {
  58. super(context, attrs);
  59. mContext = context;
  60. setFocusable(true);
  61. setFocusableInTouchMode(true);
  62.  
  63. paint = new Paint(Paint.ANTI_ALIAS_FLAG);
  64. paint.setStyle(Paint.Style.STROKE);
  65. paint.setStrokeWidth(2);
  66. paint.setColor(Color.WHITE);
  67.  
  68. this.setOnTouchListener(this);
  69. points = new ArrayList<Point>();
  70. bfirstpoint = false;
  71.  
  72. }
  73.  
  74. public void onDraw(Canvas canvas) {
  75. canvas.drawBitmap(bitmap, 0, 0, null);
  76.  
  77. Path path = new Path();
  78. boolean first = true;
  79.  
  80. for (int i = 0; i < points.size(); i += 2) {
  81. Point point = points.get(i);
  82. if (first) {
  83. first = false;
  84. path.moveTo(point.x, point.y);
  85. } else if (i < points.size() - 1) {
  86. Point next = points.get(i + 1);
  87. path.quadTo(point.x, point.y, next.x, next.y);
  88. } else {
  89. mlastpoint = points.get(i);
  90. path.lineTo(point.x, point.y);
  91. }
  92. }
  93. canvas.drawPath(path, paint);
  94. }
  95.  
  96. public boolean onTouch(View view, MotionEvent event) {
  97. /// if(event.getAction() != MotionEvent.ACTION_DOWN)
  98. // return super.onTouchEvent(event);
  99.  
  100. Point point = new Point();
  101. point.x = (int) event.getX();
  102. point.y = (int) event.getY();
  103.  
  104. if (flgPathDraw) {
  105.  
  106. if (bfirstpoint) {
  107.  
  108. if (comparepoint(mfirstpoint, point)) {
  109. // points.add(point);
  110. points.add(mfirstpoint);
  111. flgPathDraw = false;
  112. showcropdialog();
  113. } else {
  114. points.add(point);
  115. }
  116. } else {
  117. points.add(point);
  118. }
  119.  
  120. if (!(bfirstpoint)) {
  121.  
  122. mfirstpoint = point;
  123. bfirstpoint = true;
  124. }
  125. }
  126.  
  127. invalidate();
  128. Log.e("Hi ==>", "Size: " + point.x + " " + point.y);
  129.  
  130. if (event.getAction() == MotionEvent.ACTION_UP) {
  131. Log.d("Action up*******~~~~~~~>>>>", "called");
  132. mlastpoint = point;
  133. if (flgPathDraw) {
  134. if (points.size() > 12) {
  135. if (!comparepoint(mfirstpoint, mlastpoint)) {
  136. flgPathDraw = false;
  137. points.add(mfirstpoint);
  138. showcropdialog();
  139. }
  140. }
  141. }
  142. }
  143.  
  144. return true;
  145. }
  146.  
  147. private boolean comparepoint(Point first, Point current) {
  148. int left_range_x = (int) (current.x - 3);
  149. int left_range_y = (int) (current.y - 3);
  150.  
  151. int right_range_x = (int) (current.x + 3);
  152. int right_range_y = (int) (current.y + 3);
  153.  
  154. if ((left_range_x < first.x && first.x < right_range_x)
  155. && (left_range_y < first.y && first.y < right_range_y)) {
  156. if (points.size() < 10) {
  157. return false;
  158. } else {
  159. return true;
  160. }
  161. } else {
  162. return false;
  163. }
  164.  
  165. }
  166.  
  167. public void fillinPartofPath() {
  168. Point point = new Point();
  169. point.x = points.get(0).x;
  170. point.y = points.get(0).y;
  171.  
  172. points.add(point);
  173. invalidate();
  174. }
  175.  
  176. public void resetView() {
  177. points.clear();
  178. paint.setColor(Color.WHITE);
  179. paint.setStyle(Style.STROKE);
  180. flgPathDraw = true;
  181. invalidate();
  182. }
  183.  
  184. private void showcropdialog() {
  185. Toast.makeText(getApplicationContext(),"Rnning show crop dialog", Toast.LENGTH_LONG);
  186.  
  187. }
  188. }
  189. @Override
  190. protected void onResume() {
  191. super.onResume();
  192.  
  193. }
  194.  
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement