2013-10-17 32 views
1

我已經擴展了一個ImageView,並在Thread的內部從外部類調用ImageView的一個方法。裏面的方法我一直在使用無效postValidate,一切嘗試,但它永遠不會叫的onDraw方法,是什麼做的調用方法 -擴展ImageView的onDraw方法沒有被調用

public class TestImageView extends ImageView { 


    public FacePreviewImageView(Context context) { 
     super(context); 

    } 


    public void process(String strImageFilePath) { 
    //doing some operation 

      invalidate(); 


    } 


    @SuppressLint("DrawAllocation") 
    @Override 
    protected void onDraw(Canvas canvas) { 
     Log.i("CAME INSIDE ", ""+faces.total()); 
     if(faces.total()>0){ 
     Paint paint = new Paint(); 
     paint.setColor(Color.RED); 
     paint.setTextSize(20); 

     String s = "Processed Face"; 
     float textWidth = paint.measureText(s); 
     canvas.drawText(s, (getWidth() - textWidth)/2, 20, paint); 
     CvRect r = new CvRect(cvGetSeqElem(faces, 0)); 
     int x = r.x(), y = r.y(), w = r.width(), h = r.height(); 
     canvas.drawRect(new Rect(x, y, x+w, y+h), paint); 

     } 
     super.onDraw(canvas); 
    } 

} 

和我的調用方法看起來像 -

new Handler().post(new Runnable() { 
       @Override 
       public void run() { 
        // Code here will run in UI thread 
        ((TestImageView)imageView).process(pictureFile.getAbsolutePath()); 
       } 
      }); 

還有一點要添加 -

我試圖直接添加內部佈局這一觀點文件 -

<FrameLayout 
     android:id="@+id/ll2" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="0.5" 
     android:orientation="horizontal" > 

<com.example.defaultfacetracker.TestImageView 
      android:id="@+id/imageView1"    
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      /> 
</FrameLayout> 

但它在啓動時拋出異常。所以我終於改變了代碼 -

<ImageView 
      android:id="@+id/imageView1"    
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:src="@android:drawable/toast_frame" /> 

而在一個類中,我只是創建一個TestImageView的新實例來工作。 如果這是在這裏做點事情的原因。

+0

看到[創建自定義ImageView](http://stackoverflow.com/questions/1470867/creating-custom-imageview)也許幫你 –

回答

4

你試過設置:

setWillNotDraw(false); 

在你的構造?

@Override 
public FacePreviewImageView(Context context) { 
    this(context, null, 0); 
} 

@Override  
public FacePreviewImageView(Context context, AttributeSet attrs) { 
    this(context, attrs, 0); 
} 

@Override 
public FacePreviewImageView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    setWillNotDraw(false); 
} 
+0

打敗我5秒哈:)我會刪除我的。這很可能是它不繪製的原因。 – dymmeh

+0

這沒有解決...... –

+0

因此,確保您重寫所有ImageView構造函數,就像我上面編輯的一樣。 很可能是第二個被調用的。 – aglour

0

setWillNotDraw(false);對我不起作用。

我有同樣的問題,在這裏,我也想通了檢查我的子類的ImageView知名度VISIBILE,而不是GONE

如果它具有GONE值的可見性,onDraw將不會被調用。

+0

這應該是顯而易見的,你不能看到'GONE'視圖,所以沒有調用'onDraw'的目的。 – afollestad

相關問題