Android - Update custom view from asynctask -
i have custom view class
public class drawingview extends view { //drawing path private path drawpath; //drawing , canvas paint private paint drawpaint, canvaspaint; //initial color private int paintcolor = 0x00660000; ... public void printpoint(point p, boolean b){ if(p != null){ paint testepaint = new paint(); testepaint.setcolor(0xff0000ff); testepaint.setantialias(true); testepaint.setstrokewidth(brushsize); testepaint.setstyle(paint.style.stroke); testepaint.setstrokejoin(paint.join.round); testepaint.setstrokecap(paint.cap.round); if(b){ testepaint.setcolor(0xffff0000); }else{ testepaint.setcolor(0xff0000ff); } drawcanvas.drawpoint(p.x, p.y, testepaint); } } public void printpath(){ if(paths.size() > 0){ path testepath = new path(); paint testepaint = new paint(); testepaint.setcolor(0xff00ff00); testepaint.setantialias(true); testepaint.setstrokewidth(brushsize); testepaint.setstyle(paint.style.stroke); testepaint.setstrokejoin(paint.join.round); testepaint.setstrokecap(paint.cap.round); testepath = paths.remove(0); drawcanvas.drawpath(testepath, testepaint); } } }
add view layout , load mainactivity using
public class mainactivity extends activity implements onclicklistener { //tcp com private tcpclient mtcpclient; // private drawingview drawview; private imagebutton currpaint, drawbtn, erasebtn, newbtn; private float smallbrush, mediumbrush, largebrush; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); linearlayout paintlayout = (linearlayout)findviewbyid(r.id.paint_colors); currpaint = (imagebutton)paintlayout.getchildat(0); currpaint.setimagedrawable(getresources().getdrawable(r.drawable.paint_pressed)); drawview = (drawingview)findviewbyid(r.id.drawing); drawview.setbrushsize(mediumbrush); .... }
when click button call following function
public void updateview(linha l){ log.d("tcp","updateview()"); if(l.getescreve() == true){ drawview.printpath(); } drawview.printpoint(l.getponto(),l.getescreve()); }
and works when call onprogressupdate
asynctask can see draws on view if click again on button.
hmm don't think should updating ui on different thread. instead override ondraw in custom view, , asynctask postinvalidate trigger ondraw being called on ui thread.
Comments
Post a Comment