如何获得静态视图的大小?

时间:2014-04-29 12:34:53

标签: android

我想获取自定义视图的高度,以便动画可以覆盖布局的所有大小,无论使用什么设备。问题是,View需要是静态的,所以当我试图获得它的高度时,它表示我不能从静态类中调用非静态方法。

private static class EjemploView extends View {

     private Path path = new Path();
     private static float Last_X = 0;
     private static float Last_Y = 0;

    public EjemploView (Context context) {

        super(context);

        new Thread(new Runnable() {
            public void run() {
                y=30;

                while(true){
                    try {
                        Thread.sleep(sleeptime);
                        if(!direction)
                        {
                        y=y+StepY_Figura;
                        }
                        else
                        {   
                            y=y-StepY_Figura;                            
                        }
                        if(y<30)
                        {
                            direction=false;                                
                            paint.setColor(Color.BLACK);
                            Thread.sleep(350);
                        }

                        if(y>260)
                        {
                            direction=true;                             
                            paint.setColor(Color.WHITE);
                            Thread.sleep(350);                              
                        }
                        postInvalidate();

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
          }).start();          


    }

@Override

protected void onDraw(Canvas canvas) {


    float x = canvas.getWidth() / 4;
    float x2=x;


    if(Unaprueba==true)
    {
        canvas.drawColor(Color.BLACK);
    }
    else
    {
        canvas.drawColor(Color.WHITE);  
    }
    canvas.drawCircle(x, y, 30, paint);
    x =x+x2+4;
    canvas.drawCircle(x, y, 30, paint);
    x =x+x2+4;
    canvas.drawCircle(x, y, 30, paint);
    String s =Integer.toString(sleeptime);
    canvas.drawText("RITHM: "+s, 10, 20, paint);

}

@Override
public boolean onTouchEvent(MotionEvent event) {
  float eventX = event.getX();
  float eventY = event.getY();
  int Change_coordY=10;
  int Change_Delay=5;
  int Minimum_Delay=5;
  switch (event.getAction()) {
  case MotionEvent.ACTION_DOWN:
    //path.moveTo(eventX, eventY);
    Last_X=eventX;
    Last_Y=eventY;

    return true;
  case MotionEvent.ACTION_MOVE:
    break;
  case MotionEvent.ACTION_UP:


      if(eventY>Last_Y+Cambio_coordY)
      {           
          sleeptime=sleeptime-Change_Delay;
          if(sleeptime<=Minimum_Delay)
          {
              sleeptime=Minimum_Delay;
          }

      }
      else if(eventY<Last_Y-Change_coordY)
      {
          sleeptime=sleeptime+Change_Delay;

      }
    break;
  default:
    return false;
  }

  invalidate();
  return true;
}    
}

如果View是静态的,你知道我是否有办法获得布局的大小?提前谢谢。

0 个答案:

没有答案