簡體   English   中英

在Android中實現像畫廊一樣的捏縮放

[英]implement pinch zoom like gallery in android

我在捏變焦上進行了過多的研發,並發現了很多用於Android的捏變焦示例。

如何在圖像縮放Android中捏縮放圖像?

Android捏縮放

但是,當我嘗試縮放大圖像(例如:> 10 MB,12200x8521)時,它無法解碼。 但是內置的畫廊可以做到,並且可以縮放。 甚至沒有縮放,但縮放后會顯示非常清晰的圖像。 雖然在上面pichZoomView無法像這樣縮放。

我發現QuickPic應用具有與圖庫相同的縮放功能。 我不知道該產品如何像系統圖庫應用程序那樣對圖像進行解碼和縮放。

如果我解碼完整圖像,則拋出OutOfMemoryException異常;如果我使用采樣解碼,則在縮放時,圖像模糊。

我花了很多時間來獲取圖庫應用程序的源代碼,但仍然無法從源代碼中找到實際的圖庫縮放實現。有人解決方案嗎? 或用於捏縮放實現的圖庫源代碼。

我花了很多時間,從下面的subsampling-scale-image-view得到了解決方案。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM