android - SurfaceView blinking when i try to hide it -


i use surfaceview draw coordinate grid , outlines future custom views. when done drawing outlines, hide surfaceview (mysurfaceview.setvisibility(view.gone)) , screen splashes black on moment. how resolve problem? sample project

mysurfaceview

    public class mysurfaceview extends surfaceview implements surfaceholder.callback {     float[] gridlines;     canvas canvas;     pointf oldpos = new pointf();     surfaceholder holder;     final int mstep = 15;     private paint paint = new paint(paint.anti_alias_flag);      public mysurfaceview(context context, attributeset attrs) {         super(context, attrs);         getholder().addcallback(this);         paint.setstyle(paint.style.stroke);         paint.setstrokewidth(3);         paint.setcolor(context.getresources().getcolor(r.color.coloraccent));     }      @override     public boolean ontouchevent(motionevent event) {         pointf newpos = new pointf();         float top;         float left;         float bottom;         float right;         switch (event.getaction()) {             case motionevent.action_down:                 oldpos.set(event.getx(), event.gety());                 break;             case motionevent.action_move:                 canvas = holder.lockcanvas();                 drawgrid(canvas);                 newpos.set(event.getx(), event.gety());                 top = math.min(oldpos.y, newpos.y);                 left = math.min(oldpos.x, newpos.x);                 bottom = math.max(oldpos.y, newpos.y);                 right = math.max(oldpos.x, newpos.x);                 int l = (int) left / mstep;                 l *= mstep;                 int r = (int) right / mstep;                 r *= mstep;                 int t = (int) top / mstep;                 t *= mstep;                 int b = (int) bottom / mstep;                 b *= mstep;                 paint.setstrokewidth(6);                 canvas.drawrect(l, t, r, b, paint);                 holder.unlockcanvasandpost(canvas);                 break;             case motionevent.action_up:                  break;         }         return true;     }      private void drawgrid(canvas canvas) {         final paint paint = new paint();         canvas.drawcolor(color.white);         paint.setstrokewidth(2);         paint.setstyle(paint.style.stroke);         paint.setcolor(color.gray);         canvas.drawlines(gridlines, paint);     }      private float[] getlines(int step) {         final list<float> floatlist = new arraylist<>();         int right = this.getright();         int bottom = this.getbottom();         (int x = 0; x < right; x += step) {                 floatlist.add((float) x);                 floatlist.add(0f);                 floatlist.add((float) x);                 floatlist.add((float) bottom);          }         (int y = 0; y < bottom; y += step) {                 floatlist.add(0f);                 floatlist.add((float) y);                 floatlist.add((float) right);                 floatlist.add((float) y);         }          float[] rez = new float[floatlist.size()];         (int k = 0; k < floatlist.size(); k++) {             rez[k] = floatlist.get(k);         }         return rez;     }       @override     public void surfacecreated(surfaceholder surfaceholder) {         holder = surfaceholder;         gridlines = getlines(mstep);         canvas = surfaceholder.lockcanvas();         drawgrid(canvas);         surfaceholder.unlockcanvasandpost(canvas);     }      @override     public void surfacechanged(surfaceholder surfaceholder, int i, int i1, int i2) {         canvas = surfaceholder.lockcanvas();         drawgrid(canvas);         surfaceholder.unlockcanvasandpost(canvas);     }      @override     public void surfacedestroyed(surfaceholder surfaceholder) {      } } 

mainfragment

    public class mainfragment extends fragment {     mysurfaceview mysurfaceview;       public mainfragment() {         // required empty public constructor     }      @override     public void oncreate(@nullable bundle savedinstancestate) {         super.oncreate(savedinstancestate);         sethasoptionsmenu(true);     }      @override     public view oncreateview(layoutinflater inflater, viewgroup container,                              bundle savedinstancestate) {         // inflate layout fragment         return inflater.inflate(r.layout.fragment_main, container, false);     }      @override     public void onviewcreated(view view, @nullable bundle savedinstancestate) {         super.onviewcreated(view, savedinstancestate);         mysurfaceview = (mysurfaceview) view.findviewbyid(r.id.surface_view);         mysurfaceview.setvisibility(view.gone);     }      @override     public void oncreateoptionsmenu(menu menu, menuinflater inflater) {         super.oncreateoptionsmenu(menu, inflater);         inflater.inflate(r.menu.main_fragment_menu, menu);     }      @override     public boolean onoptionsitemselected(menuitem item) {         switch (item.getitemid()) {             case r.id.vis:                 if (mysurfaceview.getvisibility()==view.visible) {                     item.seticon(getresources().getdrawable(r.drawable.ic_action_show));                     mysurfaceview.setvisibility(view.gone);                 } else {                     item.seticon(getresources().getdrawable(r.drawable.ic_action_hide));                     mysurfaceview.setvisibility(view.visible);                 }                 break;         }         return super.onoptionsitemselected(item);     } } 

mainactivity

public class mainactivity extends appcompatactivity {     @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);     }      @override     protected void onstart() {         super.onstart();         fragmentmanager fm = getsupportfragmentmanager();         mainfragment fragment = new mainfragment();         fragmenttransaction ft = fm.begintransaction();         ft.replace(r.id.container, fragment).commit();     } } 

fragment_main.xml

<framelayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     tools:context="com.gvozditskiy.surfaceblinking.mainfragment">      <com.gvozditskiy.surfaceblinking.mysurfaceview         android:id="@+id/surface_view"         android:layout_width="match_parent"         android:layout_height="match_parent" />  </framelayout> 


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -