android - 未调用扩展 ImageView onDraw 方法

标签 android android-imageview ondraw

我扩展了一个 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/

相关文章:

Android自定义 View 位图内存泄漏

java - 如何通过 View 填充收缩 Canvas ?

android - 如何使用ViewFlipper在AppWidget中像广告轮播一样动态添加图片

android - 使用 Espresso 检查一个 View 是否被另一个同级 View 隐藏

Android 设计指南 - 列表操作按钮

android - 如何在Android中添加全透明到半透明的渐变

android - Firebase Cloud可用于Android应用程序通知

android - 将 Glide 图像更新到 ImageView

android - 将文本和图像彼此完美对齐

java - 自定义 View 类的 onDraw 未被 Invalidate 调用