2016-09-01 4 views
2

Utilisation de la bibliothèque de transformations picasso pour l'édition d'images. Il existe deux boutons distincts pour la rotation gauche et la rotation droite.Sur un clic, l'image ne pivote qu'une seule fois. Je veux faire pivoter Image sur chaque clic de bouton dans sa direction respective.Faire pivoter l'image vers la gauche/droite par un clic sur le bouton

recyclerView.addOnItemTouchListener(new RecyclerClick(act, recyclerView, new RecyclerClickListener() { 
      @Override 
      public void onClick(View view, final int position) { 
       switch (position) { 
        case 0: 
         Picasso.with(act) 
           .load(selectedPhotoUri) 
           .rotate(90f) 
           .into(photo); 
         break; 
        case 1: 
         Picasso.with(act) 
           .load(selectedPhotoUri) 
           .rotate(90f) 
           .into(photo); 
         break; 
       } 
      } 
+0

Voulez-vous dire que le bouton clic ne fonctionne qu'une fois, quand il devrait tourner à chaque fois? –

Répondre

2

Les variables d'instance peuvent être utiles.

comme ceci:

recyclerView.addOnItemTouchListener(new RecyclerClick(act, recyclerView, new RecyclerClickListener() { 

    int rotate = 0; 

    @Override 
    public void onClick(View view, final int position) { 
     switch (position) { 
      case 0: 
       rotate += 90f 
       break; 
      case 1: 
       rotate -= 90f; 
       break; 
     } 
     Picasso.with(act) 
       .load(selectedPhotoUri) 
       .rotate(rotate) 
       .into(photo); 
    } 
})); 
1
public static Image rotate(Image img, double angle) { 
double sin = Math.abs(Math.sin(Math.toRadians(angle))), 
     cos = Math.abs(Math.cos(Math.toRadians(angle))); 

int w = img.getWidth(null), h = img.getHeight(null); 

int neww = (int) Math.floor(w*cos + h*sin), 
    newh = (int) Math.floor(h*cos + w*sin); 

BufferedImage bimg = toBufferedImage(getEmptyImage(neww, newh)); 
Graphics2D g = bimg.createGraphics(); 

g.translate((neww-w)/2, (newh-h)/2); 
g.rotate(Math.toRadians(angle), w/2, h/2); 
g.drawRenderedImage(toBufferedImage(img), null); 
g.dispose(); 

return toImage(bimg); 
} 

utiliser ce code pour faire pivoter l'image .. et appeler cette méthode sur votre CLICK