android - onTouch and OnDraw in SurfaceView -


i trying draw based on trace on screen. in process ontouch , ondraw not being executed. please assist me correct code ?

xml

<linearlayout   xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"  android:layout_weight="100" android:orientation="vertical" android:background="#ffff0000" >      <linearlayout      android:layout_width="match_parent"     android:layout_height="wrap_content"     android:weightsum="60"     android:gravity="center"  >             <com.example.test2.pathdraw             android:id="@+id/surfaceview"             android:layout_width="match_parent"             android:layout_height="140dp" />     </linearlayout>      <linearlayout      android:layout_width="match_parent"     android:layout_height="wrap_content"             android:weightsum="40"     android:gravity="center"  >               <imageview             android:layout_width="wrap_content"            android:layout_height="wrap_content"                android:background="@drawable/lion" />     </linearlayout> 

mainactivity

public class mainactivity extends activity {  @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      pathdraw sinewavesurfaceview = (pathdraw) findviewbyid(r.id.surfaceview); }  } 

pathdraw

public class pathdraw extends surfaceview implements surfaceholder.callback  { private context context;  //  list<point> points;   arraylist<point> points = new arraylist<point>();  public pathdraw(context context)    {     super(context);     // todo auto-generated constructor stub   }  public pathdraw(context context, attributeset attrs)    {     super(context, attrs);     // todo auto-generated constructor stub      this.context = context;     getholder().addcallback(this);  }  public pathdraw(context context, attributeset attrs, int defstyle)  {     super(context, attrs, defstyle);     // todo auto-generated constructor stub  }  protected void ondraw(canvas canvas, int value) {       paint paint = new paint();     paint = new paint(paint.anti_alias_flag);     paint.setstyle(paint.style.stroke);     paint.setstrokewidth(2);     paint.setcolor(color.white);      path path = new path();     boolean first = true;     for(point point : points){         if(first){             first = false;             path.moveto(point.x, point.y);         }         else{             path.lineto(point.x, point.y);         }     }      canvas.drawpath(path, paint);   }  public boolean ontouch(view view, motionevent event) {     // if(event.getaction() != motionevent.action_down)     // return super.ontouchevent(event);     point point = new point();     point.x = event.getx();     point.y = event.gety();     points.add(point);     invalidate();     log.d("pathdraw", "point: " + point);     return true; }   @override public void surfacechanged(surfaceholder arg0, int arg1, int arg2, int arg3)    {     // todo auto-generated method stub  }  @override public void surfacecreated(surfaceholder arg0)  {     // todo auto-generated method stub   }  @override public void surfacedestroyed(surfaceholder arg0)    {     // todo auto-generated method stub  }  } 

ontouch not called

i had exact same problem earlier today.

class pathdraw should implement ontouchlistener, set listener itself, this:

public class pathdraw extends surfaceview implements ontouchlistener, surfaceholder.callback{     ...         public pathdraw(context context){         super(context);         setontouchlistener(pathdraw.this);       } 

ondraw not called

this post might help:

android custom layout - ondraw() never gets called


Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

jquery - How would i go about shortening this code? And to cancel the previous click on click of new section? -