0

我有以下情况:

我有一个带有 24 个端口的交换机。我想把它们画在画布上。我设法做到了,但问题是当我更改端口的状态时,我必须再次绘制它们,但它没有发生。这是我的课程:

端口模板:

public class PortTemplate{

private Rect port = new Rect();
private PointerCoords insertPoint=new PointerCoords();
private float xCoord;
private float yCoord;
private float gap;

...

//DRAWING PORT
public Rect drawPort(){
    port.set((int)insertPoint.x, (int)insertPoint.y, (int)insertPoint.x+portWidth, (int)insertPoint.y+portHeight);
    return port;
}
//**********
...
//setters and getters

主要活动

public class MainActivity extends Activity  implements OnClickListener{
Rect rec = new Rect();
int numberPorts=24;
PortTemplate[] recs=new PortTemplate[numberPorts];
PortTemplate[] newRecs=new PortTemplate[numberPorts];
Canvas canvas;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    port = (TextView) findViewById(R.id.port);

    mSelect = (Button) findViewById(R.id.select);
    mOptions = (Button) findViewById(R.id.option);

    mSelect.setOnClickListener(this);
    mOptions.setOnClickListener(this);

    surface= (SurfaceView) findViewById(R.id.surface);
    surface.setBackgroundColor(Color.TRANSPARENT);
    surface.getHolder().addCallback(new Callback() {

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            // Do some drawing when surface is ready

            PointerCoords insertRec=new PointerCoords();
            float x = insertRec.x=36;
            float y = insertRec.y=22;
            for(int i=0; i<numberPorts; i++)
            {
                recs[i]=new PortTemplate();
                recs[i].setInsertPoint(x,y);
                recs[i].setPortHeight(21);
                recs[i].setPortWidth(23);
                recs[i].setPortNumber(i+1);

                newRecs[i]=new PortTemplate();
                newRecs[i].setInsertPoint(x,y);
                newRecs[i].setPortHeight(21);
                newRecs[i].setPortWidth(23);
                newRecs[i].setPortNumber(i+1);

                x+=26.3;
            }

            Bitmap eqImage = BitmapFactory.decodeResource(getResources(), R.drawable.ports);
            Bitmap newImage = Bitmap.createScaledBitmap(eqImage, 700, 60, true);    
            canvas = holder.lockCanvas();
            canvas.drawBitmap(newImage, new Matrix(), null);
            EquipmentTemplate newEqup = new EquipmentTemplate();
            newEqup.setHeight(200);
            newEqup.setWidth(650);
            newEqup.setxCoord(0);
            newEqup.setyCoord(0);

            for(int i=0; i<numberPorts; i++)
            {
                canvas.drawRect(recs[i].drawPort(), recs[i].getPaint());
            }

            canvas.drawColor(Color.TRANSPARENT);
            holder.unlockCanvasAndPost(canvas);
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        }
    });


}
...

在 MainActivity 内部,我有一些方法可以让用户更改选定端口的状态并更改其状态:

private void selectPortDialog()
{
    String[] portsWithNumbers = new String[numberPorts];

    for(int i=0; i<numberPorts; i++)
    {
        portsWithNumbers[i]="Port: "+recs[i].getPortNumber();
    }


    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
    dialogBuilder.setTitle("Select Port");
    dialogBuilder.setSingleChoiceItems(portsWithNumbers, -1, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            selected = recs[which].getPortNumber();

        }
    });
    dialogBuilder.setPositiveButton("Select", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "Selected port: "+selected, Toast.LENGTH_LONG).show();
            port.setText("Port "+selected);
        }
    });
    AlertDialog alertDialog = dialogBuilder.create();
    alertDialog.show();

}



private void selectOptions()
{
    final String[] options = new String[3];

    if(recs[selected-1].isConnected()==false && recs[selected-1].getReserved()==false && recs[selected-1].getDefect()==false)
    {
        options[0]="Connect";
        options[1]="Reserve";
        options[2]="Defect";

        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
        dialogBuilder.setTitle("Action");
        dialogBuilder.setSingleChoiceItems(options, -1, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                if(which==0)
                {
                    newRecs[selected-1].setConnected(true);
                    refresh();
                }

            }
        });
        AlertDialog alertDialog = dialogBuilder.create();
        alertDialog.show();

    }
}

public void refresh()
{
    surface.getHolder().addCallback(new Callback() {

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            // Do some drawing when surface is ready

            canvas = holder.lockCanvas();

            for(int i=0; i<numberPorts; i++)
            {
                canvas.drawRect(newRecs[i].drawPort(), newRecs[i].getPaint());
            }

            canvas.drawColor(Color.TRANSPARENT);
            holder.unlockCanvasAndPost(canvas);
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        }
    });

}

问题是当他们选择他们在画布上的状态时没有绘制端口(刷新方法)。任何想法如何让画布在现有端口上绘制端口?

先感谢您!

4

1 回答 1

0

几件事...

  1. 您正在绘制surfaceCreated仅在首次创建表面时调用的绘图,因此不会对该方法进行任何后续调用。
  2. 你真的应该View在你的情况下创建一个自定义。只需扩展View并在onDraw. 您还可以访问方法,例如invalidate()重新绘制您的View.
于 2013-10-14T13:09:55.047 回答