0
我有以下場景:在畫布上繪圖ANDROID
我有一個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) {
}
});
}
問題是: t當他們選擇狀態時,畫布不繪製端口(刷新方法)。任何想法如何讓畫布畫出現有端口上的端口?
預先感謝您!