为什么无效无效?

时间:2012-02-01 11:06:50

标签: android

我正在尝试绘制一个矩形,我最初在Activity类中提供它的值,然后尝试从不同的类更改其坐标。就我而言,逻辑是正确的,但是无效的不起作用..我做了大量的研究,但仍然无法使其发挥作用..

RectangleActivity.java

public class RectangleActivity extends Activity {

  private Point dStart = null;
  private Point dEnd = null;
  final private Handler handler = new Handler();
  Check check = new Check(this);
  Draw draw ;

  Runnable mUpdate = new Runnable()
    {
    public void run()
    {           
     Toast.makeText(getApplicationContext(), "update Called", Toast.LENGTH_LONG).show();
      check.update();           
    }
 };
      @Override
       public void onCreate(Bundle savedInstanceState) {        
        super.onCreate(savedInstanceState);
        dStart = new Point(0,0);
        dEnd = new Point(60,60);
        draw = new Draw(this,dStart,dEnd);
        setContentView(draw);
        handler.postDelayed(mUpdate, 4000); 
      } 
 }

Check.java

    public class Check 
    {

  private Point start = new Point(80,80);
  private Point end = new Point(150,150);
  private Context context;

  public Check(Context context)
  {
    this.context =context;      
  } 
    void update() 
  { 
    Log.d("Aditi", "Update Called");
    Draw draw = new Draw(context);
    draw.reDraw(start,end);     
  } 
 }

Draw.java

    public class Draw extends View
    {   
 private Paint paint = null;
 private Point start = null;
 private Point end = null;  

 public Draw(Context context) 
  {
    super(context);     
    paint();    
  }     
  public Draw(Context context, Point start, Point end) 
  {
    super(context);

    this.start= start;
    this.end = end;
    paint();
  } 

   protected void onDraw(Canvas canvas)
   {
    super.onDraw(canvas);
    canvas.drawRect(start.x,start.y,end.x,end.y,paint);
    Log.d("Aditi", "OnDraw Called");
    System.out.println("StartX in ondraw is: "+start.x);
    System.out.println("StartY in ondraw is: "+start.y);
    System.out.println("EndX in ondraw is: "+end.x);
    System.out.println("EndY in ondraw is: "+end.y);    
  }

  public void reDraw(Point Start, Point End)
  {
    Log.d("Aditi", "Redraw Called");
    this.start = Start;
    this.end = End;
    invalidate();
    Log.d("Aditi", "Invalidate  Called");
    System.out.println("StartX in redraw is: "+start.x);
    System.out.println("StartY in redraw is: "+start.y);
    System.out.println("EndX in redraw is: "+end.x);
    System.out.println("EndY in redraw is: "+end.y);        
   }    
     public void paint()
   {
    paint= new Paint();     
    paint.setColor(Color.YELLOW);
    paint.setAntiAlias(true);
    paint.setStyle(Paint.Style.FILL_AND_STROKE);
  }   
}

请随时指出我做错了什么..提前致谢..

3 个答案:

答案 0 :(得分:0)

我担心如果它不适合你。请尝试调用无效方法onDraw()

protected void onDraw(Canvas canvas){
    super.onDraw(canvas);
    invalidate();
}

答案 1 :(得分:0)

在上面的问题中,在onCreate()中你使用 draw = new Draw(this,dStart,dEnd); 进行了视图 然后在setContentView( draw )中使用,但是当你再次调用update()时,使用 Draw draw = new Draw(this,dStart,dEnd)再次创建了Draw对象; 并在第二个绘图对象上调用draw.reDraw(start,end),因此setContentView()中使用的实例不会失效,并且第二个绘制视图对象无效,但它没有在setContentView()中传递,因此它不可见在ui上:你的解决方案只是

    void update() {
    //    Draw draw = new Draw(context);  //remove this
        draw.reDraw(start, end);
    }

答案 2 :(得分:0)

如果您从应用程序主线程以外的线程中使视图无效,它将无效,您必须调用postInvalidate()而不是invalidate()。