0

我正在尝试单击一个按钮并添加一个电阻器。所以我需要的是在单击按钮时使视图无效。但是MyView方法内部的无效无效update()。我一直在尝试寻找这个问题,但我没有发现与我正在尝试做的事情相似的东西,或者这可能不是解决问题的方法。

DefaultActivity.java

public class DefaulActivity extends Activity {       

  MyView myView;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);         

    setContentView(R.layout.main);
    myView = new MyView(this, null); 

    final Button bAddResistor = (Button) findViewById(R.id.bAdd);
    bAddResistor.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on click              
            myView.update();                            
            Log.d("ButtonADD", "Button Add has been clicked");
        }
    });          
  } 
}

我的视图.java

public class MyView extends SurfaceView implements SurfaceHolder.Callback{      


  Resistor myResistor;
  private ArrayList<Resistor> mElements = new ArrayList<Resistor>();


  public MyView(Context context, AttributeSet attrs) {
    super(context, attrs);  
    getHolder().addCallback(this);  

  } 

  @Override
  protected void onDraw(Canvas canvas) {

    super.onDraw(canvas);

    synchronized (mElements) {
        for (Resistor element : mElements) {
            element.doDraw(canvas);                
        }
    }   

  }

  public void update() {

       mElements.add(new Resistor(getContext(), (int) 10, (int) 10));
       invalidate(); //Does not work!
  }


  @Override
  public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    // TODO Auto-generated method stub

  }

  @Override
  public void surfaceCreated(SurfaceHolder holder) {

    Canvas c = holder.lockCanvas();
    onDraw(c);      
    holder.unlockCanvasAndPost(c);      
  }

  @Override
  public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub  

  } 

}

电阻器.java

public class Resistor extends View{

  private Path mSymbol;
  private Paint mPaint;

  private int mX;
  private int mY;

  //...Override Constructors...    
  public Resistor(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
  }

  public Resistor(Context context, int x, int y){
    super(context);
    mX = x;
    mY = y;
    init();
  }

  private void init() {
    mSymbol = new Path();
    mPaint = new Paint();

    mPaint.setAntiAlias(true);      
    mPaint.setStrokeWidth(2);
    mPaint.setColor(-7829368);
    mPaint.setStyle(Paint.Style.STROKE);

    //...Your code here to set up the path,
    //...allocate objects here, never in the drawing code.

    mSymbol.moveTo(0.0F, 0.0F);
    mSymbol.lineTo(0.0F, 50.0F);
    mSymbol.lineTo(16.666666F, 58.333332F);
    mSymbol.lineTo(-16.666666F, 75.0F);
    mSymbol.lineTo(16.666666F, 91.666664F);
    mSymbol.lineTo(-16.666666F, 108.33333F);
    mSymbol.lineTo(16.666666F, 124.99999F);
    mSymbol.lineTo(-16.666666F, 141.66666F);
    mSymbol.lineTo(0.0F, 150.0F);
    mSymbol.lineTo(0.0F, 200.0F);
    mSymbol.offset(mX, mY);

  }

  public void doDraw(Canvas canvas) {
    canvas.drawPath(mSymbol, mPaint);
  }
4

2 回答 2

0

我以前没有使用过 Surfaceviews,但是如果你在API中可以看到,SurfaceView 类没有名为“invalidate()”的函数。

也许你可以设法以某种方式重用这条线:

holder.unlockCanvasAndPost(c);
于 2012-06-27T16:22:15.120 回答
0

您应该使用 aViewGroup而不是 a SurfaceView。将Resistor-Views 作为子项添加到MyView适当地测量和布局它们,然后ViewGroup会自动处理它们的绘制。

SurfaceView要求您在单独的线程中进行绘图。您必须启动该线程surfaceCreated并根据需要继续重绘视图。对于像您发布的那样简单的视图,仅在 UI 线程中进行绘图是完全可以接受的。不需要SurfaceView

于 2012-06-27T16:15:53.000 回答