invalidate method only working when put in 'onDraw'

Alexandre Maneta :

I'm trying to refresh the display of a clepsydra by using invalidate in one of the setter of the class. However, it doesn't seem to be called. On the other hand, if I put invalidate in the onDraw method, it works. Is there something I'm missing in the documentation.

So far, I've tried writing invalidate in various manner :

  • this.invalidate()
  • invalidate()
    public void setFillRatio(double fillRatio) {
        if (this.fillRatio != fillRatio){
            this.fillRatio = fillRatio;
            this.invalidate();
            Log.i("je suis passée", fillRatio + "");
        }

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint p = new Paint();
        Paint p2 = new Paint();
        p.setColor(Color.GRAY);
        p2.setColor(Color.BLUE);
        Log.i("filll ratio on draw",canvas.getHeight() - (canvas.getHeight() * fillRatio) + "");
        canvas.drawRect(new Rect(0,0, canvas.getWidth(), canvas.getHeight()), p);
        canvas.drawRect(new Rect(0, (canvas.getHeight() - (int)(canvas.getHeight() * fillRatio)), canvas.getWidth(), canvas.getHeight()), p2);
    }

Oddly enough, it does log what's under the invalidate method in setFillRatio() but the log from onDraw isn't called.

Edit 1:

If somebody wants to test the project feel free to download it via the google drive url : drive url of the project

Son Truong :

Problem: From your code, in activity_count_down.xml, you already a custom view named Clepsydra.

<com.example.countdown.Clepsydra
    android:id="@+id/view_clepsydra"
    android:layout_width="363dp"
    android:layout_height="379dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.615"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/button4" />

But in CountDownActivity.java, you declare a new instance of the custom view class

c = new Clepsydra(this);

Because they are totally different, that why you cannot see onDraw is called.

Solution: Modify your code to refer the custom view in layout xml file instead.

CountDownActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_count_down);

    // Comment-out this line.
    // c = new Clepsydra(this);

    // Add this line instead
    c = findViewById(R.id.view_clepsydra);

    h = new Handler();
    pi =  PendingIntent.getActivity(this, 1, new Intent(this, EndOfCountDownActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
    am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
    refreshRunnable = new Runnable() {
        @Override
        public void run() {
            if (isActive){
                long val = countdown - (SystemClock.elapsedRealtime() - startTime);
                TextView cd = findViewById(R.id.textView4);
                cd.setText(String.format("%02d:%02d:%02d",
                        TimeUnit.MILLISECONDS.toHours(val),
                        TimeUnit.MILLISECONDS.toMinutes(val) -
                                TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(val)),
                        TimeUnit.MILLISECONDS.toSeconds(val) -
                                TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(val))));
                c.setFillRatio((double)val/countdown);
                // Comment-out this line as well.
                // c.invalidate();
                h.postDelayed(refreshRunnable, 500);
            }
        }
    };
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=158174&siteId=1