thuytrinh / android-collage-views

Draggable, rotatable, and scalable views for Android
332 stars 88 forks source link

option to prevent views from translating beyond the boarders of parent #1

Open kcochibili opened 10 years ago

kcochibili commented 10 years ago

could you implement a way to set if the view is allowed to be translated/Scaled beyond the parents boarders. it would be really nice if you can.

kcochibili commented 10 years ago

I have updated the Multitouch listener (pending to be merged) to allow the developer to toggle if the view can be translated beyond the walls of the parent. i don't know how to make such modifications for scaling yet :/

thorbenprimke commented 10 years ago

@kcochibili awesome. I just added the multiTouchListener to my project and was looking for the same. I patched in your change locally.

One thing that I found is that your logic assumes that the parent is aligned to its parent top. If the parent has a marginTop/Bottom, the calculation is off.

I fixed it by taking adjusting the parentTop/Bottom based on the margin. In the snippet below, you can also see how I adjusted the item's viewTop/Bottom based on scaling.

int adjustedHeight = (int) ((v.getHeight() * v.getScaleY() - v.getHeight()) / 2.f);
int viewTop = v.getTop() - adjustedHeight;
int viewBottom = v.getBottom() + adjustedHeight;
View p = (View) v.getParent();
ViewGroup.MarginLayoutParams pLayoutParams = (ViewGroup.MarginLayoutParams) p.getLayoutParams();
int parentTop = p.getTop() - pLayoutParams.topMargin;
int parentBottom = p.getBottom() - pLayoutParams.topMargin - pLayoutParams.bottomMargin;

It now works with scaling but does not take into account rotation yet. I think that's also important because otherwise the user can still move an item past the parent's boundaries if it is rotated.

SwordBearer commented 10 years ago

@thorbenprimke @kcochibili Hi ! I know the view's center point is not change actually ,But if I translation& scale the view ,how can I get the visual center point ?

In the snippet below, I calculate the center point (centerX,centerY), but if scale ,the value is not right ,can you help please ?

            double transX = view.getTranslationX() + deltaVector[0];
            double transY = view.getTranslationY() + deltaVector[1];
            double scaleX = view.getScaleX();
            double scaleY = view.getScaleY();

            double originX = ((double) (view.getLeft() + view.getRight())) / 2;
            double originY = ((double) (view.getTop() + view.getBottom())) / 2;

            double width = view.getWidth() * scaleX;
            double height = view.getHeight() * scaleY;

            double halfWidth = ((double) view.getWidth() * scaleX) / 2;
            double halfHeight = ((double) view.getHeight() * scaleY) / 2;

            /* get the visual center point */
            double centerX = view.getTranslationX() + originX;
            double centerY = view.getTranslationY() + originY;
kcochibili commented 9 years ago

nice improvements! @thorbenprimke I didnt catch that earlier.

@SwordBearer I think @thorbenprimke fixed the issue here https://github.com/thorbenprimke/android-collage-views/commit/e213089c22042de2addca80a062231496bd99b5c

(sorry for the late reply guys)