2011-07-15 24 views
0

我想顯示視圖,屏幕上的座標取決於傳感器數據。我設法顯示一個文本,如that SO question中所解釋的,我現在試圖展示一些TextViews,但是在我的TextViews上調用invalidate()時,它們不會被繪製...做無效時TextView沒有繪製()

任何人都可以提供幫助嗎?

感謝

ARView.java

public class ARView extends TextView 
{ 
    public String name; 
    public float azimuth; 
    public float distance; 
    public float inclination; 
    public Location location; 

    public ARView(Context context) 
    {   
     super(context); 
     setText(name); 
    } 
} 

ARActivity.java

在這裏我我ARView添加到它的父視圖。

public class ARActivity extends Activity{ 

    private ARLayout ar; 
    private CBCameraView cv; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

      super.onCreate(savedInstanceState); 

      ar = new ARLayout(getApplicationContext()); 

      cv = new CBCameraView(this.getApplicationContext()); 

      FrameLayout rl = new FrameLayout(this.getApplicationContext()); 

      rl.addView(cv, GlobalVars.screenWidth, GlobalVars.screenHeight); 
      rl.addView(ar, GlobalVars.screenWidth, GlobalVars.screenHeight); 

      setContentView(rl); 

      Location location = new Location("me"); 
      location.setLatitude(41.371974); 
      location.setLongitude(2.166978); 

      ARView fs = new ARView(this.getApplicationContext()); 
      fs.azimuth = 0; 
      fs.inclination = 0; 
      fs.location = location; 
      fs.name="Bar seco"; 
      ar.addARView(fs);      
    } 
} 

ARLayout.java

在這裏,我得到的傳感器的數據,更新我的ARViews座標,希望吸引他們......

public class ARLayout extends View implements LocationListener, SensorEventListener 
{ 

    // ... 

    @Override 
    public void onSensorChanged(SensorEvent event) { 

     // here I get sensors data and update my views 

     for(int i=0; i<nArViews; i++) 
     { 

      ARView view = arViews.get(i);    

      int x = ...; // new x coordinate of my ARView 
      int y = ...; // new y coordinate of my ARView 

      view.layout(x, y, view.getBottom(), view.getRight()); // set new coordinates to my ARViews 

     } 

     invalidate(); // draw my ARLayout  
    } 

    public void onDraw(Canvas c) 
    { 
     for(int i=0; i<nArViews; i++) 
     {     
      ARView view = arViews.get(i); 
      view.invalidate(); // I thought it would draw my ARViews, but it doesn't :(
     } 
    } 

    // more stuff 

} 

回答

0

我不知道這是解決方案到具體的問題,但我知道這是另一個問題。

刪除invalidate()調用onDraw()方法。每次抽籤只需要調用一次失效。從onDraw()中刪除它將確保屏幕只有當您有新的傳感器數據時纔會更新。屏幕實際上不會畫到onDraw()

或者,您可以在onDraw()的for循環後將呼叫移動到invalidate(),並從onSensorChanged()完全刪除它。這將使屏幕不斷重新繪製,無論您是否有傳感器數據。

0

看來你只是在改變視圖的位置。你不必調用無效,你不必在onDraw上推翻。對於那些操作系統知道何時重繪的東西。無效並且onDraw僅用於自定義繪圖。