Android resize view with pinch

I am making an app that adds stickers on photos and i want to resize the sticker with a pinch gesture.

 final ImageView newSticker = new ImageView(getApplicationContext());
                    newSticker.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,FrameLayout.LayoutParams.WRAP_CONTENT));
                    Bitmap sticker = BitmapFactory.decodeResource(getResources(),galleryList[position]);
                    newSticker.setImageBitmap(sticker);
                    viewGroup.addView(newSticker);

I have a Frame Layout with an ImageView and i am adding Views on it. This is the onTouch method for the sticker:

newSticker.setOnTouchListener(new View.OnTouchListener()
                    {
                        PointF DownPT = new PointF(); // Record Mouse Position When Pressed Down
                        PointF StartPT = new PointF(); // Record Start Position of 'img'
                        float olddistance;

                        @Override
                        public boolean onTouch(View v, MotionEvent event)
                        {
                            selectedSticker = newSticker;

                            switch (event.getAction())
                            {

                                case MotionEvent.ACTION_MOVE :
                                    if(event.getPointerCount() == 1)
                                    {
                                        newSticker.setX((int) (StartPT.x + event.getX() - DownPT.x));
                                        newSticker.setY((int) (StartPT.y + event.getY() - DownPT.y));
                                    }
                                    else if(event.getPointerCount() == 2)
                                    {
                                        final float dX =event.getX(0) - event.getX(1);
                                        final float dY =event.getY(0) - event.getY(1);
                                        float newdistance = (float) Math.sqrt(dX * dX + dY * dY);
                                        float distance = newdistance / olddistance;
                                        FrameLayout.LayoutParams lp= new FrameLayout.LayoutParams((int) (newSticker.getHeight() * distance), (int) (newSticker.getWidth() * distance));
                                        newSticker.setLayoutParams(lp);
                                    }
                                    StartPT.set( newSticker.getX(), newSticker.getY() );
                                    break;
                                case MotionEvent.ACTION_DOWN :
                                    if(event.getPointerCount() == 1)
                                    {
                                        DownPT.set(event.getX(), event.getY());
                                        StartPT.set(newSticker.getX(), newSticker.getY());
                                    }
                                    else if(event.getPointerCount() == 2)
                                    {
                                        final float odX =event.getX(0) - event.getX(1);
                                        final float odY =event.getY(0) - event.getY(1);
                                        olddistance = (float) Math.sqrt(odX * odX + odY * odY);
                                    }
                                    break;

                                case MotionEvent.ACTION_POINTER_DOWN:

                                case MotionEvent.ACTION_UP :
                                    // Nothing have to do
                                    break;
                                default :
                                    break;
                            }
                            return true;
                        }
                    });

In the code above it is successfully moving stickers, but i don't know how to resize the View with a pinch. I don't want the sticker to scale inside of that View, but i want the whole View to be resized.



from Recent Questions - Stack Overflow https://ift.tt/3mk0H66
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Hibernate Search - Elasticsearch with JSON manipulation

Spring Elasticsearch Operations