如何正确获得缩放效果?

时间:2013-02-14 13:30:17

标签: android image-processing image-zoom

我试图获得缩放效果,我面临的问题很少

  1. 当图像的宽度和高度大于imageview中的图像时,以缩放显示。我希望我的图像应该是imageview的大小。
  2. 虽然我已经设置了最大和最小变焦的限制,然后我尝试缩放比我的图像更改位置。
  3. 这是我的代码

    public class display_page extends Activity {
    Bitmap org_bmp, bmp;
    ImageView img;
    Matrix matrix = new Matrix();
    Matrix savedMatrix = new Matrix();
    
    // We can be in one of these 3 states
    static final int NONE = 0;
    static final int DRAG = 1;
    static final int ZOOM = 2;
    int mode = NONE;
    
    // Remember some things for zooming
    PointF start = new PointF();
    PointF mid = new PointF();
    float oldDist = 1.0f;
    private float MAX_ZOOM = 1.25f;
    private float MIN_ZOOM = 0.25f;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.display_image);
        init();
        final String path = getIntent().getStringExtra("imagePath");
        org_bmp = BitmapFactory.decodeFile(path);
        // Drawable img_drawable = Drawable.createFromPath(path);
    
        img.setImageBitmap(org_bmp);
        img.setOnTouchListener(new OnTouchListener() {
    
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                // img.setImageMatrix(matrix);
    
                switch (event.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN:
                    savedMatrix.set(matrix);
                    start.set(event.getX(), event.getY());
                    mode = DRAG;
                    break;
                case MotionEvent.ACTION_POINTER_DOWN:
                    oldDist = spacing(event);
                    if (oldDist > 10f) {
                        savedMatrix.set(matrix);
                        midPoint(mid, event);
                        mode = ZOOM;
                    }
                    break;
                case MotionEvent.ACTION_UP:
    
                case MotionEvent.ACTION_POINTER_UP:
                    mode = NONE;
                    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);
                        if (newDist > 10f) {
                            matrix.set(savedMatrix);
                            float scale = newDist / oldDist;
                            matrix.postScale(scale, scale, mid.x, mid.y);
    
                            float[] values = new float[9];
                            matrix.getValues(values);
                            float scaleX = values[Matrix.MSCALE_X];
                            float scaleY = values[Matrix.MSCALE_Y];
                            if (scaleX > MAX_ZOOM) {
                                scaleX = MAX_ZOOM;
                            } else if (scaleX < MIN_ZOOM) {
                                scaleX = MIN_ZOOM;
                            }
    
                            if (scaleY > MAX_ZOOM) {
                                scaleY = MAX_ZOOM;
                            } else if (scaleY < MIN_ZOOM) {
                                scaleY = MIN_ZOOM;
                            }
    
                            values[Matrix.MSCALE_X] = scaleX;
                            values[Matrix.MSCALE_Y] = scaleY;
                            matrix.setValues(values);
                        }
                    }
                    break;
    
                }
                // Bitmap resizedImage = Bitmap.createBitmap(bitmap, 0,
                // 0,bitmap.getWidth(),bitmap.getHeight(), matrix, true);
                // limitZoom(matrix);
                // limitDrag(matrix);
                img.setImageMatrix(matrix);
                return true;
            }
        });
    
    }
    
    private void init() {
        // TODO Auto-generated method stub
        img = (ImageView) findViewById(R.id.image);
    
    }
    
    private float spacing(MotionEvent event) {
        float x = event.getX(0) - event.getX(1);
        float y = event.getY(0) - event.getY(1);
        return FloatMath.sqrt(x * x + y * y);
    }
    
    /** Calculate the mid point of the first two fingers */
    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);
    }
    

    请帮助我并纠正我的代码。提前致谢

1 个答案:

答案 0 :(得分:0)

我搜索了很多这个问题,但最后我得到了这个link的确切解决方案。这段代码对我很有用。