0

当galleryview首次显示图像时,它显示它已缩放到最大可能的尺寸以适合屏幕(如我所愿)。在我点击后,creen 图像将恢复其原始大小。如何保持图像缩放?

class ImageAdapterr extends BaseAdapter {

        /** The parent context */
        private Context myContext;

        /** Simple Constructor saving the 'parent' context. */
        public ImageAdapterr(Context c) {
            this.myContext = c;
        }

        Matrix savedMatrix = new Matrix();

        /** Returns the amount of images we have defined. */
        public int getCount() {
            return UILApplication.photo_buffer_big.size();
        }

        /* Use the array-Positions as unique IDs */
        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        /**
         * Returns a new ImageView to be displayed, depending on the position
         * passed.
         */

        public View getView(final int position, View convertView,
                ViewGroup parent) {

             imView = new ImageView(this.myContext);
            current = imView;
            imgPos = position;
            ImageLoader imageLoader = new ImageLoader(app.getApplicationContext());
            imageLoader.DisplayImage(UILApplication.photo_buffer_big.get(imgPos),
                    imView);

            /* Image should be scaled as width/height are set. */
            imView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            /* Set the Width/Height of the ImageView. */
            imView.setLayoutParams(new Gallery.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
            imView.setOnTouchListener(new OnTouchListener() {
                private static final String TAG = "Touch";
                // These matrices will be used to move and zoom image

                PointF start = new PointF();
                public PointF mid = new PointF();

                // We can be in one of these 3 states
                public static final int NONE = 0;
                public static final int DRAG = 1;
                public static final int ZOOM = 2;
                public int mode = NONE;

                float oldDist;

                public boolean onTouch(View v, MotionEvent event) {

                    ImageView view = (ImageView) v;
                    view.setScaleType(ImageView.ScaleType.MATRIX);
                    switch (event.getAction() & MotionEvent.ACTION_MASK) {
                    case MotionEvent.ACTION_DOWN:

                        savedMatrix.set(matrix);
                        start.set(event.getX(), event.getY());
                        Log.d(TAG, "mode=DRAG");
                        mode = DRAG;
                        break;

                    case MotionEvent.ACTION_POINTER_DOWN:

                        oldDist = spacing(event);
                        Log.d(TAG, "oldDist=" + oldDist);
                        if (oldDist > 10f) {

                            savedMatrix.set(matrix);
                            midPoint(mid, event);
                            mode = ZOOM;
                            Log.d(TAG, "mode=ZOOM");
                        }
                        break;

                    case MotionEvent.ACTION_MOVE:

                        if (mode == DRAG) {

                            matrix.set(savedMatrix);
                            matrix.postTranslate(event.getX() - start.x,
                                    event.getY() - start.y);
                        } else if (mode == ZOOM) {

                            float newDist = spacing(event);
                            Log.d(TAG, "newDist=" + newDist);
                            if (newDist > 10f) {

                                matrix.set(savedMatrix);
                                float scale = newDist / oldDist;
                                matrix.postScale(scale, scale, mid.x, mid.y);
                            }
                        }
                        break;

                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_POINTER_UP:

                        mode = NONE;
                        Log.d(TAG, "mode=NONE");
                        break;
                    }

                    // Perform the transformation
                    Log.d("point",
                            (event.getX() - start.x) + " "
                                    + (event.getY() - start.y));
                    // Log.d("point",start.x +" "+start.y);
                    float[] values = new float[9];
                    matrix.getValues(values);
                    float a = values[Matrix.MTRANS_X];
                    float b = values[Matrix.MTRANS_Y];
                    Log.d("touch matrix", values[Matrix.MPERSP_0] + " "
                            + values[Matrix.MPERSP_1] + " "
                            + values[Matrix.MPERSP_2]);
                    Log.d("touch matrix scale", values[Matrix.MSCALE_X] + " "
                            + values[Matrix.MSCALE_Y]);
                    Log.d("touch matrix scew", values[Matrix.MSKEW_X] + " "
                            + values[Matrix.MSKEW_Y]);
                    Log.d("touch matrix trans", values[Matrix.MTRANS_X] + " "
                            + values[Matrix.MTRANS_Y]);
                    if (reset == 1) {
                        matrix.reset();
                        savedMatrix.reset();
                    }

                    view.setImageMatrix(matrix);
                    onFront = view;
                    reset = 0;
                    // images.set(position, view);
                    return true; // indicate event was handled
                }

                private float spacing(MotionEvent event) {
                    float x = event.getX(0) - event.getX(1);
                    float y = event.getY(0) - event.getY(1);
                    return (float) Math.sqrt(x * x + y * y);
                }

                private void midPoint(PointF point, MotionEvent event) {

                    float x = event.getX(0) + event.getX(1);
                    float y = event.getY(0) + event.getY(1);
                    point.set(x / 2, y / 2);
                }

            });

            return imView;
        }

        /**
         * Returns the size (0.0f to 1.0f) of the views depending on the
         * 'offset' to the center.
         */
        public float getScale(boolean focused, int offset) {
            /* Formula: 1 / (2 ^ offset) */
            return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset)));
        }
    } 

编辑:如果添加

view.setScaleType(ImageView.ScaleType.FIT_CENTER);
                    /* Set the Width/Height of the ImageView. */
                    view.setLayoutParams(new Gallery.LayoutParams(
                            LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

到 onTouch 图像保持缩放,但不再可拖动。

4

0 回答 0