在android中实现像画廊一样的缩放缩放

时间:2013-11-06 07:14:27

标签: android android-gallery pinchzoom image-zoom

我在捏缩放上做了太多的研发,发现了很多关于Android的捏缩放的例子..

How pinch zoom image in image zoom android?

android pinch zoom

但是当我试图缩放大图像时(例如:> 10 MB,12200x8521),它无法解码。但内置画廊可以做,它可以缩放。甚至没有变焦但在变焦后它显示非常干净的图像。虽然在上面pichZoomView无法像这样放大..

我找到了QuickPic app,它具有与图库相同的缩放功能。我不知道这些产品如何解码图像和缩放系统库应用程序。

如果我解码完整图像然后它会抛出OutOfMemoryException并且如果我使用采样进行解码,那么当我缩放时它会使图像模糊。

我投入了大量时间来获取图库应用程序的源代码,但我仍然无法从源代码中找到实际的图库缩放实现。任何人都有解决方案吗?或者用于缩放缩放实现的图库源代码。

2 个答案:

答案 0 :(得分:2)

我已投入大量时间并从此处获得解决方案subsampling-scale-image-view

答案 1 :(得分:0)

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);     
setContentView(R.layout.colorchart);
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
                      bmpFactoryOptions.inSampleSize =9;
                      bmpFactoryOptions.inDither=false;                     //Disable Dithering mode
                      bmpFactoryOptions.inPurgeable=true;                   //Tell to gc that whether it needs free memory, the Bitmap can be cleared
                      bmpFactoryOptions.inInputShareable=true;              //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
    //                bmpFactoryOptions.inTempStorage=new byte[102*1024*1024];
                      bmpFactoryOptions.inJustDecodeBounds = true;
                      i1=(ImageView)findViewById(R.id.thefeelingwheelclear);
                      bmp = BitmapFactory.decodeResource(getResources(),R.drawable.thefeelingwheelclear, bmpFactoryOptions);
                      Display display = getWindowManager().getDefaultDisplay();
                      int width = display.getWidth();
                      int height = display.getHeight();
                      if (bmpFactoryOptions.outWidth > width || bmpFactoryOptions.outHeight > height) {
                          float heightRatio = (float) height / (float) bmpFactoryOptions.outHeight;
                          float widthRatio = (float) width / (float) bmpFactoryOptions.outWidth;
                          // WHY
                          if(height >= 1024 )
                          {
                              heightRatio = heightRatio /1.2f;
                              widthRatio = widthRatio / 1.15f;
                         }else if(height <= 1024 && height >= 280){

                              heightRatio = heightRatio /1.45f;
                              widthRatio = widthRatio / 1.1f;

                          }else{
                              heightRatio = heightRatio /0.95f;
                              widthRatio = widthRatio / 0.8f;

                          }
                          float scale = widthRatio;          
                          float scale1 = heightRatio;
                          matrix.setScale(scale, scale1);


                      } else {
                          matrix.setTranslate(1f, 1f);
                      }

                    realBmp = BitmapFactory.decodeResource(getResources(),R.drawable.thefeelingwheelclear);
                    i1.setImageBitmap(realBmp);
                    i1.setImageMatrix(matrix);
                    i1.setOnTouchListener(this); 

    }

    @Override
           public boolean onTouch(View v, MotionEvent event) {
              ImageView view = (ImageView) v;
              view.setScaleType(ScaleType.MATRIX);
              dumpEvent(event);
              // Handle touch events here...
              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_UP:
              case MotionEvent.ACTION_POINTER_UP:
                 mode = NONE;
                 Log.d(TAG, "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);
                    Log.d(TAG, "newDist=" + newDist);
                    if (newDist > 10f) {
                       matrix.set(savedMatrix);
                       float scale = newDist / oldDist;
                       matrix.postScale(scale, scale, mid.x, mid.y);
                    }
                 }
                 break;
              }
              view.setImageMatrix(matrix);
              return true; // indicate event was handled
           }

           /** Show an event in the LogCat view, for debugging */
           private void dumpEvent(MotionEvent event) {
              String names[] = { "DOWN", "UP", "MOVE", "CANCEL", "OUTSIDE",
                    "POINTER_DOWN", "POINTER_UP", "7?", "8?", "9?" };
              StringBuilder sb = new StringBuilder();
              int action = event.getAction();
              int actionCode = action & MotionEvent.ACTION_MASK;
              sb.append("event ACTION_").append(names[actionCode]);
              if (actionCode == MotionEvent.ACTION_POINTER_DOWN
                    || actionCode == MotionEvent.ACTION_POINTER_UP) {
                 sb.append("(pid ").append(
                       action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
                 sb.append(")");
              }
              sb.append("[");
              for (int i = 0; i < event.getPointerCount(); i++) {
                 sb.append("#").append(i);
                 sb.append("(pid ").append(event.getPointerId(i));
                 sb.append(")=").append((int) event.getX(i));
                 sb.append(",").append((int) event.getY(i));
                 if (i + 1 < event.getPointerCount())
                    sb.append(";");
              }
              sb.append("]");
              Log.d(TAG, sb.toString());
           }

           /** Determine the space between the first two fingers */
           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);
           }