wuapnjie / StickerView

[No more support] A view which can add sticker and zoom,drag,delete it
MIT License
1.27k stars 311 forks source link

Sticker-view reset all sticker to middle of screen #27

Open mrsokkary opened 7 years ago

mrsokkary commented 7 years ago

i've an issue i user dialog with custom layout contains edit text to add Text Sticker the problem is after return from the dialog to the main activity that contain the sticker view all added sticker return to the middle of the activity as if it was loaded on activity load can any one please help me how to resolve this issue

nayankukadiya commented 7 years ago

Your dialog taking more space then device width so when dialog open and closed it refresh layout of main activity or either bottom navigation bar display or hide in this between because of this you stickerview refreshed and all sticker moved to center.

mrsokkary commented 7 years ago

so what i shall do to prevent the sticker_view from refresh on this case

nayankukadiya commented 7 years ago

you can make fixed layout of sticker view that will not affected by any resolution change and in bottom navigation case you need to permanent hide or permanent show navigation bar in your stickerview activity throughout.

nithinpmolethu commented 7 years ago

Just add stickerview in between a linear layout.

abbath0767 commented 7 years ago

some problem

abbath0767 commented 7 years ago

i resolve my problem - in compound view after any event (drag, resize..) i save in HashMap sticker + Matrix current sticker. and after called super.onLayout in my parent view recreate stickers

thinhdt commented 7 years ago

@abbath0767 Currently , I have faced with the same problem . Can you help me ? Can you paste your code ? Thank you.

thinhdt commented 7 years ago

@abbath0767 Can you help me ? Thanks.

kripeshadwani commented 7 years ago

I observed that when I was adding text using Dialog it was resizing my layout which led sticker view to be reset at center when layout gets backs to its original size.

I solved the problem by adding this line to my manifest where I declared that particular activity.

android:windowSoftInputMode="adjustNothing"

Stepyon commented 6 years ago

The same problem, plus stickers are not being resized if sticker view changes it's size, it happens when I rotate the screen for example, I save stickers and restore but they have wrong positions and sizes

rosanlal26 commented 6 years ago

android:windowSoftInputMode="adjustNothing"commented by @kripeshadwani solved my problem too. Thanks @kripeshadwani

vlad-roid commented 5 years ago

There's a much better solution. Change transformSticker (which is called inside onSizeChanged and is the culprit that centers all stickers, what's even the point of that?) to this implementation:

protected void transformSticker(Sticker sticker, int w, int h, int oldw, int oldh) {
    sizeMatrix.set(sticker.getMatrix());
    float stickerWidth = sticker.getCurrentWidth();
    float stickerHeight = sticker.getCurrentHeight();
    float translateX = sticker.getMatrixValue(Matrix.MTRANS_X);
    float translateY = sticker.getMatrixValue(Matrix.MTRANS_Y);
    float scaleX = (float) w / oldW;
    float scaleY = (float) h / oldH;
    // move the origin to center of the sticker to make scaling easier
    sizeMatrix.postTranslate(-translateX - stickerWidth / 2f, -translateY - stickerHeight / 2f);
    // scale the sticker according to the difference in width and height
    sizeMatrix.postScale(scaleX, scaleY);
    // move the origin to where it was, but also apply the scale to the coordinates
    sizeMatrix.postTranslate((translateX + stickerWidth / 2f) * scaleX,
                             (translateY + stickerHeight / 2f) * scaleY);
    sticker.setMatrix(sizeMatrix);
}

This scales all stickers according to the difference in width and height the view experiences for whatever reason, so that they essentially don't move at all. Finally, add an invalidate() call to the end of onSizeChanged (no need to call it inside transformSticker after each matrix recalculation, it's a waste of resources).