Home » Java » java – Add sticker only once using invalidate()-Exceptionshub

java – Add sticker only once using invalidate()-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

On clicking 2nd sticker it should display the second sticker alone (the first one should be removed).
current output
when clicking in sticker 1(yellow),it display sticker 1 on screen and when sticker 2 (red) is clicked the previous sticker is still on the screen(which i do not want)
The output must be sticker 2 alone
I have tried implementing a for loop but it does not work.
Full Activity source code
Figure 1 is the current output,fig 2 and 3 are expected output for 1st and 2nd clicks respectively

public void addSticker(final Sticker sticker) {
  if (sticker == null) {
     Log.e(TAG, "Sticker to be added is null!");
     return;
   }

    float offsetX = (getWidth() - sticker.getWidth());
    float offsetY = (getHeight() - sticker.getHeight());
    sticker.getMatrix().postTranslate(offsetX, offsetY);
    float scaleFactor;
    if (getWidth() < getHeight()) {
         scaleFactor = (float) getWidth() / sticker.getDrawable().getIntrinsicWidth();
    } 
    else {
          scaleFactor = (float) getHeight() / sticker.getDrawable().getIntrinsicHeight();
      }
    sticker.getMatrix().postScale(scaleFactor, scaleFactor, getWidth(), getHeight());

    mHandlingSticker = sticker;
    mStickers.add(sticker);
    invalidate();
}
How to&Answers: