screen reload based on condition - android -


package com.example.company;  public class multipleviewslayered extends graphicsactivity {     /** called when activity first created. */      public int mycounter;     public boolean onedone;     public boolean twodone;     public boolean threedone;     public boolean fourdone;     public boolean screencompleted;      public imageview iv3;       @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         //setcontentview(r.layout.multipleviewslayered);          // grabbing application context                  final context context = getapplication();                            // creating new linearlayout  add linear definition again.                relativelayout relativelayout = new relativelayout(this);                            // setting orientation vertical                  ////relativelayout.setorientation(linearlayout.vertical);                             // creating fish          final imageview iv = new imageview(this);                  iv.setimageresource(r.drawable.fish2);         // relative layout parameters         relativelayout.layoutparams lp = new relativelayout.layoutparams(                      relativelayout.layoutparams.fill_parent,                  relativelayout.layoutparams.fill_parent);                 //iv.setid(1);                                   relativelayout.addview(iv,lp);                  // creating transparent image numbers.         final imageview iv2 = new imageview(this);         iv2.setimageresource(r.drawable.ctdsquareone);         //iv2.setid(2);         relativelayout.layoutparams lp2 = new relativelayout.layoutparams(                      relativelayout.layoutparams.fill_parent,                  relativelayout.layoutparams.fill_parent);         relativelayout.addview(iv2,lp2);                              final customviewcanvas mycanvas = new customviewcanvas(this);         relativelayout.layoutparams lp3 = new relativelayout.layoutparams(                      relativelayout.layoutparams.fill_parent,                  relativelayout.layoutparams.fill_parent);         relativelayout.addview(mycanvas,lp3);          setcontentview(relativelayout);                     // app's shared preferences                 sharedpreferences app_preferences = preferencemanager.getdefaultsharedpreferences(this);         sharedpreferences.editor editor = app_preferences.edit();                mycounter = app_preferences.getint("mycounter", 0);              // increment counter                                editor.putint("mycounter", ++mycounter);                 editor.commit();           onedone = false;         twodone = false;         threedone = false;         fourdone = false;         screencompleted = false;          mpaint = new paint();         mpaint.setantialias(true);         mpaint.setdither(true);         mpaint.setcolor(0xffff0000);         mpaint.setstyle(paint.style.stroke);         mpaint.setstrokejoin(paint.join.round);         mpaint.setstrokecap(paint.cap.round);         mpaint.setstrokewidth(12);                 memboss = new embossmaskfilter(new float[] { 1, 1, 1 },                                        0.4f, 6, 3.5f);          mblur = new blurmaskfilter(8, blurmaskfilter.blur.normal);          }       private paint       mpaint;     private maskfilter  memboss;     private maskfilter  mblur;       public class customviewcanvas extends view {         paint paint = new paint();                private bitmap  mbitmap;         private paint   mbitmappaint;          private path mpath;         private canvas  mcanvas;           public customviewcanvas (context context){             super(context);             paint.setcolor(color.black);              //new bitmap empty             mbitmap = bitmap.createbitmap(480, 800, bitmap.config.argb_8888);              mcanvas = new canvas(mbitmap);                        mbitmappaint = new paint(paint.dither_flag);              //path             mpath = new path();                                                           }                  @override         protected void ondraw(canvas canvas) {             canvas.drawcolor(color.transparent);                                     canvas.drawbitmap(mbitmap, 0, 0, mbitmappaint);               //path             mcanvas.drawpath(mpath, mpaint);                      }          //          private float mx, my;             private static final float touch_tolerance = 4;              private void touch_start(float x, float y) {                 mpath.reset();                 mpath.moveto(x, y);                 mx = x;                 = y;             }             private void touch_move(float x, float y) {                 float dx = math.abs(x - mx);                 float dy = math.abs(y - my);                 if (dx >= touch_tolerance || dy >= touch_tolerance) {                     mpath.quadto(mx, my, (x + mx)/2, (y + my)/2);                     mx = x;                     = y;              //*************************************************************************              // here issue             // want change iv image here when screencompleted true.             // set true here when line completed.              //*************************************************************************                   }                           }              private void touch_up() {                 mpath.lineto(mx, my);                 // commit path our offscreen                             mcanvas.drawpath(mpath, mpaint);                                 // kill don't double draw                 mpath.reset();             }              @override             public boolean ontouchevent(motionevent event) {                 float x = event.getx();                 float y = event.gety();                  switch (event.getaction()) {                     case motionevent.action_down:                         touch_start(x, y);                         invalidate();                         break;                     case motionevent.action_move:                         touch_move(x, y);                         invalidate();                         break;                     case motionevent.action_up:                         touch_up();                         invalidate();                         break;                 }                 return true;             }                //     } } 

update:

after reading comment "changing image trying do. if can don't have reload view. condition boolean turning true false", guess need way reference widget other class. then, changing image using new setimageresource (rid); should enough refresh screen new image. here complete example of it. there's class upon clicking on button, checks whether checkbox checked or not. if is, change image:

listviewtest.java

package com.aleadam.test;  import android.app.activity; import android.os.bundle; import android.widget.imageview; import android.widget.linearlayout;  public class listviewtest extends activity {     @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         linearlayout ll = new linearlayout(this);         imageview img = new imageview(this);         linearlayout.layoutparams lp = new linearlayout.layoutparams(                 linearlayout.layoutparams.wrap_content, linearlayout.layoutparams.wrap_content);         img.setimageresource(r.drawable.image1);         mylayout myl = new mylayout (this);         myl.setimage(img);         ll.addview(img, lp);         ll.addview(myl, lp);         setcontentview (ll);     } } 

mylayout.java

package com.aleadam.test;  import android.content.context; import android.view.view; import android.widget.button; import android.widget.checkbox; import android.widget.imageview; import android.widget.linearlayout;  public class mylayout extends linearlayout {     private imageview img;     private button btn;     private checkbox chkbox;     public mylayout(context context) {         super (context);         this.setorientation(vertical);         btn = new button (context);         chkbox = new checkbox (context);         btn.settext("click");         btn.setonclicklistener(new mylistener());         chkbox.settext("select");         layoutparams lp = new layoutparams(layoutparams.wrap_content, layoutparams.wrap_content);         this.addview(btn, lp);         this.addview(chkbox,lp);     }     public void setimage (imageview view) {         this.img = view;     }     private class mylistener implements onclicklistener {         public void onclick (view v) {             if (chkbox.ischecked()) {                 img.setimageresource(r.drawable.image2);             }         }     } } 

if class2 object visible on screen, try this.getrootview().invalidate();

from view reference page:

drawing

drawing handled walking tree , rendering each view intersects the invalid region. because tree traversed in-order, means parents draw before (i.e., behind) children, siblings drawn in order appear in tree. if set background drawable view, view draw before calling ondraw() method.

note framework not draw views not in invalid region.

to force view draw, call invalidate().

http://developer.android.com/reference/android/view/view.html


Comments