2013-02-02 34 views
1

我的自定義視圖的onDraw()未被觸發。爲什麼?我正在使用2.3.3。視圖在創建時不應該自動繪製? onMeasure()方法被觸發,我得到正確的屏幕寬度和高度。onDraw創建時未觸發

package com.example.assignment2b; 

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.util.Log; 
import android.view.Display; 
import android.view.Menu; 
import android.view.View; 
import android.view.WindowManager; 

public class AcesActivity extends Activity { 

    private static final String TAG = "DEBUG"; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     HeartView heartView = new HeartView(this); 
     setContentView(heartView); 
     heartView.invalidate(); 
    } 


    class HeartView extends View { 

     private int screenWidth; 
     private int screenHeight; 
     private Context context; 

     public HeartView(Context context) { 
      super(context); 
      this.context = context; 

     } 

     @Override 
     public void onDraw(Canvas canvas) { 
      Log.println(Log.DEBUG, TAG, "onDraw fired"); 
      int cardWidth = screenWidth - 10; 
      int cardHeight = (int) (cardWidth * 1.4); 


      canvas.drawColor(Color.DKGRAY); 

      Paint paint = new Paint(); 
      paint.setColor(Color.WHITE); 
      paint.setStyle(Paint.Style.FILL); 
      canvas.drawRect(5, (screenHeight/2) - (cardHeight/2), screenWidth - 5, (screenHeight/2) + (cardHeight/2), paint); 
      paint.setColor(Color.BLACK); 
      paint.setStyle(Paint.Style.STROKE); 
      canvas.drawRect(5, (screenHeight/2) - (cardHeight/2), screenWidth - 5, (screenHeight/2) + (cardHeight/2), paint); 

     } 

     @Override 
     public void onMeasure(int width, int height) { 
      Log.println(Log.DEBUG, TAG, "onMeasure fired"); 
      WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 
      Display display = wm.getDefaultDisplay(); 
      screenWidth = display.getWidth(); 
      screenHeight = display.getHeight(); 
      setMeasuredDimension(screenWidth, screenHeight); 
      Log.println(Log.DEBUG, TAG, "screenWidth = " + screenWidth); 
      Log.println(Log.DEBUG, TAG, "scrrenHeight = " + screenHeight); 
      super.onMeasure(screenWidth, screenHeight); 
     } 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     return true; 
    } 
} 
+0

我通過Android例子去時發現兩件事情:1)他們裹在佈局定製視圖發送它關閉到活動前和2)他們沒有調用無效()在onCreate。我不確定這些事情中的哪一個能解決你的問題,但你也可以給他們一個機會。 – Prime

回答

1

幾個不同的問題正在進行。首先,如@dragonwrenn所建議的,沒有ViewGroup和佈局參數,您的視圖沒有內在界限。撥打invalidate()是不必要的,但沒有問題。

public class AcesActivity extends Activity { 

    private static final String TAG = "DEBUG"; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     RelativeLayout rl = new RelativeLayout(this); 
     RelativeLayout.LayoutParams params = new 
      RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, 
      LayoutParams.MATCH_PARENT); 
     setContentView(rl); 

     HeartView heartView = new HeartView(this); 
     heartView.setLayoutParams(params); 
     rl.addView(heartView);  
    } 

    class HeartView extends View { 

     private int screenWidth; 
     private int screenHeight; 
     private Context context; 

     // Moved a couple of variable declarations out of the onDraw method 
     Paint paint; 
     int cardWidth; 
     int cardHeight; 

     public HeartView(Context context) { 
      super(context); 
      this.context = context; 
      paint = new Paint(); 

     } 

其次,你的數學爲rects吸引他們超出了屏幕的邊界。我把一些硬數字當做考驗。你會想重新修改你的電話號碼以得到你真正想要的。此外,您的行程沒有寬度,因此請注意

 @Override 
     public void onDraw(Canvas canvas) { 

      Log.println(Log.DEBUG, TAG, "onDraw fired"); 

      canvas.drawColor(Color.DKGRAY); 

      paint.setColor(Color.WHITE); 
      paint.setStyle(Paint.Style.FILL); 
      canvas.drawRect(10, 10, 50, 50, paint); 
      paint.setColor(Color.BLACK); 
      paint.setStyle(Paint.Style.STROKE); 
      paint.setStrokeWidth(3); 
      canvas.drawRect(10, 10, 50, 50, paint); 

     } 

第三,我不知道什麼一切onMeasure試圖實現,但它造成的觀點的問題。你不應該需要比什麼是下面更多:

 @Override 
     public void onMeasure(int width, int height) { 
      Log.println(Log.DEBUG, TAG, "onMeasure fired"); 
      screenWidth = width; 
      screenHeight = height; 

      Log.println(Log.DEBUG, TAG, "screenWidth = " + screenWidth); 
      Log.println(Log.DEBUG, TAG, "scrrenHeight = " + screenHeight); 
      super.onMeasure(screenWidth, screenHeight); 
      cardWidth = screenWidth - 10; 
      cardHeight = (int) (cardWidth * 1.4); 
     } 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     return true; 
    } 
} 

編輯:紙條給原來的海報,上面是在Eclipse中測試的功能代碼。如果每個部分都被完全複製並粘貼,它就會起作用。

+0

Upvote-深入分析與建議 – Prime

+0

感謝您的建議。不幸的是,在我實現它們之後,onDraw仍然不會啓動。 – badgerduke

+0

如果您完全複製此代碼,它將會觸發。我自己在Eclipse中運行它,在屏幕上看到一個正方形,在logcat中看到了onDraw。再試一次。 – anthropomo

0

onMeasure()方法中執行以下更改: 在您的班級中聲明顯示度量標準。 private DisplayMetrics m_metrics;

 @Override 
    public void onMeasure(int width, int height) { 
     Log.println(Log.DEBUG, TAG, "onMeasure fired"); 
      Display m_display = getWindowManager().getDefaultDisplay(); 
     m_metrics = context.getResources().getDisplayMetrics(); 
     m_display.getMetrics(m_metrics); 
     screenWidth = m_display.getWidth(); 
     screenHeight = m_display.getHeight(); 
      super.onMeasure(screenWidth, screenHeight); 
    } 
+0

謝謝你的提示。不幸的是,當我嘗試它時,它不會導致onDraw觸發。 – badgerduke

+0

我已經嘗試過並測試過它。我得到它的工作成功,也在我的logcat它打印onDraw發射消息。請通過複製我的代碼來嘗試。 – GrIsHu