我已經擴展了一個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的新實例來工作。 如果這是在這裏做點事情的原因。
看到[創建自定義ImageView](http://stackoverflow.com/questions/1470867/creating-custom-imageview)也許幫你 –