2012-11-11 5 views
1

Je prépare une activité somecompass. J'ai ma rose de vents .png et je veux la mettre à l'échelle au début de l'activité et la faire pivoter après que l'orientation ait changé. Le problème est que lorsque je redimensionne mon image (onCreate seulement), après la première (et seulement la première) utilisation de la méthode rotate, ma vue est redimensionnée une fois de plus, et je ne sais pas pourquoi.Mise à l'échelle et rotation de l'image

Laissez-moi vous envoyer mes méthodes de rotation et d'échelle. rose est mon instance ImageView (ImageView rose)

S'il vous plaît jeter un oeil et poster des conseils. Dois-je essayer d'autres façons de redimensionner mon ImageView?

Merci pour toute aide!

Rotation (ressemble fonctionne bien)

public void rotateRose(float angle){ 

      angle = 360 - angle; 
      Matrix matrix=new Matrix(); 
      rose.setScaleType(ScaleType.MATRIX); 
      pivX = rose.getDrawable().getBounds().width()/2; 
      pivY = rose.getDrawable().getBounds().height()/2; 
      matrix.preRotate((float) angle, pivX, pivY); 
      rose.setImageMatrix(matrix); 
} 

Échelle (trouvé source link)

private void scaleImage(ImageView view, int boundBoxInDp) 
    { 
     // Get the ImageView and its bitmap 
     Drawable drawing = view.getDrawable(); 
     Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap(); 

     // Get current dimensions 
     int width = bitmap.getWidth(); 
     int height = bitmap.getHeight(); 

     // Determine how much to scale: the dimension requiring less scaling is 
     // closer to the its side. This way the image always stays inside your 
     // bounding box AND either x/y axis touches it. 
     float xScale = ((float) boundBoxInDp)/width; 
     float yScale = ((float) boundBoxInDp)/height; 
     float scale = (xScale <= yScale) ? xScale : yScale; 

     // Create a matrix for the scaling and add the scaling data 
     Matrix matrix = new Matrix(); 
     matrix.postScale(scale, scale); 

     // Create a new bitmap and convert it to a format understood by the ImageView 
     Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true); 
     BitmapDrawable result = new BitmapDrawable(scaledBitmap); 
     width = scaledBitmap.getWidth(); 
     height = scaledBitmap.getHeight(); 

     // Apply the scaled bitmap 
     view.setImageDrawable(result); 

     // Now change ImageView's dimensions to match the scaled image 
     RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) view.getLayoutParams(); 
     params.width = width; 
     params.height = height; 
     view.setLayoutParams(params); 

     //pivX = width/2; 
     // pivY = height/2; 
    } 

Répondre

1

Ok je l'ai!

remplacer

view.setImageDrawable(result); 

dans la méthode d'échelle avec

view.setImageBitmap(scaledBitmap); 

Si vous êtes à la recherche de méthodes d'échelle et la rotation pour affichage de l'image: Ces travaux ci-dessus bien (avec ce petit changement)

Questions connexes