ImageView Drag Limitation In Android

前端 未结 5 1665
遇见更好的自我
遇见更好的自我 2021-02-09 06:21

I have an ImageView in a layout and set OnTouchListener on ImageView to drag the ImageView. It\'s working perfectly. My problem is how can I prevent from move ImageView to out o

5条回答
  •  忘掉有多难
    2021-02-09 06:43

    I have done this code. when I zoom hen it should not go out of boundry. its working fine.But when it touches cordinate of x=0 and y=0 it do not minimize.

    int FLAG=0;   
    
    
    
     else if (mode == ZOOM) {
    
                        float newDistance = spacing(event);
    
                        Log.i("new distance ", newDistance+"");
    
                        matrix.getValues(matrixValues);
                        matrixX = matrixValues[2];
                        matrixY = matrixValues[5];
                        width = matrixValues[0]
                                * (((ImageView) view).getDrawable().getIntrinsicWidth());
                        height = matrixValues[4]
                                * (((ImageView) view).getDrawable()
                                        .getIntrinsicHeight());
    
                        dx = event.getX() - start.x;
                        dy = event.getY() - start.y;
    
                        // if image will go outside left bound
                        if (matrixX + dx < 0) {
                            FLAG=1;
                        }
                        if (matrixX + dx + width > view.getWidth()) {
                            FLAG=1;
                            //dx = view.getWidth() - matrixX - width;
                        }
                        // if image will go oustside top bound
                        if (matrixY + dy < 0) {
                            FLAG=1;
                            //dy = -matrixY;
                        }
                        // if image will go outside bottom bound
                        if (matrixY + dy + height > view.getHeight()) {
                            FLAG=1;
                            //dy = view.getHeight() - matrixY - height;
                        }
                    if (matrixX + dx ==0 || matrixY + dy==0){ 
                        FLAG=0;
                    }
    
                        if (newDistance > 10f && FLAG==0) {
    
                            matrix.set(savedMatrix);
    
                            float scale = newDistance / oldDistance;
    
                            float[] values = new float[9];
    
                            matrix.getValues(values);
                            float currentScale = values[Matrix.MSCALE_X];
                            if (scale * currentScale > MAX_ZOOM)
                                scale = MAX_ZOOM / currentScale;
                            else if (scale * currentScale < MIN_ZOOM)
                                scale = MIN_ZOOM / currentScale;
                            matrix.postScale(scale, scale, mid.x, mid.y);
                        }
                    }
                    break;
    

提交回复
热议问题