2

I have extended an ImageView and calling a method of ImageView from outside class , inside a Thread. Inside the method I have tried using invalidate postValidate and everything but it never called the onDraw method , is it something to do with the calling method-

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);
    }

}

and my calling method looks like -

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>

but its throwing exception while launching. SO I finally changed the code to-

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

And in a class I am just creating a new instance of TestImageView to work for. If that is the reason to do something here.

Abhishek Choudhary
  • 8,255
  • 19
  • 69
  • 128

2 Answers2

4

Have you tried to set:

   setWillNotDraw(false);

In your constructor?

@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);
}
aglour
  • 924
  • 8
  • 15
  • Beat me by 5 seconds hah :) I'll delete mine. This is most likely the reason why it's not drawing. – dymmeh Oct 17 '13 at 16:22
  • So, make sure you are overriding all ImageView constructors, like I edited above. Most likely it is the second one that is being called. – aglour Oct 17 '13 at 16:34
  • Also, you have an error, your class is called TestImageView and your constructor is called FacePreviewImageView. That shouldn't even compile. – aglour Oct 17 '13 at 16:40
  • well .. constructor as well didn't work.. @aglour.. sorry for test code , I made change , ... – Abhishek Choudhary Oct 17 '13 at 16:43
  • 1
    If you use ImageView in the xml, of course it will not call TestImageView onDraw. You need to use TestImageView on the XML. What exception was it throwing? it may be because you are not overriding all the constructors. – aglour Oct 17 '13 at 16:57
0

setWillNotDraw(false); doesn't work for me.

I had the same issue, here, and I figured out checking that my child class of ImageView has visibility VISIBILE and not GONE.

If it has GONE value for visibility, onDraw will not be called.

ricky.tribbia
  • 124
  • 10
  • That should be obvious, you can't see a `GONE` view so there would be no purpose in having `onDraw` invoked. – afollestad Jul 07 '17 at 17:16