java - onDraw does not work on ImageView Android -


i need draw point on imageview android.

i use custom subclass of view class , override ondraw method like:

public class drawpoints extends view {      private float x;     private float y;     private paint mpaint;      public drawpoints(context context, float x, float y) {         super(context);         this.x = x;         this.y = y;     }      @override     protected void ondraw(canvas canvas) {         super.ondraw(canvas);          log.i("tag", "ondraw: " + x + "" + y);         mpaint = new paint();         mpaint.setcolor(color.yellow);         canvas.drawcircle(x,y,200,mpaint);      } } 

in mainactivity use ontouch event draw, not forget invalidate(), not draw point.

public class mainactivity extends appcompatactivity {      private imageview mimageview;     private canvas mcanvas;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);          mcanvas = new canvas();         setcontentview(r.layout.activity_main);         mimageview = (imageview) findviewbyid(r.id.image_main);         mimageview.setontouchlistener(new view.ontouchlistener() {              @override             public boolean ontouch(view view, motionevent motionevent) {                  float y = motionevent.gety();                 float x = motionevent.getx();                 new drawpoints(mainactivity.this,x,y).draw(mcanvas);                 mimageview.invalidate();                 return false;             }         });     } } 

what be? have read many examples ondraw on stackoverflow none help.

you create imageview draw circle on touch events. example:

import android.content.context; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.graphics.point; import android.util.attributeset; import android.util.log; import android.view.motionevent;  import java.util.arraylist;  public class drawpoints extends android.support.v7.widget.appcompatimageview {      private static final string tag = drawpoints.class.getsimplename();      private arraylist<point> msavedpoints = new arraylist<>();     private point mlasttouchpoint;     private paint mpaint;      public drawpoints(context context) {         super(context);         init();     }      public drawpoints(context context, attributeset attrs) {         super(context, attrs);         init();     }      public drawpoints(context context, attributeset attrs, int defstyleattr) {         super(context, attrs, defstyleattr);         init();     }      private void init() {         mpaint = new paint();         mpaint.setcolor(color.yellow);     }      @override public boolean dispatchtouchevent(motionevent event) {         mlasttouchpoint = new point((int) event.getx(), (int) event.gety());         postinvalidate();         return super.dispatchtouchevent(event);     }      @override protected void ondraw(canvas canvas) {         super.ondraw(canvas);         if (mlasttouchpoint != null) {             if (mlasttouchpoint.x > 0 || mlasttouchpoint.y > 0) {                 msavedpoints.add(mlasttouchpoint);                 log.i(tag, mlasttouchpoint.tostring());             }         }          (point point : msavedpoints) {             canvas.drawcircle(point.x, point.y, 200, mpaint);         }          log.i(tag, msavedpoints.tostring());     } } 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -