在画布上绘制ANDROID

时间:2013-10-14 12:11:59

标签: java android android-canvas surfaceview

我有以下情况:

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

PortTemplate:

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

MainActivity

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

}

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

提前谢谢!

1 个答案:

答案 0 :(得分:0)

有几件事......

  1. 您正在surfaceCreated进行绘图,只有在首次创建曲面时才会调用,因此不会再对此方法进行任何调用。
  2. 您应该在自己的案例中创建自定义View。只需展开View并在onDraw中绘制图纸即可。您还可以访问invalidate()这样的方法,这些方法会重新绘制您的View