如何重绘SurfaceView

时间:2017-07-17 05:13:42

标签: android canvas surfaceview repaint surfaceholder

每当我从蓝牙接收新数据时,我需要在我的SurfaceView上通过画布绘制线条,第一次我可以成功绘制但是从第二次开始,surfacecreated()方法永远不会被调用,请帮我如何重绘surfaceview.I添加我的布局和代码供进一步参考,

<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>

从MianActivity我调用我的SurfaceView类,

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

我粘贴了整个SurfaceView类,请帮助我在哪里做错了,以及每当我收到新数据时如何重新绘制我的surfaceview

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");
}
}

我粘贴了我的logcat以供进一步参考,从logcat我们可以来konw surfacecreated()第二次收到新数据时不会被调用

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

我添加了我的sessionmanager类以供参考,

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();
}
}

0 个答案:

没有答案