2013-02-14 81 views
0

我想獲得的變焦效果和我面臨的一些問題如何正確獲取縮放效果?

  1. 當圖像的寬度和高度比大在imageview的圖像顯示在變焦。我想我的圖像應該是imageview的大小。
  2. 雖然我已經設置了最大和最小縮放限制,之後我試圖放大比我的圖像更改位置更多。

這是我的代碼

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); 
} 

請幫助我,糾正我的代碼。在此先感謝

回答

0

我搜索了很多這個問題,但最後我從這link確切的解決方案。這段代碼對我很好。