2

我已经完成了这段代码。当我放大母鸡时,它不应该越界。它工作正常。但是当它触及 x=0 和 y=0 的坐标时,它不会最小化。我在这里使用了 FLAG 来控制它,我们可以使用其他东西。

 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;
4

0 回答 0