Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top