我扩展了一个 ImageView 并从外部类的线程内调用 ImageView 的方法。在方法内部,我尝试使用 invalidate postValidate 和所有内容,但它从未调用 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());
}
});
One more point to add-
I tried to add this view directly inside layout file-
<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 的新实例。 如果这就是在这里做某事的原因。
最佳答案
您是否尝试过设置:
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);
}
关于android - 未调用扩展 ImageView onDraw 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19431957/