0

Je travaille sur une fonction de sélection de mise en page multiple Frame d'une application Android.Comment faire pour obtenir la coordonnée supérieure et gauche de Bitmap Visible à l'intérieur de ImageView dans android

Mon exigence est d'obtenir les coordonnées en haut et à gauche de Bitmap après le zoom et pincer l'image. J'utilise TouchImageView pour la fonction Zoom et Déplacer.

J'ai essayé tellement de solutions SO, mais je n'ai pas obtenu la solution de mes besoins.

1er essai -

public static int[] getBitmapOffset(ImageView img, boolean includeLayout) { 
     int[] offset = new int[2]; 
     float[] values = new float[9]; 

     Matrix m = img.getImageMatrix(); 
     m.getValues(values); 

     offset[0] = (int) values[5]; 
     offset[1] = (int) values[2]; 

     if (includeLayout) { 
      ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) img.getLayoutParams(); 
      int paddingTop = (int) (img.getPaddingTop()); 
      int paddingLeft = (int) (img.getPaddingLeft()); 

      offset[0] += paddingTop + lp.topMargin; 
      offset[1] += paddingLeft + lp.leftMargin; 
     } 
     return offset; 
    } 

2ème essai -

  Rect r = img.getDrawable().getBounds(); 

      int drawLeft = r.left; 
      int drawTop = r.top; 
      int drawRight = r.right; 
      int drawBottom = r.bottom; 

      Logger.logsInfo(TAG, "Focus drawLeft : " + i + " : " +drawLeft); 
      Logger.logsInfo(TAG, "Focus drawTop : " + i + " : " +drawTop); 
      Logger.logsInfo(TAG, "Focus drawRight : " + i + " : " +drawRight); 
      Logger.logsInfo(TAG, "Focus drawBottom : " + i + " : " +drawBottom); 

S'il vous plaît laissez-moi savoir ce que je fais mal, parce que je travaille sur ce billet depuis 3 derniers jours et toujours pas fait .

Merci à l'avance. Suggestions vraiment appréciées.

+0

get 'img.getDrawingCache() vous allez continuer clairement visible. avant cela, ajoutez 'img.setDrawingCacheEnabled (true);' – Qamar

+0

Dans votre premier essai avez-vous essayé d'utiliser 'Matrix.postScale (yourScaleFactor, yourScaleFactor)'? –

+0

Vous redimensionnez l'image par la matrice. Après l'image 'postScale' obtenir de nouvelles coordonnées, je pense que matrix.mapRect va vous aider. Obtenez vue rect et carte avec matrice d'image puis gauche pointera vers votre x attendu. – Qamar

Répondre

0

Découvrez getZoomedRect de TouchImageView

Ajouter la fonction et le retour PointF topLeft = transformCoordTouchToBitmap(0, 0, true);

signature de la méthode

private PointF transformCoordTouchToBitmap(float x, float y, boolean clipToBitmap) 

En passant clipToBitmap moyenne réelle rect visible

/** 
    * Return a Rect representing the zoomed image. 
    * @return rect representing zoomed image 
    */ 
    public RectF getZoomedRect() { 
     if (mScaleType == ScaleType.FIT_XY) { 
      throw new UnsupportedOperationException("getZoomedRect() not supported with FIT_XY"); 
     } 
     PointF topLeft = transformCoordTouchToBitmap(0, 0, true); 
     PointF bottomRight = transformCoordTouchToBitmap(viewWidth, viewHeight, true); 

     float w = getDrawable().getIntrinsicWidth(); 
     float h = getDrawable().getIntrinsicHeight(); 
     return new RectF(topLeft.x/w, topLeft.y/h, bottomRight.x/w, bottomRight.y/h); 
    } 
+0

Merci pour cela, mais je reçois ceci - RectF (0.90758044, 0.0, 0.9993077, 0.33333334) –

+0

Et aussi comment puis-je redessiner cette image par RectF? –

+0

Consultez cette méthode docs. Canvas.drawBitmap (Bitmap, Rect, RectF, Paint) – Qamar