2013-09-02 6 views
5

Ich versuche, die PhotoView-Bibliothek zu verwenden, um ein Zuschneiden-Tool für Fotos zu erstellen, aber ich habe Probleme, den Wert von zurückgegeben getDisplayRect(). Ich habe das Foto auf den ImageView wie so:Ich habe Probleme zu verstehen, was PhotoViews getDisplayRect() tatsächlich zurückgibt (Android-Foto zuschneiden Tool)

photo.setImageDrawable(new BitmapDrawable(getResources(), image)); 

wo image das Bitmap-Objekt ist. Ich habe dann Setup einige Skalierungswerte:

float minScale = ((float)image.getWidth() > (float)image.getHeight()) 
    ? (float)image.getWidth()/(float)image.getHeight() 
    : (float)image.getHeight()/(float)image.getWidth(); 
attacher.setMaxScale(minScale * 5f); 
attacher.setMidScale(minScale * 2.5f); 
attacher.setMinScale(minScale); 
attacher.setScale(minScale, (float)image.getWidth()/2f,(float)image.getHeight()/2f, false); 
attacher.update(); 

wo attacher ist das PhotoViewAttacher Objekt.

Wenn der Benutzer i verwenden Sie die folgende, um zu bestimmen den Teil der Bitmap, die in der ImageView erfolgt sichtbar ist: obwohl

RectF rect  = attacher.getDisplayRect(); 
float scale    = attacher.getScale(); 
PhotoData ret = new PhotoData(data); 
ret.x  = (int)(Math.abs(rect.left)/scale); 
ret.y  = (int)(Math.abs(rect.top)/scale); 
ret.width = (int)(rect.width()/scale); 
ret.height = (int)(rect.height()/scale); 

ich unerwartete Ergebnisse erhalten. Vielleicht kann hier jemand Einblick geben?

Antwort

6

Hier ist die Antwort:

  RectF rect   = attacher.getDisplayRect(); 
      float viewScale  = attacher.getScale(); 

      float imageRatio = (float)size.width()/(float)size.height(); 
      float viewRatio = (float)photoView.getWidth()/(float)photoView.getHeight(); 

      float scale = 0; 
      if (imageRatio > viewRatio) { 
       // scale is based on image width 
       scale = 1/((float)size.width()/(float)photoView.getWidth()/viewScale); 

      } else { 
       // scale is based on image height, or 1 
       scale = 1/((float)size.height()/(float)photoView.getHeight()/viewScale); 
      } 

      // translate to bitmap scale 
      rect.left  = -rect.left/scale; 
      rect.top  = -rect.top/scale; 
      rect.right  = rect.left + ((float)photoView.getWidth()/scale); 
      rect.bottom  = rect.top + ((float)photoView.getHeight()/scale); 

      if (rect.top<0) { 
       rect.bottom -= Math.abs(rect.top); 
       rect.top = 0; 
      } 
      if (rect.left<0) { 
       rect.right -= Math.abs(rect.left); 
       rect.left = 0; 
      } 
+0

Was Size.width ist() und Size.height()? – braintrapp

+0

@ user3809445 Ich nehme an, die Größe Variable bezieht sich auf die Bitmap der ursprünglichen Bilddatei, die in der PhotoView angezeigt wurde. – AmuletxHeart