2010-12-02 14 views
2

Je veux qu'une image tourne pendant un temps indéfini ........ ce qui signifie que je veux le boucler. C'est ma tentative mais malheureusement ça ne marche pas. Aucune suggestion?Android Rotation de l'image

package com.android.test; 

import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Matrix; 
import android.graphics.drawable.BitmapDrawable; 
import android.os.Bundle; 
import android.widget.ImageView; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.LinearLayout; 
import android.widget.ImageView.ScaleType; 

public class imagerotate extends Activity { 
int x=1; 
int y=3; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     LinearLayout linearLayout = new LinearLayout(this); 

     while (y==3) { 
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon); 

    int width = bitmap.getWidth(); 
    int height = bitmap.getHeight(); 

    Matrix matrix = new Matrix(); 
    matrix.postRotate(x); 

    Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width,height, matrix, true); 
    BitmapDrawable bmd = new BitmapDrawable(rotatedBitmap); 

    ImageView imageView = new ImageView(this); 
    imageView.setImageDrawable(bmd); 
    imageView.setScaleType(ScaleType.CENTER); 

    linearLayout.addView(imageView, new LinearLayout.LayoutParams(
    LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
    setContentView(linearLayout); 

    x+=1; 
    } 
    } 
} 

Répondre

2

Vous ne pouvez pas boucler dans le thread principal. Cela rendra immédiatement votre application insensible. Pensez à utiliser un RotateAnimation - voir le lien pour la documentation.

+0

OK ................ Je ne sais pas ce que cela signifie ............. mais pouvez-vous s'il vous plaît me diriger vers un tutoriel ou quelque chose qui enseigne comment accomplir cela? OU pouvez-vous réparer mon code et l'afficher ici. Cela serait utile – efa

+0

Il y a une section dans les démos de l'API sur les animations. C'est un bon début: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/animation/index.html – EboMike

0

bien ici est la bonne façon de faire pivoter une image, s'il vous plaît voir cela va tourner l'image au hasard .. comme je l'utilise générateur aléatoire "comme une boucle" pour faire pivoter l'image entre 0 degré à 1000 degrés.

Random generator = new Random(); 
     int n = 10000; 
     n = generator.nextInt(n); 
     Matrix mtx = new Matrix(); 
     mtx.postRotate(n); 
     bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), 
       bmp.getHeight(), mtx, true); 
     contentView.setImageBitmap(bmp); 

alors bmp est le Bitmap vous voulez tourner. pour vous de faire pivoter l'image normalement, il suffit de changer le mtx.postRotate(n); en mtx.postRotate(90); ajouter au bouton ou aux paramètres du menu. c'est à vous. Bravo. éditer: P.S contentView fait référence à votre ImageView.

0

Vous pouvez utiliser le code suivant pour faire pivoter une vue d'image pendant une période INFINITE.

Animation rotateAnim = new RotateAnimation(0, 360); 
rotateAnim.setDuration(5000); 
rotateAnim.setRepeatCount(Animation.INFINITE); 
rotateAnim.setInterpolator(new AccelerateInterpolator()); 
rotateAnim.setRepeatMode(Animation.REVERSE); 
img.startAnimation(rotateAnim); 

Si votre durée totale doit être fixe, dites "total_duration". Et la "durée" pour un cycle est également définie. Vous pouvez utiliser le code ci-dessus avec le compte personnalisé comme

int count = total_duration/duration; 
Animation rotateAnim = new RotateAnimation(0, 360); 
rotateAnim.setDuration(duration); 
rotateAnim.setRepeatCount(count); 
rotateAnim.setInterpolator(new AccelerateInterpolator()); 
rotateAnim.setRepeatMode(Animation.REVERSE); 
img.startAnimation(rotateAnim); 

Espérons que cela aide.