0

Whenever i receive new data from bluetooth,i need to draw lines on my SurfaceView through canvas, First time i can draw successfully but from second time surfacecreated() method is never called, please help me how to repaint surfaceview.I added my layout and code for further reference,

<LinearLayout
        android:id="@+id/phasorPane"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="10dp"
        android:orientation="vertical"
        android:visibility="gone">

        <com.rd.sands.bluetooth.MySurfaceView
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

    </LinearLayout>

From MianActivity i call my SurfaceView class like,

myView = new MySurfaceView(getApplicationContext());
                    myView.invalidate();

I pasted my entire SurfaceView class please help me where i am doing wrong and how to repaint my surfaceview whenever i receive new data

public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback {

private SurfaceHolder surfaceHolder;
Paint paint;
private int length;
Canvas canvas;
UserSessionManager sessionManager;
ArrayList resList = new ArrayList();

public MySurfaceView(Context context) {
    super(context);
    System.out.println("---------------MySurfaceView called");
    init(length);
}

public MySurfaceView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(length);
}

public MySurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(length);
}


private void init(final int theta){

    System.out.println("---------------init called");

    paint = new Paint();
    paint.setColor(Color.BLACK);
    paint.setStrokeWidth(6);
    paint.setStyle(Paint.Style.STROKE);

    surfaceHolder = getHolder();
    surfaceHolder.addCallback(this);
    // deprecated setting, but required on Android versions prior to 3.0
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    System.out.println("---------------callback called");

}
protected void drawSomething(Canvas canvas) {

    canvas.drawColor(Color.WHITE);
    //surfaceHolder.setFormat(PixelFormat.TRANSPARENT);
    //surfaceHolder.setFormat(PixelFormat.OPAQUE);
    //canvas.drawColor( 0, PorterDuff.Mode.CLEAR );

    sessionManager = new UserSessionManager(getContext());

    ArrayList<String> retrievedValues = new ArrayList<>();
    retrievedValues = sessionManager.retriveArrayList();
    System.out.println("------Pref values(SurfaceView): " + retrievedValues + ", " + retrievedValues.size());

    for (int i = 0; i < retrievedValues.size(); i++) {
        System.out.println("--------------retrievedValues--> " + retrievedValues.get(i));
        //findParameters(canvas.getWidth()/2,canvas.getHeight()/2,retrievedValues.get(i));


        //System.out.println("findParameters("+x1+","+y1+","+arr+")");
        int x1 = canvas.getWidth() / 2;
        int y1 = canvas.getHeight() / 2;
        System.out.println("findParameters(" + canvas.getWidth() / 2 + "," + canvas.getHeight() / 2 + "," + retrievedValues.get(i) + ")");

        String replace = retrievedValues.get(i).replace("[", "");
        System.out.println("replace1 " + replace);
        String replace1 = replace.replace("]", "");
        System.out.println("replace2 " + replace1);

        if (replace1.contains(",")) {
            String[] res = replace1.split(",");

            String colorCode = res[0].trim();
            String len = res[1].trim();
            String angle = res[2].trim();
            System.out.println("Splitted String,colorCode: " + colorCode + ",len: " + len + ",angle: " + angle);

            //drawPhasorDiagram(x1,y1,len,Integer.parseInt(angle),colorCode);

            if (len.equals("l/2")) {
                length = canvas.getWidth() / 2;
                System.out.println("length " + length);
            } else if (len.equals("l/4")) {
                length = canvas.getWidth() / 4;
                System.out.println("length " + length);
            }

            System.out.println("drawPhasorDiagram" + "(x0,y0)->(" + x1 + "," + y1 + "),len: " + length + ",angle: " + angle + ",colorCode: " + colorCode + ")");

            double x2 = x1 + (length * Math.cos(Math.toRadians(Integer.parseInt(angle) - 90)));
            double y2 = y1 + (length * Math.sin(Math.toRadians(Integer.parseInt(angle) - 90)));
            System.out.println("(x2,y2) " + "(" + x2 + "," + y2 + ")");

            switch(colorCode){
                case "VR":
                    paint.setColor(Color.RED);
                    paint.setStrokeWidth(6);
                    break;
                case "VY":
                    paint.setColor(Color.YELLOW);
                    paint.setStrokeWidth(6);
                    break;
                case "VB":
                    paint.setColor(Color.BLUE);
                    paint.setStrokeWidth(6);
                    break;
                case "IR":
                    paint.setColor(Color.RED);
                    paint.setStrokeWidth(3);
                    break;
                case "IY":
                    paint.setColor(Color.YELLOW);
                    paint.setStrokeWidth(3);
                    break;
                case "IB":
                    paint.setColor(Color.BLUE);
                    paint.setStrokeWidth(3);
                    break;
            }

            paint.setStyle(Paint.Style.STROKE);
            System.out.println("drawLine(" + canvas.getWidth() / 2 + "," + canvas.getHeight() / 2 + "," + (float) x2 + "," + (float) y2 + ",p)");
            canvas.drawLine(canvas.getWidth() / 2, canvas.getHeight() / 2, (float) x2, (float) y2, paint);
            //canvas.drawLine(340,407, (float) 340.0, (float) 67.0,paint);

            System.out.println("drewLine");

            sessionManager.clearSession();
            invalidate();
        }

    }
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    System.out.println("onDraw()");
}

@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
    System.out.println("---------------surfaceCreated called");
    setWillNotDraw(false); //Allows us to use invalidate() to call onDraw()

    canvas = surfaceHolder.lockCanvas(null);
    //canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
    drawSomething(canvas);
    surfaceHolder.unlockCanvasAndPost(canvas);
}

@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
    System.out.println("---------------surfaceChanged called");
    postInvalidate();
}

@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
    System.out.println("---------------surfaceDestroyed called");
}
}

I pasted my logcat also for further reference, from logcat we can come to konw surfacecreated() is not called second time when new data received

07-17 10:22:20.285 17169-17169/com.rd.sands.bluetooth I/System.out: --------------Received Data(writeMessage)--  $L,VB,l/2,270
07-17 10:22:20.285 17169-17169/com.rd.sands.bluetooth I/System.out: --------------ArrayList--  [$L, VB, l/2, 270]
07-17 10:22:20.285 17169-17169/com.rd.sands.bluetooth I/System.out: --------------Received Data is for Phasor Diagram--  
07-17 10:22:20.290 17169-17169/com.rd.sands.bluetooth I/System.out: --------------Line ArrayList after removing spl chars--  [VB, l/2, 270]
07-17 10:22:20.290 17169-17169/com.rd.sands.bluetooth I/System.out: --------------LinkedList--> [VB, l/2, 270], 1
07-17 10:22:20.290 17169-17169/com.rd.sands.bluetooth D/Stored sharedPref: [[VB, l/2, 270]]
07-17 10:22:20.295 17169-17169/com.rd.sands.bluetooth I/System.out: ---------------MySurfaceView called
07-17 10:22:20.295 17169-17169/com.rd.sands.bluetooth I/System.out: ---------------init called
07-17 10:22:20.295 17169-17169/com.rd.sands.bluetooth I/System.out: ---------------callback called
07-17 10:22:20.295 17169-17169/com.rd.sands.bluetooth I/System.out: ---------------myview.invalidate()
07-17 10:22:20.320 17169-17169/com.rd.sands.bluetooth I/System.out: ---------------surfaceCreated called
07-17 10:22:20.325 17169-17169/com.rd.sands.bluetooth D/Retrieve sharedPref: [[VB, l/2, 270]]
07-17 10:22:20.330 17169-17169/com.rd.sands.bluetooth I/System.out: ------Pref values(SurfaceView): [[VB, l/2, 270]], 1
07-17 10:22:20.330 17169-17169/com.rd.sands.bluetooth I/System.out: --------------retrievedValues--> [VB, l/2, 270]
07-17 10:22:20.330 17169-17169/com.rd.sands.bluetooth I/System.out: findParameters(340,407,[VB, l/2, 270])
07-17 10:22:20.330 17169-17169/com.rd.sands.bluetooth I/System.out: replace1 VB, l/2, 270]
07-17 10:22:20.330 17169-17169/com.rd.sands.bluetooth I/System.out: replace2 VB, l/2, 270
07-17 10:22:20.330 17169-17169/com.rd.sands.bluetooth I/System.out: Splitted String,colorCode: VB,len: l/2,angle: 270
07-17 10:22:20.330 17169-17169/com.rd.sands.bluetooth I/System.out: length 340
07-17 10:22:20.330 17169-17169/com.rd.sands.bluetooth I/System.out: drawPhasorDiagram(x0,y0)->(340,407),len: 340,angle: 270,colorCode: VB)
07-17 10:22:20.335 17169-17169/com.rd.sands.bluetooth I/System.out: (x2,y2) (0.0,407.00000000000006)
07-17 10:22:20.335 17169-17169/com.rd.sands.bluetooth I/System.out: drawLine(340,407,0.0,407.0,p)
07-17 10:22:20.335 17169-17169/com.rd.sands.bluetooth I/System.out: drewLine
07-17 10:22:20.335 17169-17169/com.rd.sands.bluetooth I/System.out: UserSessionManager->clearSession() 
07-17 10:22:20.340 17169-17169/com.rd.sands.bluetooth I/System.out: ---------------surfaceChanged called
07-17 10:22:20.345 17169-17169/com.rd.sands.bluetooth I/System.out: onDraw()
07-17 10:22:20.365 17169-17169/com.rd.sands.bluetooth I/System.out: onDraw()
07-17 10:22:26.820 17169-17637/com.rd.sands.bluetooth I/System.out: -------------Series Removed
07-17 10:22:26.820 17169-17637/com.rd.sands.bluetooth I/System.out: -------------Bar Series Removed
07-17 10:22:26.820 17169-17637/com.rd.sands.bluetooth I/System.out: -------------New Series
07-17 10:22:26.820 17169-17169/com.rd.sands.bluetooth I/System.out: --------------Received Data(writeMessage)--  $L,VB,l/2,270
07-17 10:22:26.820 17169-17169/com.rd.sands.bluetooth I/System.out: --------------ArrayList--  [$L, VB, l/2, 270]
07-17 10:22:26.820 17169-17169/com.rd.sands.bluetooth I/System.out: --------------Received Data is for Phasor Diagram--  
07-17 10:22:26.820 17169-17169/com.rd.sands.bluetooth I/System.out: --------------Line ArrayList after removing spl chars--  [VB, l/2, 270]
07-17 10:22:26.820 17169-17169/com.rd.sands.bluetooth I/System.out: --------------LinkedList--> [VB, l/2, 270], 1
07-17 10:22:26.820 17169-17169/com.rd.sands.bluetooth D/Stored sharedPref: [[VB, l/2, 270]]
07-17 10:22:26.820 17169-17169/com.rd.sands.bluetooth I/System.out: ---------------MySurfaceView called
07-17 10:22:26.820 17169-17169/com.rd.sands.bluetooth I/System.out: ---------------init called
07-17 10:22:26.820 17169-17169/com.rd.sands.bluetooth I/System.out: ---------------callback called
07-17 10:22:26.820 17169-17169/com.rd.sands.bluetooth I/System.out: ---------------myview.invalidate()
07-17 10:27:18.840 17169-17169/com.rd.sands.bluetooth V/ActivityThread: updateVisibility : ActivityRecord{6d487cf token=android.os.BinderProxy@a0d65ef {com.rd.sands.bluetooth/com.rd.sands.bluetooth.MainActivity}} show : true

I added my sessionmanager class also for reference,

public class UserSessionManager {
SharedPreferences pref;
SharedPreferences.Editor editor;
Context _context;
int PRIVATE_MODE = 0;
private static final String PREFER_NAME = "MyPref";
ArrayList<String> arrPackage = new ArrayList<>();

public UserSessionManager(Context context){
    this._context = context;
    pref = _context.getSharedPreferences(PREFER_NAME, PRIVATE_MODE);
    editor = pref.edit();
}


public void saveArrayList(ArrayList<String> arrayList) {
    Set<String> set = new HashSet<String>();
    set.addAll(arrayList);
    editor.putStringSet("DATE_LIST", set);
    editor.apply();
    Log.d("Stored sharedPref",""+set);
}

public ArrayList<String> retriveArrayList() {

    Set<String> set = pref.getStringSet("DATE_LIST", null);
    arrPackage.addAll(set);
    Log.d("Retrieve sharedPref",""+set);
    return arrPackage;
}

public void clearSession(){
    System.out.println("UserSessionManager->clearSession() ");
    editor.clear();
    editor.commit();
}
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Annie
  • 160
  • 1
  • 2
  • 15
  • 1
    Call `drawSomething()` in the `onDraw()` method. – Phantômaxx Jul 17 '17 at 05:27
  • Is it wrong that i called drawSomething() inside surfaceCreated() – Annie Jul 17 '17 at 05:49
  • when i cal drawSomething() inside onDraw() i am getting java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object[] java.util.Collection.toArray()' on a null object reference in this line,retrievedValues = sessionManager.retriveArrayList(); – Annie Jul 17 '17 at 05:51
  • 1
    No, but `surfaceCreated()` only fires once, while `onDraw()` runs continously. `NPE`: This means that some object is not yet initialized before you use it. – Phantômaxx Jul 17 '17 at 05:53
  • When i removed the line of code, sessionManager.clearSession(); inside drawSomething(), now onDraw() is repeatedly gets called without stopping, I added my UsersessionManager class also please check it for further reference – Annie Jul 17 '17 at 05:57
  • do you really need `SurfaceView` for that? cannot you just use a custom `View`? – pskink Jul 17 '17 at 06:53
  • @pskink i googled difference between view and surfaceview but still i didnt get a clear picture of it and not sure what should be used in my case for better performance – Annie Jul 17 '17 at 11:08
  • how often your data changes? – pskink Jul 17 '17 at 11:11
  • @pskink often i get new data through bluetooth and i need to draw phasor diagram with that data, every time i get new data i need to update the diagram – Annie Jul 17 '17 at 11:24
  • 1
    start with this: https://pastebin.com/FSKD9arF i made it yesterday for another `SurfaceView` related question and it actually shows some animation but you will get idea how it works - in your case you should simply call `handler.sendMessage` instead of `handler.sendEmptyMessageDelayed` – pskink Jul 17 '17 at 11:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/149434/discussion-between-annie-and-pskink). – Annie Jul 18 '17 at 04:25
  • @pskink do you know answer for my question which is posted in below link, "stackoverflow.com/questions/45322864/…; no one answered for this,if you know please help – Annie Aug 02 '17 at 10:06
  • never worked with com.jjoe64.graphview.GraphView, sorry... – pskink Aug 02 '17 at 10:11
  • @pskink in normal linearlayout do you know how to bring back to original state after zooming(zoomout) – Annie Aug 02 '17 at 10:12
  • setscale 1 settranslation 0 – pskink Aug 02 '17 at 10:37
  • @pskink thanks i will try this – Annie Aug 02 '17 at 10:43

0 Answers0